Clear Filters
Clear Filters

How to stop pi appearing as a text in the disp result?

2 views (last 30 days)
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?

Accepted Answer

Dyuman Joshi
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)
0.707429206709773
  4 Comments
Dyuman Joshi
Dyuman Joshi on 23 Apr 2023
@Image Analyst, OP is trying to estimate the value of cos(pi/4) not pi/4
cos(pi/4)
ans = 0.7071

Sign in to comment.

More Answers (2)

Image Analyst
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).

Walter Roberson
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);
0.7074
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.

Community Treasure Hunt

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

Start Hunting!