Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
511 B
1
Indexable
Never
clc;

f = @(x) x^3 + 3*x + 2;
f_prime = @(x) 3*x^2 + 3;

x0 = 0;

tolerable_error = 0.0001;

% my input

nvalue = input('my number for iterations: ');

x = x0;

for iterations = 1:nvalue
    x_new = x - f(x) / f_prime(x);
    disp(x_new)
    if abs(x_new - x) < tolerable_error
        fprintf('root found at x = %f after %d iterations\n', x_new, iterations);
        break;
    end

    x = x_new;
end

if iterations >= nvalue
    disp('Newton-Raphson not convergen number.');
end
Leave a Comment