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
Sam Chak 2024-4-3
From the dynamics
the condition given at the boundary , , causes the singularity .
Thanh Hoang
Thanh Hoang 2024-4-3
Hey,
thanks for pointing this out, there was a tippo from my side it should have been y2' = y1 - y3 / 2

请先登录,再进行评论。

回答(3 个)

Aquatris
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);
Error using bvp4c (line 196)
Unable to solve the collocation equations -- a singular Jacobian encountered.
% 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

Torsten
Torsten 2024-4-3
移动:Torsten 2024-4-3
If all boundary conditions are given at x = 0 as in your case, you have an initial value problem instead of a boundary value problem. In this case, use "ode45" instead of "bvp4c".

Sam Chak
Sam Chak 2024-4-3
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

类别

Help CenterFile Exchange 中查找有关 Boundary Value Problems 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by