Couple ODE System not enough Input arguments, Why?
1 次查看(过去 30 天)
显示 更早的评论
Hey all,
I have a coupled system of ODE's that I want to solve and followed the instructions. However, there seems to be an error with the input arguments. Could somebody help?
Best
% Define the boundary value problem
solinit = bvpinit(linspace(0,1,10), @guess);
% Solve the boundary value problem
sol = bvp4c(bvpfun, bcfun, solinit);
% Plot the solution
x = linspace(0,1,100);
y = deval(sol, x);
plot(x, y(1,:), 'b-', 'LineWidth', 2);
hold on
plot(x, y(2,:))
xlabel('x');
ylabel('y');
title('Solution of Second Order ODE with BVP');
grid on;
function dydx = bvpfun(x, y)
dydx = zeros(3, 1); % Initialize dydx as a column vector
dydx(1) = y(3) + y(1);
dydx(2) = y(1) - 2/y(3);
dydx(3) = y(1) - 1;
dydx = [dydx(1); dydx(2); dydx(3)];
end
function res = bcfun(ya,yb)
res(1) = ya(1);
res(2) = ya(2);
res(3) = ya(3);
end
function y = guess(x)
y = [0; 0; 0];
end
2 个评论
Sam Chak
2024-4-3
From the dynamics
the condition given at the boundary , , causes the singularity .
回答(3 个)
Aquatris
2024-4-3
编辑:Aquatris
2024-4-3
You should use @ symbol before the functions in your bvp4c call. Now you have another error, good luck solving it :D hint: what happens when y(3) = 0.
% Define the boundary value problem
solinit = bvpinit(linspace(0,1,10), @guess);
% Solve the boundary value problem
sol = bvp4c(@bvpfun, @bcfun, solinit);
% Plot the solution
x = linspace(0,1,100);
y = deval(sol, x);
plot(x, y(1,:), 'b-', 'LineWidth', 2);
hold on
plot(x, y(2,:))
xlabel('x');
ylabel('y');
title('Solution of Second Order ODE with BVP');
grid on;
function dydx = bvpfun(x, y)
dydx = zeros(3, 1); % Initialize dydx as a column vector
dydx(1) = y(3) + y(1);
dydx(2) = y(1) - 2/y(3);
dydx(3) = y(1) - 1;
dydx = [dydx(1); dydx(2); dydx(3)];
end
function res = bcfun(ya,yb)
res(1) = ya(1);
res(2) = ya(2);
res(3) = ya(3);
end
function y = guess(x)
y = [0; 0; 0];
end
0 个评论
Sam Chak
2024-4-3
Hi @Thanh Hoang
I have added these four lines to your original code. The modifications made are minimal.
%% ----- added these 4 lines -----
ivpfun = @bvpfun;
xspan = [0, 1];
yinit = [0; 0; 0];
sol = ode45(ivpfun, xspan, yinit);
%% ----- added these 4 lines -----
% sol = bvp4c(@bvpfun, @bcfun, solinit);
% Plot the solution
x = linspace(0,1,100);
y = deval(sol, x);
plot(x, y(1,:), 'b-', 'LineWidth', 2);
hold on
plot(x, y(2,:))
xlabel('x');
ylabel('y');
title('Solution of Second Order ODE with BVP');
grid on;
function dydx = bvpfun(x, y)
dydx = zeros(3, 1); % Initialize dydx as a column vector
dydx(1) = y(3) + y(1);
dydx(2) = y(1) - y(3)/2;
dydx(3) = y(1) - 1;
dydx = [dydx(1);
dydx(2);
dydx(3)];
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Boundary Value Problems 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!