wrong differential equation solution

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.

2 个评论

Share the actual code as text instead of pictures.
syms x(t)
Dx=diff(x,t);
D2x=diff(x,t,2);
cond=Dx(0)==1;
cond1=D2x(0)==1;
cond2=x(0)==0;
deq=diff(x,t,3)+diff(x,t,2)+diff(x,t)+x==cos(3*t);
sol=dsolve(deq,cond,cond1,cond2)
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
simplify(sol)
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:
a=(1/80)*(36*exp(-t)-35*cos(t)-cos(3*t)+125*sin(x)-3*sin(3*t));
a-sol
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
simplify(ans(t))
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

请先登录,再进行评论。

 采纳的回答

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 =
0
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.

5 个评论

Tnank you! And by the way, is it possible to make matlab show the answer in the same form as in wolfram?
Convincing a symbolic solver to give you exactly the form you want it to produce is not always easy. They can be cantankerous beasts, and you need to twist their arms a bit. The problem is, if all you do is issue a generic command like simplify, the code will make it as simple as it can, simple in the eyes of the author of the code, that is. As well, it may need to be forced to transform specific terms to achieve your goal. Human eyes and brains are great at seeing an overall picture, but computers can be pretty dumb at times.
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
First, I'll convert the cos^2*sin term into a sin^3 term. This part is easy enough.
ysol = expand(subs(ysol,cos(t)^2,1 - sin(t)^2))
ysol =
(9*exp(-t))/20 - (2*cos(t))/5 + (29*sin(t))/20 - cos(t)^3/20 + (3*sin(t)^3)/20
Now we have terms in there with the cube of a sin and the cube of a cos. I recall seeing trig identities for terms like cos(t)^3 and sin(t)^3. And, since Google is my friend, I quickly found them online. Just search for the "cosine cubed formula".
ysol = subs(ysol,cos(t)^3,(3*cos(t) + cos(3*t))/4)
ysol =
(9*exp(-t))/20 - cos(3*t)/80 - (7*cos(t))/16 + (29*sin(t))/20 + (3*sin(t)^3)/20
In fact, the sine cubed formula is pretty close in form. You can find that online too. As you can see, it looks a lot like the other.
ysol = subs(ysol,sin(t)^3,(3*sin(t) - sin(3*t))/4)
ysol =
(9*exp(-t))/20 - cos(3*t)/80 - (3*sin(3*t))/80 - (7*cos(t))/16 + (25*sin(t))/16
Now does that compare to the Wolfram solution?
yalpha
yalpha =
(9*exp(-t))/20 - cos(3*t)/80 - (3*sin(3*t))/80 - (7*cos(t))/16 + (25*sin(t))/16
As you can see, they match up perfectly now.
To be honest, I wish the rewrite function had a cos^3 option and a sin^3 option. Other powers of trig functions would be nice too That would have made things a little faster to do the work, but the above did succeed.
That seems complicated and I honestly didn't expect it at all xD, but anyway...thank you so much!
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);
ysimpl(5)
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.

请先登录,再进行评论。

更多回答(1 个)

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 个评论

I don't know what I did wrong, I did again and the result was 0. But anyway, thank you!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by