필터 지우기
필터 지우기

Set answers of first BVP as new boundary condition for second BVP

조회 수: 2 (최근 30일)
Thanh Hoang
Thanh Hoang 2024년 4월 8일
댓글: Thanh Hoang 2024년 4월 10일
Hello all,
I want to set the answer of one BVP as a boundary condition for a second equation. However, there seems to be an issue with evaluating the solution of the first BVP. Do you have suggestings how to solve this?
Best,
Thanh
% Solve the first BVP
solinit1 = bvpinit(linspace(0, 1, 10), [0 0]);
sol1 = bvp4c(@myFirstODEFun, @myFirstBCFun, solinit1);
% Evaluate the solution of the first BVP at x = 1
value_at_1 = deval(sol1, 1);
% Solve the second BVP
solinit2 = bvpinit(linspace(0, 1, 10), [0 0]);
sol2 = bvp4c(@mySecondODEFun, @mySecondBCFun, solinit2);
Unrecognized function or variable 'value_at_1'.

Error in solution>mySecondBCFun (line 21)
res = [ya(1) - value_at_1(1); yb(1) - 2]; % Use value_at_1 in the boundary conditions

Error in bvparguments (line 97)
testBC = bc(ya,yb,bcExtras{:});

Error in bvp4c (line 119)
bvparguments(solver_name,ode,bc,solinit,options,varargin);
% Define the first ODE and its boundary conditions
function dydx = myFirstODEFun(x, y)
dydx = [y(2); -y(1)]; % Your first ODE here
end
function res = myFirstBCFun(ya, yb)
res = [ya(1); yb(2) - 1]; % Boundary conditions for the first ODE
end
% Define the second ODE and its boundary conditions, incorporating value_at_1
function dydx = mySecondODEFun(x, y)
dydx = [y(2); -2*y(1)]; % Your second ODE here
end
function res = mySecondBCFun(ya, yb)
res = [ya(1) - value_at_1(1); yb(1) - 2]; % Use value_at_1 in the boundary conditions
end
% Plot or analyze sol2 as needed

답변 (1개)

Torsten
Torsten 2024년 4월 8일
편집: Torsten 2024년 4월 8일
"value_at_1" is defined in the script part of your code, but it's not automatically visible in the functions you define. Either pass "value_at_1" to "mySecondBCFun" as additional input or use nested functions as done below.
main()
function main()
% Solve the first BVP
solinit1 = bvpinit(linspace(0, 1, 10), [0 0]);
sol1 = bvp4c(@myFirstODEFun, @myFirstBCFun, solinit1);
% Evaluate the solution of the first BVP at x = 1
value_at_1 = deval(sol1, 1);
% Solve the second BVP
solinit2 = bvpinit(linspace(0, 1, 10), [0 0]);
sol2 = bvp4c(@mySecondODEFun, @mySecondBCFun, solinit2);
hold on
plot(sol1.x,sol1.y(1,:))
plot(sol2.x,sol2.y(1,:))
hold off
grid on
% Define the first ODE and its boundary conditions
function dydx = myFirstODEFun(x, y)
dydx = [y(2); -y(1)]; % Your first ODE here
end
function res = myFirstBCFun(ya, yb)
res = [ya(1); yb(2) - 1]; % Boundary conditions for the first ODE
end
% Define the second ODE and its boundary conditions, incorporating value_at_1
function dydx = mySecondODEFun(x, y)
dydx = [y(2); -2*y(1)]; % Your second ODE here
end
function res = mySecondBCFun(ya, yb)
res = [ya(1) - value_at_1(1); yb(1) - 2]; % Use value_at_1 in the boundary conditions
end
% Plot or analyze sol2 as needed
end

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by