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
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);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!