MATLAB Answers

Solving a Nonlinear Equation using Newton-Raphson Method

1,241 views (last 30 days)
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

  1 Comment

DEBADITYA GUPTA
DEBADITYA GUPTA on 29 Sep 2017
suppose I need to solve f(x)=a*x.^3+b*x.^2+c using Newton-Raphson method where a,b,c are to be import from excel file or user defined, the what i need to do?

Sign in to comment.

Accepted Answer

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 25 Nov 2013
Edited: Brewster on 25 Nov 2013
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

  4 Comments

Show 1 older comment
Bruno Pop-Stefanov
Bruno Pop-Stefanov on 25 Nov 2013
I edited my answer to display the value of x at each iteration and the error with the true value. pause stops the loop until the you press a key.
Hassan Mohamed
Hassan Mohamed on 25 Nov 2013
i'm sorry but, the value of error in incorrect Relative Approximate Ture Error = (True Error / True value) * 100
Skyler Kolb
Skyler Kolb on 23 Sep 2015
if your initial guess is
x = 0.05
but then late declare your initial guess as a new value
x_old = x
in your while loop, how is that going to work? Won't that defeat the purpose of having an initial guess?

Sign in to comment.

More Answers (6)

Pourya Alinezhad
Pourya Alinezhad on 25 Nov 2013
you can use the following line of code;
x = fzero(@(x)x.^3 - 0.165*x.^2 + 3.993*10.^-4,0.05)

  0 Comments

Sign in to comment.


Eduardo
Eduardo on 13 Apr 2014
Hello,
I am trying to make a program using the Newton-Raphson method for complex equations, but I am in a infinite loop.
If I change the expression,
x = x - (x^3 - 0.165*x^2 + 3.993*10^-4)/(3*x^2 - 0.33*x);
For this:
x = x - (sqrt(-x))/(-0.5.*(1./sqrt(-x)));
The algorithm does not work.
how can I use a complex equation in this case ??
Thank you very much.

  0 Comments

Sign in to comment.



Valdes Corleone
Valdes Corleone on 1 Sep 2018
Hi ! I want to solved an système of three equations non-linear with three variables ,discretised with finit differential and solved with newton raphson. Please can I have a code matlab

  0 Comments

Sign in to comment.


Sayeed
Sayeed on 21 Oct 2018
How can i Code it?
u=0.23 y=0.3 v=0.00001
u/u’=5*log(u’*y/v)-3.05
u’=?
I need to find the value of u’.

  1 Comment

madhan ravi
madhan ravi on 21 Oct 2018
Please ask a separate question question because it would make this thread unrelated

Sign in to comment.


Sayeed
Sayeed on 21 Oct 2018
Thanks, I did. Can u help with that?
https://se.mathworks.com/matlabcentral/answers/425183-code-for-equation-with-ln

  0 Comments

Sign in to comment.