I need to fix the code by using for loop to plot the relative error E in 2 norm versus n.

조회 수: 3 (최근 30일)
%%%% Taylor ploynomials pn(x)
x=2:0.01:3;
f = 1./x;
p1=1/2.5;
p2= 1/2.5 -(4/25)*(x-2.5);
p3= 1/2.5 -(4/25)*(x-2.5) + (8/125)*(x-2.5).^2;
p4= 1/2.5 -(4/25)*(x-2.5) + (8/125)*(x-2.5).^2 -(16/625)*(x-2.5).^3;
E1=sqrt((f-p1).^2)/sqrt((f).^2)
E2=sqrt((f-p2).^2)/sqrt((f).^2)
E3=sqrt((f-p3).^2)/sqrt((f).^2)
E4=sqrt((f-p4).^2)/sqrt((f).^2)
n=[1 2 3 4]
E=[ E1 E2 E3 E4];
semilogy(n,E)

답변 (1개)

Sivani Pentapati
Sivani Pentapati 2021년 9월 2일
Please refer to the below code snippet to calculate the l2 norm of error in iterative way. For more information, please refer to for loop in MATLAB documentation.
p(1,:)=1/2.5;
for i=2:4
p(i,:)= p(i-1,:)+ (4/25)*(2/5).^(i-2)*(-1).^(i-1)*(x-2.5).^(i-1);
end
E=sqrt((f-p).^2)/sqrt((f).^2);
n=1:4;
semilogy(n,E);

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by