Employ the NewtonRaphson method to determine a real root for
Solution
Main Program File :
% Solve a non-linear equation using Newton-Raphson method
 % Main Script File
 clc;
 clear all;
 close all;
 x = 4.5;
 x_old = 100;
 x_true = 4.512;
 x = nraphson( x_old,x_true,x );
 disp(\'The root is: \');
 disp(x);
% Plotting
 x = -10:0.01:10;
 f = 0.5*x.^3 - 4*x.^2 + 6*x -2;
 figure;
 plot(f)
 grid on;
Function file :
function [ x ] = nraphson( x_old,x_true,x )
 %Function for Newton-Raphson method
iter = 0;
 while abs(x_old-x) > 10^-3 && x ~= 0
 x_old = x;
 x = x - (0.5*x^3 - 4*x^2 + 6*x -2)/(1.5*x^2 - 8*x + 6);
 iter = iter + 1;
 fprintf(\'Iteration %d: x=%.20f, err=%.20f\ \', iter, x, x_true-x);
 end
end
Output :
If initital guess is 4.5 then real root of the given equation is 6.1563
If initital guess is 4.43 then real root of the given equation is 0.4746

