Couple ODE System not enough Input arguments, Why?

조회 수: 31 (최근 30일)
Thanh Hoang
Thanh Hoang 2024년 4월 3일
답변: Sam Chak 2024년 4월 3일
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에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by