Find the root of the following equation using the Newton-Raphson method. Choose the
initial guess as 𝑥
(0) = 10. Choose the error tolerance as tol=1e-6. In other words, the
iterations should be stopped when the error
i. Please report the value of x after 2 iterations
ii. Please report the converged solution, where error is less than 1e-6.
iii. Please report the number of iterations required to reach this converged solution
clc;
clear all;
close all;
% function
f = @(x) x^2.5 + 23*x^1.5 -50*x + 1150;
% derivative
f_prime = @(x) 2.5*x^1.5 + 23*1.5*x^0.5 - 50;
% function for next iteration according to the Newton-Raphson method
g = @(x) x - f(x) / f_prime(x);
% tolerance
tol = 1e-6;
x_old = 10; % previous value of x
x = [x_old]; % array to store x after each iteration
x_new = g(x_old); % new value of x
ii = 0;
while abs(x_new - x_old) > tol
x_old = x_new;
x_new = g(x_old);
x = [x, x_new]; % append array with current iteration
end
fprintf('The value of x after 2 iterations: %f%+fj\n', real(x(3)), imag(x(3)))
fprintf('The converged solution: %f%+fj\n', real(f(x(end))), imag(f(x(end))))
fprintf('The number of iterations required to reach this converged solution: %i\n', length(x)-1)
Comments
Leave a comment