Solving a Nonlinear Equation using Newton-Raphson Method

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

4 件のコメント

DEBADITYA GUPTA
DEBADITYA GUPTA 2017 年 9 月 29 日
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?
Syed nisar Abbas
Syed nisar Abbas 2021 年 7 月 5 日
Write a code in matlab of newton rephson method to solve cos(x)+2sin(x)+x^2 any one can answer me quickly
Rajesh
Rajesh 2022 年 12 月 10 日
to solve the given equation (x^2-1)/(x-1) taking minimum values for x and draw the stem graph and step graph

サインインしてコメントする。

 採用された回答

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2013 年 11 月 25 日
編集済み: MathWorks Support Team 2022 年 9 月 27 日

16 投票

The following code implements the Newton-Raphson method for your problem:
fun = @(x)x^3 - 0.165*x^2 + 3.993e-4;
x_true = fzero(fun,[0.01 0.1],optimset("Display","iter"));
Func-count x f(x) Procedure 2 0.1 -0.0002507 initial 3 0.0644397 -1.82743e-05 interpolation 4 0.0617709 5.41455e-06 interpolation 5 0.0623809 -2.96286e-08 interpolation 6 0.0623776 -4.33628e-11 interpolation 7 0.0623776 1.0842e-17 interpolation 8 0.0623776 0 interpolation Zero found in the interval [0.01, 0.1]
x = 0.1;
x_old = 100;
iter = 0;
while abs(x_old-x) > 1e-10 && iter <= 10 % x ~= 0
x_old = x;
x = x - (x^3 - 0.165*x^2 + 3.993e-4)/(3*x^2 - 0.33*x);
iter = iter + 1;
fprintf('Iteration %d: x=%.18f, err=%.18f\n', iter, x, x_true-x);
pause(1);
end
Iteration 1: x=0.016433333333333161, err=0.045944248180416342 Iteration 2: x=0.094298416648742320, err=-0.031920835134992817 Iteration 3: x=0.042655687778369436, err=0.019721893735380067 Iteration 4: x=0.063158887832661437, err=-0.000781306318911934 Iteration 5: x=0.062375951756182137, err=0.000001629757567366 Iteration 6: x=0.062377581507153931, err=0.000000000006595571 Iteration 7: x=0.062377581513749496, err=0.000000000000000007
You can plot the function with, for example:
x = linspace(0,0.1);
f = x.^3 - 0.165*x.^2 + 3.993*10^-4;
figure;
plot(x,f,'b',x,zeros(size(x)),'r--')
grid on

6 件のコメント

Hassan Mohamed
Hassan Mohamed 2013 年 11 月 25 日
If possible, i want the code to be with steps and iterations, and if possible calculate the error also, please. thnx so much for helping
Bruno Pop-Stefanov
Bruno Pop-Stefanov 2013 年 11 月 25 日
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 2013 年 11 月 25 日
編集済み: Hassan Mohamed 2013 年 11 月 25 日
i'm sorry but, the value of error in incorrect Relative Approximate Ture Error = (True Error / True value) * 100
Skyler Kolb
Skyler Kolb 2015 年 9 月 23 日
編集済み: Skyler Kolb 2015 年 9 月 23 日
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?
Brian Reinhard
Brian Reinhard 2021 年 3 月 15 日
ajarin master
Cesar Castro
Cesar Castro 2021 年 9 月 21 日
It did not work , May you help me, please? My F = x(1)^4+x(2)^4+2*x(1)^2*x(2)^2-4*x(1)+3 I Know the root is 1.
I do not know why, it did not work. Thanks in advance.

サインインしてコメントする。

その他の回答 (4 件)

Dhruv Bhavsar
Dhruv Bhavsar 2020 年 8 月 28 日

7 投票

  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.

4 件のコメント

Juan Sebastian Castro Barrero
Juan Sebastian Castro Barrero 2021 年 2 月 12 日
i need make a program in matlab to solve
F(1)=1+(x(1)^2)-(x(2)^2)+((exp(x(1)))*cos(x(2)));
F(2)=(2*(x(1))*(x(2)))+((exp(x(1)))*sin(x(2)));
starting initials (-1,4)
5 iterations.
can you help me?
Upendra Kumar
Upendra Kumar 2021 年 4 月 13 日
i need to solve 5 non linear equations with 5 unknowns in matlab so how i can write program for solving those equations
Munish Jindal
Munish Jindal 2023 年 2 月 13 日
編集済み: Munish Jindal 2023 年 2 月 13 日
Got this error.
unrecognized function or variable 'NewtonRaphson_nl_print'.
Walter Roberson
Walter Roberson 2023 年 2 月 13 日
the code is given above starting at the line
function [v1 , no_itr, norm1] = NewtonRaphson_nl_print(v,fn,jacob_fn,no_itr,error)

サインインしてコメントする。

Pourya Alinezhad
Pourya Alinezhad 2013 年 11 月 25 日

3 投票

you can use the following line of code;
x = fzero(@(x)x.^3 - 0.165*x.^2 + 3.993*10.^-4,0.05)
Mohamed Hakim
Mohamed Hakim 2021 年 5 月 21 日
編集済み: Walter Roberson 2022 年 2 月 12 日

3 投票

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

3 件のコメント

Taha Anum
Taha Anum 2023 年 10 月 22 日
can i have a generic code which may ask user for the equation
Vicki
Vicki 2026 年 1 月 6 日
What is the role of the "syms" function here?
Torsten
Torsten 2026 年 1 月 6 日
By using x as a symbolic variable, f is a symbolic function. The "diff" command can analytically differentiate symbolic functions. Thus if you are not able to compute the derivative of x-cos(x), symbolic math and the diff command will do it for you.
Equivalently, you could just set
f = @(x) x-cos(x);
df = @(x)1+sin(x);
without the
syms x
line.

サインインしてコメントする。

likhith krishna
likhith krishna 2025 年 11 月 16 日

0 投票

function [y, exitflag, iter_count] = newton(R, J, x0, tol, maxit)
y = x0;
iter_count = 0;
exitflag = 0;
R_val = R(y);
if norm(R_val, 2) < tol
exitflag = 1;
return;
end
for k = 1:maxit-1
J_val = J(y);
if rcond(J_val) < 1e-12
exitflag = -1;
iter_count = k - 1;
return;
end
dx = -J_val \ R_val;
y = y + dx;
iter_count = k;
R_val = R(y);
if norm(R_val, 2) < tol
exitflag = 1;
return;
end
end
exitflag = 0;
iter_count = maxit;
end

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by