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

%%%% 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 件のコメント

ebtisam almehmadi
ebtisam almehmadi 2021 年 8 月 3 日
Plot I get looks acceptable but the code is wrong. Please Help

サインインしてコメントする。

回答 (1 件)

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);

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2021 年 8 月 3 日

回答済み:

2021 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by