Taylor Series figure problem

4 views (last 30 days)
Consider the following expression:
f(x)= x^2 sin(x)+e^-x * cos^2 (x)
Plot varying 'x' from '-π' to '+π' for 100 points.
Using Taylor's series expansion for of degree 4, plot the graph with the above graph using hold on feature for same range of x.
Add Taylor series of degrees 7 and 10 to the same plot.
Compare the results. In the graph, add legend, title, axis titles. Add different line style, markers, colors. Choose appropriate font size for the graph.
this is my code and I got two figures but one of them is not the true solution which is the first figure so can you please help?
xx= linspace(-pi,pi, 100);
syms x
fx=(x*x^2)*sin(x*x)+exp(-x*x)*(cos(x*x)^2);
figure(1)
fplot(xx,fx, 'linewidth' ,2)
title('Original Signal')
xlabel('x')
ylabel('f(x)')
f= (x^2)*sin(x)+exp(-x)*(cos(x)^2);
T4 = taylor(f,x, 'Order',4);
figure (2)
fplot(T4,[-pi pi],'--')
hold on;
T7 = taylor(f,x,'Order', 7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order', 10);
fplot(T10,[-pi pi],'-')
legend('Degree = 4','Degree = 7', 'Degree = 10')
xlabel('x')
ylabel('f(x)')
title('Taylor Series Approximation')

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 11 Feb 2023
The first function definition is incorrect
xx = linspace(-pi,pi, 100);
syms x
%function definition
f(x)=(x^2)*sin(x)+exp(-x)*(cos(x)^2);
%You can use the same definition for the whole code
figure(1)
plot(xx,double(f(xx)),'linewidth',2)
xlim([-pi pi])
title('Original Signal')
xlabel('x')
ylabel('f(x)')
T4 = taylor(f,x,'Order',4);
figure (2)
fplot(T4,[-pi pi],'--')
hold on;
T7 = taylor(f,x,'Order',7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order',10);
fplot(T10,[-pi pi],'-')
legend('Degree = 4','Degree = 7', 'Degree = 10')
xlabel('x')
ylabel('f(x)')
title('Taylor Series Approximation')

More Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2023
Edited: Walter Roberson on 11 Feb 2023
Your fplot syntax was wrong.
xx= linspace(-pi,pi, 100);
syms x
fx=(x*x^2)*sin(x*x)+exp(-x*x)*(cos(x*x)^2);
figure(1)
fplot(fx, [-pi pi], 'linewidth' ,2)
title('Original Signal')
xlabel('x')
ylabel('f(x)')
f= (x^2)*sin(x)+exp(-x)*(cos(x)^2);
T4 = taylor(f,x, 'Order',4);
figure (2)
fplot(fx, [-pi pi], 'linewidth' ,2)
hold on
fplot(T4,[-pi pi],'--')
T7 = taylor(f,x,'Order', 7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order', 10);
fplot(T10,[-pi pi],'-')
legend({'origina', 'Degree = 4','Degree = 7', 'Degree = 10'})
xlabel('x')
ylabel('f(x)')
title('Taylor Series Approximation')

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!