Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function prova()
- f = @(x) x^2-9
- a = 0; b = 10;
- k = 24; % <==== CAMBIARE QUESTO
- res = bisection(f, a, b, k);
- fprintf('Soluzione: %f\n', res);
- end
- function res = bisection(f, a_i, b_i, k)
- if f(a_i)*f(b_i) > 0
- fprintf('Segni sbagliati');
- res = (-1);
- end
- x_star = (a_i + b_i)/2.0;
- iteration_counter = 1;
- while iteration_counter < k
- if f(a_i)*f(x_star) > 0
- a_i = x_star;
- else
- b_i = x_star;
- end
- x_star = (a_i + b_i)/2;
- iteration_counter = iteration_counter + 1;
- end
- fprintf("Errore %f\n", x_star - 3); % LA SO ANALITICAMENTE
- fprintf("Iterazioni max %f\n", iteration_counter);
- res = x_star;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement