I need to fix the code by using for loop to plot the relative error E in 2 norm versus n.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%%%% 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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/701627/image.jpeg)
Réponses (1)
Sivani Pentapati
le 2 Sep 2021
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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!