How do I save the error data for each iteration?

13 vues (au cours des 30 derniers jours)
Willson Blesstian
Willson Blesstian le 9 Mar 2021
Hello,
I have a bisection function code that doesn't quite do what I want it to do. I want it to output the error data for each iteration. Right now, it's only giving me the final error value. I know I'm supposed to do err = [] or something like that but I haven't been able to implement it correctly. Could I please get some assistance? Thank you
function [x_i,err,yi] = bisect(f,x_l,x_r,delta)
yl = feval(f,x_l);
yr = feval(f,x_r);
err = [];
if yl*yr > 0
disp('error'),0;
else
max1 = 1+round((log(x_r-x_l)-log(delta))/log(2));
for k = 1:max1
x_i = (x_l+x_r)/2;
yi = feval(f,x_i);
if yi == 0
x_l = x_i;
x_r = x_i;
elseif yr*yi > 0
x_r = x_i;
yr = yi;
else
x_l = x_i;
yl = yi;
end
if x_r-x_l < delta, break,end
end
end
x_i = (x_l+x_r)/2;
err = abs(x_r-x_l);
yi = feval(f,x_i);

Réponse acceptée

Mathieu NOE
Mathieu NOE le 9 Mar 2021
hello
simply put err computation in the main loop and index it
function [x_i,err,yi] = bisect(f,x_l,x_r,delta)
yl = feval(f,x_l);
yr = feval(f,x_r);
err = [];
if yl*yr > 0
disp('error'),0;
else
max1 = 1+round((log(x_r-x_l)-log(delta))/log(2));
for k = 1:max1
x_i = (x_l+x_r)/2;
yi = feval(f,x_i);
if yi == 0
x_l = x_i;
x_r = x_i;
elseif yr*yi > 0
x_r = x_i;
yr = yi;
else
x_l = x_i;
yl = yi;
end
err(k) = abs(x_r-x_l); %% here
% if x_r-x_l < delta, break,end
if err(k) < delta, break,end %% and here
end
end
x_i = (x_l+x_r)/2;
% err = abs(x_r-x_l); % remove here
yi = feval(f,x_i);
end
  1 commentaire
Willson Blesstian
Willson Blesstian le 9 Mar 2021
Oh wow, it was that simple. Thank you so much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB 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!

Translated by