# ode45 solving a differential equation that has no solution for given boundary conditions

4 views (last 30 days)
Ankit Singh on 8 Jul 2021
Commented: Ankit Singh on 9 Jul 2021
Hi ,
I used the ode45 to solve a second order differential equation by manipulating it with symbolic toolbox first. The ode45 solves the system of first order differential equatio obtained from the toolbox instead of giving an error.
Does the ode45 return an error if the boundary value problem has no solution?
Here is what i did..
syms x(t)
eqn = diff(x,2) + x == 0
[V] = odeToVectorField(eqn)
M = matlabFunction(V,'vars',{'t','Y'})
sol = ode45(M ,[0 pi],[1 1])
there s no solution for f(0) = 1 and f(pi) = 1 ..but the solver still solves but solves incorrectly ..of course

John D'Errico on 8 Jul 2021
Edited: John D'Errico on 8 Jul 2021
So, you formulated a 2nd degree ODE. Converted it as:
syms x(t)
eqn = diff(x,2) + x == 0
eqn(t) =
[V] = odeToVectorField(eqn)
V =
M = matlabFunction(V,'vars',{'t','Y'})
M = function_handle with value:
@(t,Y)[Y(2);-Y(1)]
Then you created TWO initial conditions. You did not formulate a boundary value problem, which ODE45 is NOT designed to handle. It handles INITAL value problems.
sol = ode45(M ,[0 pi],[1 1])
sol = struct with fields:
solver: 'ode45' extdata: [1×1 struct] x: [0 0.2010 0.5151 0.8293 1.1434 1.4576 1.7717 2.0859 2.4001 2.7142 3.0284 3.1416] y: [2×12 double] stats: [1×1 struct] idata: [1×1 struct]
That second argument is the span in t to solve over. It does NOT tell ODE45 to solve the problem you thought you were solving. READ THE HELP!
So the solution it returns looks like this:
plot(sol.x,sol.y(1,:))
Did you think that call told ODE45 to solve a problem with x(0) = 1, and x(pi) == 1? WRONG. As you should see, x'(0) looks to be 1.
That call told ODE45 to solve the problem where x(0) == 1, AND x'(0) == 1. Does that have a solution? Of course it does!
dx = diff(x,t,1);
xsol = dsolve(eqn,x(0) == 1,dx(0)==1)
xsol =
fplot(xsol,[0,pi])
It should be no surprise the two look alike.
Again, ODE45 is NOT a boundary value solver.
Now, what happens if you decide to solve the problem you wanted to solve? First in symbolic form:
xsolb = dsolve(eqn,x(0) ==1, x(pi) == 1)
Warning: Unable to find symbolic solution.
xsolb = [ empty sym ]
So dsolve could not find a solution. (Hey, you got that correct.) Now it would seem more appropriate to use a solver like bvp4c.
Ankit Singh on 9 Jul 2021
ahh..my bad. should have carefully read the ode45 help. thank for the nice explanation.

R2021a

### Community Treasure Hunt

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

Start Hunting!