How do I solve this equation?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
John D'Errico
2020-5-23
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.
0 个评论
更多回答(1 个)
Ameer Hamza
2020-5-23
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'})
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!