Untitled
unknown
plain_text
10 months ago
726 B
4
Indexable
% x_n+1 = x_n - f(x_n)/f'(x_n)
% f(x) = x^3 + 2x^2 + x
% f'(x) = 3x^2 + 4x +1
f = @(x) x.^3 + 2*x.^2+x;
df = @(x) 3*x.^2 + 4*x + 1;
tol = 0.01;
maxiter = 100;
x0 = [-2, 1];
for i = 1:length(x0)
x=x0(i);
fprintf('\nPunto iniziale x0 = %f\n', x);
for iter = 1:maxiter
x_new = x-f(x)/df(x);
if abs(x_new - x) < tol
fprintf('Convergenza raggiunta in %d iterazione\n', iter);
fprintf('Radice trovata: %f\n', x_new)
break;
end
x = x_new;
end
end
x = linspace(-3, 2, 1000);
y = f(x);
figure;
plot(x,y,'b-', 'LineWidth', 2);
grid on;
hold on;
plot(x, zeros(size(x)), 'k--');
xlabel('x');
ylabel('f(x)');
title('Funzione f(x) = x^3 + 2x^2 + x');Editor is loading...
Leave a Comment