ODE symbolic result plotting with fplot()

4 次查看(过去 30 天)
Hello,
So I am trying to solve an ODE using the symbolic toolbox.
syms phi(t) g l d m
dphi = diff(phi,t); % Derivative of phi(t)
phi(t) = dsolve(diff(phi,t,t)== -g/l*phi-d/(m*l^2)*diff(phi,t),... % ODE (governing the movement of a pendulum)
phi(0) ==1,... % Initial condition 1
dphi(0)==0) % Initial condition 2
phi(t) = subs(phi(t),{l,m,g,d},{10,5,9.81,50}) % Replacing with known values
fplot(phi,[1 100]) % symbolic plotting between 1 and 100 s
I get the expected result but when I plot it, I get numerous vertical lines that add noise to my plot and it is almost unreadable. I am using Matlab on a Mac. How do i get rid of those lines ?

采纳的回答

Star Strider
Star Strider 2020-11-28
I am not certain what the problem is with the original function.
If you simplify it first:
phi = vpa(simplify(phi, 'Steps',500),5)
the vertical lines (indicating infinite results) disappear. (I use vpa here to shorten the output so I can see all of it. It is not necessary for the code.)
  8 个评论
M.Many
M.Many 2020-11-28
Thanks for your answer, I do understand this.
But I do not agree with that sentence : 'Full precision, however you define it, is maintained intermally, so nothing is lost.'
Please have a look at the following code, that I use with my previous phi function evaluated at 1:
>> phi(1)-vpa(phi(1))
ans =
cos(978500^(1/2)/1000)*exp(-1/20) + (978500^(1/2)*sin(978500^(1/2)/1000)*exp(-1/20))/19570 - 0.562748412343890484348604524131142562316
Which gives the exact error we make by using vpa (because it is an expression where the cosines and square roots are not evaluated) and it is not 0. We can evaluate the error by applying the vpa function again :
>> vpa(phi(1)-vpa(phi(1)),100)
ans =
0.000000000000000000000000000000000000000000000001010157116631602241471408195383622793040240093593829057563725786616790742733299511920032906991801494
So full precision is not maintained internally
Again if we type the following :
>> phi(1)-phi(1)
ans =
0
we get an exact symbolic 0 because it is the result of comparing expressions and not evaluations of expressions.
Am I correct or am I missing something here ?
Star Strider
Star Strider 2020-11-28
The vpa function converts fractions to their decimal-fraction equivalents, so there can be approximation errors in the conversion of finite fractions to finite decimal fractions. Nevertheless, the fractions retain their full internal precision, and the results of vpa retains its full internal precision. Because of the conversion, the original fractions and the decimal equivalents are not absolutely the same.
So will always be the same, however (however many finite terms it uses) will never be exactly equal to it.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by