MATLAB: Solving a Nonlinear Equation using Newton-Raphson Method

newton raphson

It's required to solve that equation: f(x) = x.^3 – 0.165*x.^2 + 3.993*10.^-4 using Newton-Raphson Method with initial guess (x0 = 0.05) to 3 iterations and also, plot that function.
Please help me with the code (i have MATLAB R2010a) … I want the code to be with steps and iterations and if possible calculate the error also, please

Best Answer

The following code implements the Newton-Raphson method for your problem:
x = 0.05;
x_old = 100;
x_true = 0.0623776;
iter = 0;
while abs(x_old-x) > 10^-3 && x ~= 0
x_old = x;
x = x - (x^3 - 0.165*x^2 + 3.993*10^-4)/(3*x^2 - 0.33*x);
iter = iter + 1;
fprintf('Iteration %d: x=%.20f, err=%.20f\n', iter, x, x_true-x);
pause;
end
You can plot the function with, for example:
x = -10:0.01:10;
f = x.^3 - 0.165*x.^2 + 3.993*10^-4;
figure;
plot(f)
grid on