Effacer les filtres
Effacer les filtres

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

7 vues (au cours des 30 derniers jours)
Thanh Hoang
Thanh Hoang le 8 Avr 2024
Commenté : Thanh Hoang le 10 Avr 2024
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

Réponses (1)

Torsten
Torsten le 8 Avr 2024
Modifié(e) : Torsten le 8 Avr 2024
"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

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by