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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 8월 29일

댓글:

2020년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by