BVP Error, help with explaination
2 views (last 30 days)
Show older comments
Hello all,
I am struggeling a bit with understanding why I get an error message here. The code works fine with bigger numbers for A or with smaller numbers for B but is crashing for example with the given parameters. I already figured that y(1) is becoming NaN at some point which causes the code to crash. Could somebody help me understand why this is happening?
bvp()
function bvp
A = 50e-3;
B = 1;
xmesh = linspace(0, 1, 100);
sol_init = bvpinit(xmesh, @init);
sol = bvp4c(@ode, @bcfun, sol_init);
function dydx = ode(x, y)
dydx(1) = y(2);
dydx(2) = exp(y(1)*log(10)/A);
%disp(y(1))
end
% Define the boundary conditions
function res = bcfun(ya, yb)
res(1) = ya(1) - B;
res(2) = yb(1);
end
% Define the initial guess for the solution
function guess = init(x)
guess(1) = B;
guess(2) = 0;
end
end
0 Comments
Answers (2)
Ayush Anand
on 23 May 2024
Edited: Ayush Anand
on 23 May 2024
The magnitude of the exponential function exp(y(1)*log(10)/A) blows up very rapidly as A is . This is most probably causing the overflow, leading to an Inf (infinity) value in MATLAB. Further calculations involving Inf can lead to NaN (Not a Number), as these are operations that MATLAB cannot numerically evaluate to a real or infinite number.
Also, the solver bvp4c iteratively adjusts y(1) and y(2) to satisfy both the differential equation and the boundary conditions. If during its iterations, y(1) becomes large enough, exp(y(1)*log(10)/A) will overflow. Once y(1) or any subsequent value becomes NaN, the solver cannot proceed with numeric computations and thus the error you see.
2 Comments
Ayush Anand
on 24 May 2024
I think as mentioned by Torsten in the other answer, the solver reaches the limits of computation/precision possible on the machine and hence can't proceed with any further feasible steps. It seems to be associated with the system of equations itself and you may want to change the formulation of the system.
Torsten
on 23 May 2024
Edited: Torsten
on 23 May 2024
The solver is no longer able to capture the steep gradient of your solution at x=0.
Look at sol.x in the last step of the continuation method from below and how fine the mesh has to be chosen around x = 0.
bvp()
function bvp
A_array = [5e-1,3e-1,2e-1,1.5e-1,1e-1,0.9e-1,0.8e-1,0.7e-1,0.6e-1,0.55e-1,0.5e-1];
B = 1;
xmesh = linspace(0, 1, 100);
sol_init = bvpinit(xmesh, [0 B]);
hold on
for i = 1:numel(A_array)
A = A_array(i);
sol = bvp4c(@ode, @bcfun, sol_init);
plot(sol.x,sol.y(1,:))
init = @(x)interp1(sol.x.',sol.y.',x);
sol_init = bvpinit(sol.x,init);
end
format long
sol.x
hold off
grid on
function dydx = ode(x, y)
dydx(1) = y(2);
dydx(2) = exp(y(1)*log(10)/A);
%disp(y(1))
end
% Define the boundary conditions
function res = bcfun(ya, yb)
res(1) = ya(1) - B;
res(2) = yb(1);
end
end
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!