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

2 次查看(过去 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 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by