How to stop pi appearing as a text in the disp result?
2 views (last 30 days)
Show older comments
Tuna Dikmen
on 23 Apr 2023
Commented: Image Analyst
on 23 Apr 2023
I want to estimate the value of cos(pi/4) using the taylor series expansion around the point x=0, here is my code,
syms x
f= cos(x);
t6= taylor(f,x);
val=subs(t6,x,pi/4);
disp(val);
I expect the result as a floating number however the result is printed as : pi^4/6144 - pi^2/32 + 1
How can I overcome this?
0 Comments
Accepted Answer
Dyuman Joshi
on 23 Apr 2023
Convert the symbolic value to floating point value -
format long
syms x
f = cos(x);
t6 = taylor(f,x);
val = double(subs(t6,x,pi/4));
disp(val)
4 Comments
More Answers (2)
Image Analyst
on 23 Apr 2023
I suggest you don't use symbolic terms at all. I suggest you just do it numerically with a for loop to sum some number of terms (like a thousand or so).
0 Comments
Walter Roberson
on 23 Apr 2023
sympref('FloatingPointOutput', true);
syms x
f= cos(x);
t6= taylor(f,x);
val=subs(t6,x,pi/4);
disp(val);
I do not recommend this approach: there is not much control over the number of digits output, and there are (in my opinion) too many places where you would want symbolic instead of floating point values.
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!