Advertisement
Guest User

Untitled

a guest
Jan 30th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 0.71 KB | None | 0 0
  1. function prova()
  2.     f = @(x) x^2-9
  3.     a = 0;   b = 10;
  4.     k = 24; % <==== CAMBIARE QUESTO
  5.     res = bisection(f, a, b, k);
  6.     fprintf('Soluzione: %f\n', res);
  7. end
  8.  
  9.  
  10. function res = bisection(f, a_i, b_i, k)
  11.   if f(a_i)*f(b_i) > 0
  12.       fprintf('Segni sbagliati');
  13.       res = (-1);
  14.   end
  15.   x_star = (a_i + b_i)/2.0;
  16.   iteration_counter = 1;
  17.   while iteration_counter < k
  18.       if f(a_i)*f(x_star) > 0
  19.         a_i = x_star;
  20.       else
  21.         b_i = x_star;
  22.       end
  23.       x_star = (a_i + b_i)/2;
  24.       iteration_counter = iteration_counter + 1;
  25.   end
  26.   fprintf("Errore %f\n", x_star - 3);   % LA SO ANALITICAMENTE
  27.   fprintf("Iterazioni max %f\n", iteration_counter);
  28.   res = x_star;
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement