常见函数的Taylor级数展开的可视化过程
1、常见函数的Taylor级数展开式
一些常见函数的Taylor级数展开如下:
-
指数函数 e x e^x ex 在 a = 0 a = 0 a=0 处的展开:
e x = 1 + x + x 2 2 ! + x 3 3 ! + ⋯ = ∑ n = 0 ∞ x n n ! e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots = \sum_{n=0}^{\infty} \frac{x^n}{n!} ex=1+x+2!x2+3!x3+⋯=n=0∑∞n!xn -
正弦函数 sin ( x ) \sin(x) sin(x) 在 a = 0 a = 0 a=0 处的展开:
sin ( x ) = x − x 3 3 ! + x 5 5 ! − ⋯ = ∑ n = 0 ∞ ( − 1 ) n x 2 n + 1 ( 2 n + 1 ) ! \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \dots = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n+1}}{(2n+1)!} sin(x)=x−3!x3+5!x5−⋯=n=0∑∞(−1)n(2n+1)!x2n+1 -
余弦函数 cos ( x ) \cos(x) cos(x) 在 a = 0 a = 0 a=0 处的展开:
cos ( x ) = 1 − x 2 2 ! + x 4 4 ! − ⋯ = ∑ n = 0 ∞ ( − 1 ) n x 2 n ( 2 n ) ! \cos(x) = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \dots = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n}}{(2n)!} cos(x)=1−2!x2+4!x4−⋯=n=0∑∞(−1)n(2n)!x2n
2、指数函数的泰勒展开可视化
可视化代码
clc;
clear;
close all;
% 定义目标函数
f = @(x) exp(x); % 目标函数:e^x
% 选择展开点
x0 = 0; % 展开点
% 定义x的取值范围
x = linspace(-2, 2, 400);
% 计算实际函数值
y_actual = f(x);
% 定义要展示的泰勒级数项数
terms_to_show = [1,2,3,4,5,6,7,8,9];
% 创建一个图形窗口
figure;
% 绘制实际函数
plot(x, y_actual, 'k', 'LineWidth', 2, 'DisplayName', '实际函数 f(x)');
hold on;
% 颜色循环
colors = jet(length(terms_to_show));
legend_str = {'实际函数 f(x)'};
% 用于保存GIF的变量
filename = 'taylor_series.gif';
% 计算并动态展示不同项数的泰勒级数展开
for i = 1:length(terms_to_show)
n_terms = terms_to_show(i);
y_taylor = zeros(size(x));
for n = 0:n_terms-1
if n == 0
f_n = f(x0);
else
syms X;
sym_f = sym(f(X));
f_n = double(subs(diff(sym_f, n), X, x0));
end
taylor_term = (f_n * (x - x0).^n) / factorial(n);
y_taylor = y_taylor + taylor_term;
end
% 绘制当前项数的泰勒级数近似
plot(x, y_taylor, '--', 'LineWidth', 2, 'Color', colors(i,:), 'DisplayName', ['泰勒项数=' num2str(n_terms)]);
legend_str{end+1} = ['泰勒项数=' num2str(n_terms)];
% 更新图例
legend(legend_str, 'Location', 'northwest');
title(['泰勒级数展开: f(x) = e^x at x = ' num2str(x0), ', 项数=', num2str(n_terms)]);
% 绘制当前帧并保存到GIF
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf, 'DelayTime',0.5);
else
imwrite(imind,cm,filename,'gif','WriteMode','append', 'DelayTime',0.5);
end
end
% 关闭绘图保持
hold off;
% 显示GIF文件的路径
disp(['GIF文件已保存到: ', fullfile(pwd, filename)]);
3、正弦函数的泰勒展开可视化
% 定义目标函数
f = @(x) sin(x); % 目标函数:sin(x)
% 选择展开点
x0 = 0; % 展开点
% 定义x的取值范围
x = linspace(-2*pi, 2*pi, 400);
% 计算实际函数值
y_actual = f(x);
% 定义要展示的泰勒级数项数
terms_to_show = [1,2,3,4,5,6,7,8];
% 创建一个图形窗口
figure;
% 绘制实际函数
plot(x, y_actual, 'k', 'LineWidth', 2, 'DisplayName', '实际函数 f(x)');
hold on;
% 颜色循环
colors = cool(length(terms_to_show));
legend_str = {'实际函数 f(x)'}; % 初始图例
% 用于保存GIF的变量
filename = 'taylor_series_sin.gif';
% 计算并动态展示不同项数的泰勒级数展开
for i = 1:length(terms_to_show)
n_terms = terms_to_show(i);
y_taylor = zeros(size(x));
% 计算泰勒级数展开
for n = 0:n_terms-1
% 使用泰勒展开公式:(x-x0)^n / n!
y_taylor = y_taylor + ((-1)^n * x.^(2*n+1)) / factorial(2*n+1);
end
% 绘制当前项数的泰勒级数近似
plot(x, y_taylor, '--', 'LineWidth', 2, 'Color', colors(i,:), 'DisplayName', ['泰勒项数=' num2str(n_terms)]);
legend_str{end+1} = ['泰勒项数=' num2str(n_terms)];
% 更新图例
legend(legend_str, 'Location', 'northwest');
title(['泰勒级数展开: f(x) = sin(x) at x = ' num2str(x0), ', 项数=', num2str(n_terms)]);
% 绘制当前帧并保存到GIF
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf, 'DelayTime',0.5);
else
imwrite(imind,cm,filename,'gif','WriteMode','append', 'DelayTime',0.5);
end
end
% 关闭绘图保持
hold off;
% 显示GIF文件的路径
disp(['GIF文件已保存到: ', fullfile(pwd, filename)]);
3、余弦函数的泰勒展开可视化
% 定义目标函数
f = @(x) cos(x); % 目标函数:cos(x)
% 选择展开点
x0 = 0; % 展开点
% 定义x的取值范围
x = linspace(-2*pi, 2*pi, 400);
% 计算实际函数值
y_actual = f(x);
% 定义要展示的泰勒级数项数
terms_to_show = [1,2,3,4,5,6,7,8];
% 创建一个图形窗口
figure;
% 绘制实际函数
plot(x, y_actual, 'k', 'LineWidth', 2, 'DisplayName', '实际函数 f(x)');
hold on;
% 颜色循环
colors = cool(length(terms_to_show));
legend_str = {'实际函数 f(x)'}; % 初始图例
% 用于保存GIF的变量
filename = 'taylor_series_cos.gif';
% 计算并动态展示不同项数的泰勒级数展开
for i = 1:length(terms_to_show)
n_terms = terms_to_show(i);
y_taylor = zeros(size(x));
% 计算泰勒级数展开
for n = 0:n_terms-1
% 使用泰勒展开公式:((-1)^n * (x^(2n)) / (2n)!) 用于cos(x)
y_taylor = y_taylor + ((-1)^n * x.^(2*n)) / factorial(2*n);
end
% 绘制当前项数的泰勒级数近似
plot(x, y_taylor, '--', 'LineWidth', 2, 'Color', colors(i,:), 'DisplayName', ['泰勒项数=' num2str(n_terms)]);
legend_str{end+1} = ['泰勒项数=' num2str(n_terms)];
% 更新图例
legend(legend_str, 'Location', 'northwest');
title(['泰勒级数展开: f(x) = cos(x) at x = ' num2str(x0), ', 项数=', num2str(n_terms)]);
% 绘制当前帧并保存到GIF
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf, 'DelayTime',0.5);
else
imwrite(imind,cm,filename,'gif','WriteMode','append', 'DelayTime',0.5);
end
end
% 关闭绘图保持
hold off;
% 显示GIF文件的路径
disp(['GIF文件已保存到: ', fullfile(pwd, filename)]);
4、普通非线性函数的泰勒展开可视化
% 定义目标函数
f = @(x) x + (1/3) * x.^3; % 目标函数:f(x) = x + (1/3) * x^3
% 选择展开点
x0 = 0; % 展开点
% 定义x的取值范围
x = linspace(-2, 2, 400);
% 计算实际函数值
y_actual = f(x);
% 定义要展示的泰勒级数项数
terms_to_show = [1,2,3,4];
% 创建一个图形窗口
figure;
% 绘制实际函数
plot(x, y_actual, 'k', 'LineWidth', 2, 'DisplayName', '实际函数 f(x)');
hold on;
% 颜色循环
colors = jet(length(terms_to_show));
legend_str = {'实际函数 f(x)'}; % 初始图例
% 用于保存GIF的变量
filename = 'taylor_series_f.gif';
% 计算并动态展示不同项数的泰勒级数展开
for i = 1:length(terms_to_show)
n_terms = terms_to_show(i);
y_taylor = zeros(size(x));
% 计算泰勒级数展开
for n = 0:n_terms-1
% 计算泰勒展开的每一项:f^(n)(0) * (x - x0)^n / n!
% 逐阶计算导数值
if n == 0
f_n = f(x0); % f(0) = 0 + (1/3)*0^3 = 0
elseif n == 1
f_n = 1 + x0^2; % f'(0) = 1 + 0^2 = 1
elseif n == 2
f_n = 2 * x0; % f''(0) = 2*0 = 0
elseif n == 3
f_n = 2; % f'''(x) = 2
else
f_n = 0; % 高阶导数为0
end
% 泰勒展开公式
taylor_term = (f_n * (x - x0).^n) / factorial(n);
y_taylor = y_taylor + taylor_term;
end
% 绘制当前项数的泰勒级数近似
plot(x, y_taylor, '--', 'LineWidth', 2, 'Color', colors(i,:), 'DisplayName', ['泰勒项数=' num2str(n_terms)]);
legend_str{end+1} = ['泰勒项数=' num2str(n_terms)];
% 更新图例
legend(legend_str, 'Location', 'northwest');
title(['泰勒级数展开: f(x) = x + (1/3) * x^3 at x = ' num2str(x0), ', 项数=', num2str(n_terms)]);
% 绘制当前帧并保存到GIF
drawnow
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf, 'DelayTime',0.5);
else
imwrite(imind,cm,filename,'gif','WriteMode','append', 'DelayTime',0.5);
end
end
% 关闭绘图保持
hold off;
% 显示GIF文件的路径
disp(['GIF文件已保存到: ', fullfile(pwd, filename)]);