Taylor Series figure problem

4 ビュー (過去 30 日間)
Mohammed Alahmad
Mohammed Alahmad 2023 年 2 月 11 日
編集済み: Walter Roberson 2023 年 2 月 11 日
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 Center および File ExchangeCalculus についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by