Taylor series: Error estimation
4 次查看(过去 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
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!