MATLAB Answers

Solving a Nonlinear Equation using Newton-Raphson Method

3 505 views (last 30 days)
Hassan Mohamed
Hassan Mohamed on 25 Nov 2013
Answered: Mohamed Hakim on 21 May 2021 at 12:20
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
  5 Comments

Sign in to comment.

More Answers (9)

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)

Dhruv Bhavsar
Dhruv Bhavsar on 28 Aug 2020
  1. Solve the system of non-linear equations.
x^2 + y^2 = 2z
x^2 + z^2 =1/3
x^2 + y^2 + z^2 = 1
using Newton’s method having tolerance = 10^(−5) and maximum iterations upto 20
%Function NewtonRaphson_nl() is given below.
fn = @(v) [v(1)^2+v(2)^2-2*v(3) ; v(1)^2+v(3)^2-(1/3);v(1)^2+v(2)^2+v(3)^2-1];
jacob_fn = @(v) [2*v(1) 2*v(2) -2 ; 2*v(1) 0 2*v(3) ; 2*v(1) 2*v(2) 2*v(3)];
error = 10^-5 ;
v = [1 ;1 ;0.1] ;
no_itr = 20 ;
[point,no_itr,error_out]=NewtonRaphson_nl(v,fn,jacob_fn,no_itr,error)
NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,error);
# OUTPUT.
Functions Below.
function [v1 , no_itr, norm1] = NewtonRaphson_nl(v,fn,jacob_fn,no_itr,error)
% nargin = no. of input arguments
if nargin <5 , no_itr = 20 ; end
if nargin <4 , error = 10^-5;no_itr = 20 ; end
if nargin <3 ,no_itr = 20;error = 10^-5; v = [1;1;1]; end
v1 = v;
fnv1 = feval(fn,v1);
i = 0;
while true
jacob_fnv1 = feval(jacob_fn,v1);
H = jacob_fnv1\fnv1;
v1 = v1 - H;
fnv1 = feval(fn,v1);
i = i + 1 ;
norm1 = norm(fnv1);
if i > no_itr && norm1 < error, break , end
%if norm(fnv1) < error , break , end
end
end
function [v1 , no_itr, norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,error)
v1 = v;
fnv1 = feval(fn,v1);
i = 0;
fprintf(' Iteration| x | y | z | Error | \n')
while true
norm1 = norm(fnv1);
fprintf('%10d |%10.4f| %10.4f | %10.4f| %10.4d |\n',i,v1(1),v1(2),v1(3),norm1)
jacob_fnv1 = feval(jacob_fn,v1);
H = jacob_fnv1\fnv1;
v1 = v1 - H;
fnv1 = feval(fn,v1);
i = i + 1 ;
norm1 = norm(fnv1);
if i > no_itr && norm1 < error, break , end
%if norm(fnv1) < error , break , end
end
end
This covers answer to your question and also queries for some comments I read in this thread.
  2 Comments
Upendra Kumar
Upendra Kumar on 13 Apr 2021
i need to solve 5 non linear equations with 5 unknowns in matlab so how i can write program for solving those equations

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.


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

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

ali raza
ali raza on 28 Mar 2021
function [v1 , no_itr, norm1] = NewtonRaphson_nl(v,fn,jacob_fn,no_itr,error) % nargin = no. of input arguments if nargin <5 , no_itr = 20 ; end if nargin <4 , error = 10^-5;no_itr = 20 ; end if nargin <3 ,no_itr = 20;error = 10^-5; v = [1;1;1]; end
v1 = v;
fnv1 = feval(fn,v1);
i = 0;
while true
jacob_fnv1 = feval(jacob_fn,v1);
H = jacob_fnv1\fnv1;
v1 = v1 - H;
fnv1 = feval(fn,v1);
i = i + 1 ;
norm1 = norm(fnv1);
if i > no_itr && norm1 < error, break , end
%if norm(fnv1) < error , break , end
end
end
function [v1 , no_itr, norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,error) v1 = v; fnv1 = feval(fn,v1); i = 0; fprintf(' Iteration| x | y | z | Error | \n') while true norm1 = norm(fnv1); fprintf('%10d %10.4f %10.4f | %10.4f| %10.4d |\n',i,v1(1),v1(2),v1(3),norm1) jacob_fnv1 = feval(jacob_fn,v1); H = jacob_fnv1\fnv1; v1 = v1 - H; fnv1 = feval(fn,v1); i = i + 1 ; norm1 = norm(fnv1); if i > no_itr && norm1 < error, break , end %if norm(fnv1) < error , break , end
end
end

Mohamed Hakim
Mohamed Hakim on 21 May 2021 at 12:20
function NewtonRaphsonMethod
%Implmentaton of Newton-Raphson method to determine a solution.
%to approximate solution to x = cos(x), we let f(x) = x - cos(x)
i = 1;
p0 = 0.5*pi; %initial conditions
N = 100; %maximum number of iterations
error = 0.0001; %precision required
syms 'x'
f(x) = x - cos(x); %function we are solving
df = diff(f); %differential of f(x)
while i <= N
p = p0 - (f(p0)/df(p0)); %Newton-Raphson method
if (abs(p - p0)/abs(p)) < error %stopping criterion when difference between iterations is below tolerance
fprintf('Solution is %f \n', double(p))
return
end
i = i + 1;
p0 = p; %update p0
end
fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', N, error) %error for non-convergence within N iterations
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by