simple Fixed Point Iteration

2 visualizzazioni (ultimi 30 giorni)
지민 이
지민 이 il 7 Nov 2021
Commentato: 지민 이 il 7 Nov 2021
clear all;
clc;
x(1)=0;
g=@(x) exp(-x);
f=@(x) exp(-x)-x;
true_root=0.56714329;
disp('----------------------------------------------------');
disp('i xi Ea(%) Et(%) ');
disp('----------------------------------------------------');
for i=1:10
x(i+1)=g(x(i));
end
if x~=0
ea=100*abs(x(i+1)-g(x(i))/x(i+1));
end
e_t=100*abs(x-true_root)/true_root;
res=[[0:10]' x' ea' e_t'];
fprintf('%d %1.6f %3.3f %3.3f\n', res');
i want to add the ea. but i can. how can i this??

Risposta accettata

Alan Stevens
Alan Stevens il 7 Nov 2021
Probably more like this (though you don't seem to have used function f anywhere):
n = 11;
x = zeros(1,numel(n));
ea = zeros(1,numel(n));
g=@(x) exp(-x);
f=@(x) exp(-x)-x;
true_root=0.56714329;
disp('----------------------------------------');
----------------------------------------
disp('i xi Ea(%) Et(%) ');
i xi Ea(%) Et(%)
disp('----------------------------------------');
----------------------------------------
for i=1:n-1
x(i+1)=g(x(i));
if x(i+1)~= 0
ea(i+1) = 100*abs(x(i+1)-x(i))/x(i+1);
end
end
e_t = 100*abs(x-true_root)/true_root;
res=[(0:n-1)' x' ea' e_t'];
fprintf('%d %10.6f %10.3f %10.3f\n', res');
0 0.000000 0.000 100.000 1 1.000000 100.000 76.322 2 0.367879 171.828 35.135 3 0.692201 46.854 22.050 4 0.500474 38.309 11.755 5 0.606244 17.447 6.894 6 0.545396 11.157 3.835 7 0.579612 5.903 2.199 8 0.560115 3.481 1.239 9 0.571143 1.931 0.705 10 0.564879 1.109 0.399

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by