How do I solve this equation?

1 view (last 30 days)
Joo Seo Lee
Joo Seo Lee on 23 May 2020
Answered: John D'Errico on 23 May 2020
The given equation is
d^2(x)/dt^2=0.002cos(x-t)-sin(x)
I tried to solve it by
for t=0:100
dsolve('D2x==0.002*cos(x-t)-sin(x)','x(0)==0,Dx(0)==0')
end
plot(x,t)
but it doesn't work

Accepted Answer

John D'Errico
John D'Errico on 23 May 2020
I've added a separate answer here only to explain how you would have used dsolve, and then how you would plot the solution.
You don't use a loop to solve it like that. There is no need to vary t in a loop, since the solution, IF dsolve is able to find one, will be a function of t already. Anyway, those multiple calls to dsolve saved no result in any variable to be able to then plot.
I might have tried this:
syms x(t)
Dx = diff(x,t);
sol = dsolve(diff(x,t,2)==0.002*cos(x-t)-sin(x),x(0)==0,Dx(0)==0)
Warning: Unable to find explicit solution.
> In dsolve (line 190)
sol =
[ empty sym ]
However MATLAB gives up, unable to find a solution. That may mean it simply needs a little help. For example, convert the shifted cosine into a pair of terms using an identity, thus;
cos(x-t) = cos(x)*cos(t) + sin(x)*sin(t)
This too fails however.
sol = dsolve(diff(x,t,2)==0.002*(cos(x)*cos(t) + sin(x)*sin(t))-sin(x),x(0)==0,Dx(0)==0)
Warning: Unable to find explicit solution.
> In dsolve (line 190)
sol =
[ empty sym ]
Unfortunately, it is quite easy to write a differential equation that lacks a solution, at least one that dsolve can handle. Had dsolve managed to find a solution, for example here on a much simpler problem, then the plot can be gained from fplot.
sol = dsolve(diff(x,t)==sin(x),x(0)==1)
sol =
2*atan(exp(t + log(tan(1/2))))
fplot(sol,[0,3*pi])
Lacking a solution, you are best served using a numerical solver. Ameer has shown how to do that already.

More Answers (1)

Ameer Hamza
Ameer Hamza on 23 May 2020
You ode does not seem to have an analytical solution (at least dsolve() is not able to find a solution). You can try a numerical solver, e.g., ode45. See this example for second-order ODE.: https://www.mathworks.com/help/matlab/ref/ode45.html#bu3uj8b. This is the code for your equation
dfun = @(t, x) [x(2); 0.002*cos(x(1)-t)-sin(x(1))];
time = [0 100];
ic = [0; 0];
[t, x] = ode15s(dfun, time, ic);
plot(t, x);
legend({'x', 'xdot'})

Tags

Community Treasure Hunt

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

Start Hunting!