Taylor series: Error estimation

4 次查看(过去 30 天)
Rahul Janardhanan
Rahul Janardhanan 2020-8-29
评论: David Hill 2020-8-30
Trying to estimate the error for taylor series expansion for 1/1-x. I'm trying to see the convergence for various values of N (no. of iterations) within a certain domain (x). I want to know how to calculate the error and plot it vs changing N.
This is what I have so far:
x= -0.9:0.05:0.9;
y= 1./(1-x);
N = 1; %N: no. of terms
y1 = 0*y;
for i = 0:N
y1= y1+(x.^i);
e1= abs(y-y1);
end
N2 = 2;
y2=0*y;
for i = 0:N2
y2 = y2+(x.^i);
e2= abs(y-y2);
end
N3 = 10;
y3=0*y;
for i= 0:N3
y3= y3+(x.^i);
e3= abs(y-y3);
end
N4 = 100;
y4=0*y;
e4 = 0*0;
for i = 0:N4
y4 = y4+(x.^i);
e4 = abs(y-y4);
end
hold on
plot(x,y,'*r')
plot(x,y1,'-b');
plot(x,y2,'^g');
plot(x,y3,'>k');
plot(x,y4,'--m');
hold off
figure
hold on
grid on
plot(1:N+1,e1,'ok'); %Matlab shows an error while doing this
plot(1:N2+1,e2,'-b');
plot(1:N3+1,e3,'^g');
plot(1:N4+1,e4,'pc');
hold off

回答(1 个)

David Hill
David Hill 2020-8-30
x= (-0.9:0.05:0.9)';
y= 1./(1-x);
m=cumsum(repmat(x,1,101).^(0:100),2);
err=abs(y-m);
surf(repmat(x,1,101),repmat([0:100],37,1),err);
figure;
hold on;
grid on;
plot(x,y,'*r');
plot(x,m(:,2),'-b');
plot(x,m(:,3),'^g');
plot(x,m(:,11),'>k');
plot(x,m(:,101),'--m');
  2 个评论
Rahul Janardhanan
Rahul Janardhanan 2020-8-30
Can you please elaborate about what the surf plot displays? Thanks!
David Hill
David Hill 2020-8-30
The surface plot is displaying all the x-values, N from 0-100, and corresponding errors on the z-axis.

请先登录,再进行评论。

类别

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

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by