ODE symbolic result plotting with fplot()
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
Thanks for the answer, it works indeed but the vpa function just evaluates the terms (fractions,square roots etc...) in the symbolic phi function, so it is not anymore the exact solution.
I think I figured out the problem, phi is actually a complex function that's why it doesn't plot so well. I have to take the real part of phi and then plot it to get the expected solution and now it works. Thanks !
fplot(real(phi),[1 100]) % symbolic plotting between 1 and 100 s
Star Strider
2020년 11월 28일
As always, my pleasure!
The vpa function affects only the display of the expression. MATLAB maintains full internal symbolic precision, unless you tell it otherswise (using round, fix, ceil, floor, for example, that may not apply to symbolic expressions, or convert it to an anonymous function with matlabFunction, that then uses full floating-point precision).
When I plotted both the real and imaginary parts of ‘phi’, the imaginary part was identically 0. Also, if it had not been so, fplot (and other ploltting functions) would have thrown a warning about ignoring the imaginary parts.
M.Many
2020년 11월 28일
Yes you are right ! The simplify() command somehow doesn't want to further simplify the complex expression of the real function phi, since, as you said, the imaginary part is zero.
Star Strider
2020년 11월 28일
Thank you!
M.Many
2020년 11월 28일
However, the documentation on the vpa function clearly tells it approximates the symbolic expression
"vpa(x) uses variable-precision floating-point arithmetic (VPA) to evaluate each element of the symbolic input x to at least d significant digits, where d is the value of the digits function. The default value of digits is 32."
Star Strider
2020년 11월 28일
Everything in a computer has a finite precision (consider evaluating any expression involving π otherwise), and beyond a certain number of significant digits, the results are usually not important. Note that the symbolic numbers use 32 decimal digits (that can be increased if necessary), while floating-point precision is limited to 16 decimal digits. Those are generally enough for most applications. The vpa function simply limits the display of an expression. Full precision, however you define it, is maintained intermally, so nothing is lost.
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
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.
will always be the same, however 추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
