MATLAB Answers

wrong differential equation solution

6 views (last 30 days)
Hello guys, I have been struggling with a differential equation. I solved it manually, and then double-checked my answer both manually and with Wolfram Alpha, and the answer was right. However, matlab seems to give another answer. The two answers are supposed to be the same, just written in different forms. However, after I copied the Wolfram Alpha answer to matlab, I found out that they are actually not equal.Did I do anything wrong? Thank you.


madhan ravi
madhan ravi on 31 Jul 2020
Share the actual code as text instead of pictures.
Graciano Ding
Graciano Ding on 31 Jul 2020
syms x(t)
which gives the output
sol=(9*cos(3*t))/80 + (9*exp(-t))/20 + (7*sin(3*t))/80 - (5*cos(t))/16 + (25*sin(t))/16 - (cos(2*t)*(cos(3*t)/8 - sin(3*t)/8 + (3*cos(t))/8 + sin(t)/8))/2 - (tan(t/2)*cos(t)*(tan(t/2) - 3*tan(t/2)^2 - 6*tan(t/2)^3 + 3*tan(t/2)^4 + tan(t/2)^5 - tan(t/2)^6 + 1))/(tan(t/2)^2 + 1)^4
Obviously the answer is too complicated, so I used "simplify" command
which gives the output:
ans=(9*exp(-t))/20 - (2*cos(t))/5 + (8*sin(t))/5 - cos(t)^3/20 - (3*cos(t)^2*sin(t))/20
just to show you that the answers from matlab and wolfram alpha are different, I checked by doing the copying the wolfram answer to matlab:
which gives a quite complicated answer:
ans(t)=(25*sin(x(t)))/16 - sin(3*t)/8 - cos(3*t)/8 - cos(t)/8 - (25*sin(t))/16 + (cos(2*t)*(cos(3*t)/8 - sin(3*t)/8 + (3*cos(t))/8 + sin(t)/8))/2 + (tan(t/2)*cos(t)*(tan(t/2) - 3*tan(t/2)^2 - 6*tan(t/2)^3 + 3*tan(t/2)^4 + tan(t/2)^5 - tan(t/2)^6 + 1))/(tan(t/2)^2 + 1)^4
which gives
ans=(25*sin(x(t)))/16 - (25*sin(t))/16
If the answers from matlab and wolfram were equal, the result should be 0.
Thank you

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 31 Jul 2020
Edited: John D'Errico on 31 Jul 2020
Sorry. I don't know what you did incorrectly, but here is what I see.
syms y(t)
yalpha = (1/80)*(36*exp(-t) + 125*sin(t) -3*sin(3*t) -35*cos(t) -cos(3*t));
ode = diff(y,t,3) + diff(y,t,2) + diff(y,t) + y == cos(3*t);
dy = diff(y,t);
ddy = diff(y,t,2);
ysol = simplify(dsolve(ode,y(0) == 0,dy(0) == 1,ddy(0) == 1))
ysol =
(9*exp(-t))/20 - (2*cos(t))/5 + (8*sin(t))/5 - cos(t)^3/20 - (3*cos(t)^2*sin(t))/20
simplify(ysol - yalpha)
ans =
The difference is identically zero. You may have copied something incorrectly. It is difficult to know, because we don't truly know what is inside the variables you claim have created.


Show 2 older comments
Graciano Ding
Graciano Ding on 1 Aug 2020
That seems complicated and I honestly didn't expect it at all xD, but anyway...thank you so much!
John D'Errico
John D'Errico on 1 Aug 2020
No problem. It was an interesting exercise in arm twisting. The problem is, if I look at virtually any of the intermediate forms, they all could be construed as of similar complexity to any other. So when we tell MATLAB to simplify the expression, it needed a little direction.
There is another trick that can sometimes work. It is pretty random though. If I add two arguments to simplify, it will return a sequence of variations on the expression.
ysimpl = simplify(ysol,'all',true,'steps',20)
ysimpl =
(9*exp(-t))/20 - (2*cos(t))/5 + (29*sin(t))/20 - cos(t)^3/20 + (3*sin(t)^3)/20
(exp(-t)*(29*exp(t)*sin(t) - 8*exp(t)*cos(t) - exp(t)*cos(t)^3 + 3*exp(t)*sin(t)^3 + 9))/20
(9*exp(-t))/20 - (10^(1/2)*cos(3*t - atan(3)))/80 - (674^(1/2)*cos(t + atan(25/7)))/16
(9*cos(t*1i))/20 + (sin(t*1i)*9i)/20 - (2*cos(t))/5 + (29*sin(t))/20 - cos(t)^3/20 - (3*sin(t)*(cos(t)^2 - 1))/20
(sin(t*1i)*9i)/20 + (29*sin(t))/20 + (3*sin(t)^3)/20 - ((2*sin(t/2)^2 - 1)*(sin(t)^2 - 1))/20 + (4*sin(t/2)^2)/5 - (9*sin((t*1i)/2)^2)/10 + 1/20
(9*exp(-t))/20 - exp(-t*1i)*(1/5 - 29i/40) - exp(t*1i)*(1/5 + 29i/40) - (exp(-t*1i)/2 + exp(t*1i)/2)^3/20 + (3*((exp(-t*1i)*1i)/2 - (exp(t*1i)*1i)/2)^3)/20
-exp(t*(- 1 - 3i))*(- (9*exp(t*3i))/20 + exp(t*(1 + 2i))*(7/32 - 25i/32) + exp(t*(1 + 4i))*(7/32 + 25i/32) + exp(t*(1 + 6i))*(1/160 - 3i/160) + exp(t)*(1/160 + 3i/160))
(29*tan(t/2))/(10*(tan(t/2)^2 + 1)) - (9*(tan((t*1i)/2) - 1i))/(20*(tan((t*1i)/2) + 1i)) + (tan(t/2)^2 - 1)^3/(20*(tan(t/2)^2 + 1)^3) + (6*tan(t/2)^3)/(5*(tan(t/2)^2 + 1)^3) + (2*(tan(t/2)^2 - 1))/(5*(tan(t/2)^2 + 1))
Here, for example, I see it has managed to find the first step in the sequence I chose as one of the alternatives. Now I might decide to try another shot in the dark.
ysol = ysimpl(1)
ysol =
(9*exp(-t))/20 - (2*cos(t))/5 + (29*sin(t))/20 - cos(t)^3/20 + (3*sin(t)^3)/20
ysimpl = simplify(ysol,'all',true,'steps',200);
This was a huge mess of variations. One of them however got me closer to my goal. Now repeat the process, examoming the results to see if simplify was able to help me out.
ysol = ysimpl(4)
ysol =
(9*exp(-t))/20 - cos(3*t)/80 - (7*cos(t))/16 + (29*sin(t))/20 + (3*sin(t)^3)/20
ysimpl = simplify(ysol,'all',true,'steps',200);
ans =
(9*exp(-t))/20 - cos(3*t)/80 - (3*sin(3*t))/80 - (7*cos(t))/16 + (25*sin(t))/16
Scanning down through the mess it produces gives me an answer, at the 5th step in that sequence.
Honestly, the intelligently directed solution I did in my previous answer was more satisfying. But sometimes, if you have no clue what to try, this may help you get past a bump in the road.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 31 Jul 2020
a1 = matlabFunction(simplify(sol));
a = matlabFunction((1/80)*(36*exp(-t)-35*cos(t)-cos(3*t)+125*sin(t)-3*sin(3*t)));
t = linspace(0, 2*pi);
all(abs(a1(t) - a(t)) < 1e-2) % to check if they yield the same results

  1 Comment

Graciano Ding
Graciano Ding on 1 Aug 2020
I don't know what I did wrong, I did again and the result was 0. But anyway, thank you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by