Taylor Series figure problem

4 次查看(过去 30 天)
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')

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-2-11
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')

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-2-11
编辑:Walter Roberson 2023-2-11
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')

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by