Clear Filters
Clear Filters

Couple ODE System not enough Input arguments, Why?

42 views (last 30 days)
Thanh Hoang
Thanh Hoang on 3 Apr 2024 at 8:58
Answered: Sam Chak on 3 Apr 2024 at 12:28
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 Comments
Sam Chak
Sam Chak on 3 Apr 2024 at 12:03
From the dynamics
the condition given at the boundary , , causes the singularity .
Thanh Hoang
Thanh Hoang on 3 Apr 2024 at 12:13
Hey,
thanks for pointing this out, there was a tippo from my side it should have been y2' = y1 - y3 / 2

Sign in to comment.

Answers (3)

Aquatris
Aquatris on 3 Apr 2024 at 10:02
Edited: Aquatris on 3 Apr 2024 at 10:06
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 on 3 Apr 2024 at 11:22
Moved: Torsten on 3 Apr 2024 at 11:22
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 on 3 Apr 2024 at 12:28
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

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!