Solve second order non linear differential equation
12 views (last 30 days)
Show older comments
I need to solve F'' + F^2 -1/2pi = 0
Boundary conditons
F(0) = 0;
F(inf) = 1
I am new to using the ode solver in matlab and am not sure how to make it solve a non-linear SECOND order equation. Any suggestion would be appreciated.
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 9 Nov 2021
You can try a builtin fcn: bvp4c(). Here is a nice documentation on bvp4c() with examples: https://www.mathworks.com/help/matlab/ref/bvp4c.html
3 Comments
John D'Errico
on 9 Nov 2021
I don't think the bvpsolver can handle an infinite domain, since it uses collocation.
More Answers (2)
John D'Errico
on 9 Nov 2021
You will not be able to use bvp4c directly, since it will use collocation. That will fail on an infinite domain.
One option might be to use a finite domain, but one that is relatively large. So [0,Xmax] where Xmax is sufficiently far out that the solve has a chance to finish in a reasonable time, yet not that far out that numerical problems exist.
I did try dsolve, and it fails to find a solution, but that is not unexpected. My next suggestion is to use a transformation of the domain into a bounded domain. For example, you might try a transformation like
y = atan(x)
This maps the domain [0,inf] into [0,pi/2], which may be surprisingly a good idea, considering the equation at hand. You can see some ideas here on a similar problem:
But certainly other links exist where people explain how to transform such a problem into a finite domain.
3 Comments
John D'Errico
on 10 Nov 2021
Edited: John D'Errico
on 10 Nov 2021
As a quick shot, it might look like this.
xmax = 100;
xmesh = linspace(0,xmax,1001);
guess = @(x) [x/xmax;repmat(1/xmax,size(x))]; % linear, but satisfies the BC
solinit = bvpinit(xmesh, guess)
bcfun = @(ya,yb) [ya(1);yb(1) - 1]; % zero at x=zero, 1 at x=xmax
% the second order BVP, sonverted to a system of two first order equations.
% Since the ODE is:
% y'' + y^2 - pi/2 = 0
%
% converted to a 1st order system, we will have
% y1 = y
% y2 = y'
%
% Then the problem reduces to the nonlinear system:
% y1' = y2
% y2' = -y1^2 + pi/2
bvpfun = @(X,Y) [Y(2);-Y(1)^2 + pi/2];
sol = bvp4c(bvpfun,bcfun,solinit)
plot(sol.x,sol.y)
Which does not look very satisfying to me. I cannot really talk about any limiting behavior as x --> inf, since it seems to be oscillating between two limits for large x. And that means what it returned as a possible solution is pretty much pure crapola. I do note that bvp4c did not think it had converged. That might suggest I got something wrong in the equations (easy enough, I was not terribly careful) or that I provided a poor initial guess.
So perhaps a better guess might make sense. Suppose I guess the relationship looks like sin(x) over that range, thus starts at zero, then approaches 1 at the right end point?
guess = @(x) [sin(x/xmax*pi/2);cos(x/xmax*pi/2)*pi/2/xmax];
solinit = bvpinit(xmesh, guess);
sol = bvp4c(bvpfun,bcfun,solinit);
plot(sol.x,sol.y)
Far less nasty looking, but still not happy.
Ok, consider if my initial guess looks like erf? Erf has the necessary properties, that it satisfies the bounday conditions, and it is trivial to differentiate.
xmax = 20;
xmesh = linspace(0,xmax,1001);
guess = @(x) [erf(x);1/sqrt(pi)*exp(-x.^2)]; % linear, but satisfies the BC
solinit = bvpinit(xmesh, guess);
plot(solinit.x,solinit.y)
sol = bvp4c(bvpfun,bcfun,solinit);
At least this time, bvp4c does not have a hissy fit about convergence. Probably due to the narrower interval.
plot(sol.x,sol.y)
Again, while I chose a smaller interval, the solution bvp4c finds is a purely oscillatory one. And that makes the boundary condition as x--> inf would seem to be meaningless if the function is a sinusoidally oscillating one. Actually though, it is not so, since you can have a function that is everywhere oscillatory, yet has a finite limit at infinity. For example:
y = 1 + exp(-x)*sin(x)
has a limit of 1 as x--> inf, yet is purely oscillatory. Is that the behavior I would expect for any solution of this ODE? Well, actually, yes. That is, suppose F(x) is a function that approaches a limit of 1 at inf, where the derivatives, so F'(x) and F''(x) all approach zero? That cannot be a solution to your ODE, thus:
F''(x) + F(x)^2 - pi/2 = 0
since then as x--> inf, if F''(x)--0, then we would have
1^2 - pi/2 = 0
which is clearly false. And that means ANY solution to this problem is indeed an oscillatory one. That relieves me, since bvp4c was trying to tell me that all along.
MOSLI KARIM
on 17 Dec 2022
Pi=1;
BC=@(ya,yb)[ya(1)
yb(1)-1];
dxdy=@(x,y)[y(2);(1/2)*Pi-y(1)^2];
solinit=bvpinit(linspace(0,1,5),[0 0]);
sol=bvp4c(dxdy,BC,solinit);
figure(2)
hold on
plot(sol.x,sol.y)
0 Comments
See Also
Categories
Find more on Boundary Value Problems 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!