Set answers of first BVP as new boundary condition for second BVP
3 次查看(过去 30 天)
显示 更早的评论
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);
% 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
0 个评论
回答(1 个)
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 Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!