作业帮 > 综合 > 作业

matlab 泰勒函数逼近

来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/04/29 21:30:20
matlab 泰勒函数逼近
用matlab做x*sin(x)的泰勒级数
matlab 泰勒函数逼近
close all
clear,clc
syms x;
f = x*sin(x);
t = taylor(f);
% 画x*sin(x)原函数
plotT = ezplot(f,[-3,3]);
set(plotT,'Color','red','LineWidth',4);
% 画x*sin(x)(n = 2 4 6 8)阶泰勒级数的曲线
NN = 3:2:9;
x1 = -3:0.01:3;
for I = 1:4
y = taylor(f,NN(I)); % degree n-1 functions
y1 = subs(y,x,x1);
PY(I,:) = y1(1,:); % degree n-1 functions
end
hold on;
plot(x1,PY','LineWidth',2);
xlabel('x')
ylabel('Tn(x)')
title('Taylor Series Expansion')
legend('sin(x)/x','n = 2','n = 4','n = 6','n = 8','Location','North')
grid on