Solving DE initial value problem symbolically in Matlab

9 views (last 30 days)
Hello, I'm trying to solve a differential equation IVP symbolically with MATLAB, but my answer is different than the actual answer. Am I doing something wrong?
syms x
dsolve('x*Dy+y=-sin(x)','y(pi/2)=0')
Gives the answer as:
exp(-t/x)*exp(pi/(2*x))*sin(x) - sin(x)
But the actual answer should be:
-(x*sin(x)-cos(x))/x^2

Accepted Answer

John D'Errico
John D'Errico on 29 Jul 2025
Edited: John D'Errico on 29 Jul 2025
Before we go too far, we want to look at your claimed solution.
syms y(x) % tells matlab that y is an unknown function of x
ODE = x*diff(y,x)+y==-sin(x); % defines the ODE, so we can use it
yoursol(x) = -(x*sin(x)-cos(x))/x^2;
First, does your solution satisfy the initial conditions?
yoursol(pi/2)
ans = 
In fact, no, it does not even satisfy your own boundary condition, so we know your claimed solution is not correct. I won't even bother to see if it "satisfies" the ODE, aince it cannot do so.
Now lets use dsolve to solve the ODE.
sol(x) = dsolve(ODE,y(pi/2) == 0)
sol(x) = 
Does this satisfy the ODE?
sol(pi/2)
ans = 
0
simplify(subs(ODE,y,sol))
ans(x) = 
symtrue
Sorry. It looks like dsolve got it right.
  2 Comments
rezheen
rezheen on 29 Jul 2025
Yes, you're correct. The solution is cos(x)/x. The answer I gave above is y' which I took the derivative of cos(x)/x I'd get it. But the problem asked inc my book was giving cos(x)/x as a candidate solution and take derivative of it, plug it into the original question, we'd get -sin(x). Basically an algebra problem after a bit of calculus. Thanks for the answers.
John D'Errico
John D'Errico on 29 Jul 2025
Edited: John D'Errico on 30 Jul 2025
My point is, dsolve does give the correct solution. IF you use it properly. As far as the result you wanted to see, nothing stops you from doing exactly as you said, since you already should know how to do that.
You used dsolve (incorrectly), and it gave you an "answer". The answer does not agree with the answer to a question that you never even told us about, a completely different question from that implicit in what you tried to solve using dsolve.
Your real problem in the code was you gave MATLAB an equation where too much was left undefined, unspecified. Should y be a function of x? If not specified, it seems dsolve got that wrong, because it can only solve the problem as posed, using its own defined set of rules for how the input will be interpreted.
Instead, you need to tell MATLAB what is a function of what. Never assume a computer can read your mind. It will get things wrong far too often. Well, at least until the mind reading toolbox comes out, and I'm not sure you really want a copy of that toolbox anyway. The proposed name is the Terminator toolbox. ;-)

Sign in to comment.

More Answers (1)

Torsten
Torsten on 29 Jul 2025
Edited: Torsten on 29 Jul 2025
By default, y is assumed a function of t:
syms x y(t)
sol(t) = dsolve('x*Dy+y=-sin(x)','y(pi/2)=0')
Warning: Support for character vector or string inputs will be removed in a future release. Instead, use syms to declare variables and replace inputs such as dsolve('Dy = -3*y') with syms y(t); dsolve(diff(y,t) == -3*y).
sol(t) = 
simplify(x*diff(sol,t)+sol(t)+sin(x))
ans(t) = 
0
sol(pi/2)
ans = 
0
... but you want y as a function of x:
syms y(x)
sol(x) = dsolve(x*diff(y,x)+y==-sin(x),y(pi/2)==0)
sol(x) = 
simplify(x*diff(sol,x)+sol+sin(x))
ans(x) = 
0
sol(pi/2)
ans = 
0

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!