Solving a Nonlinear Equation using Newton-Raphson Method
조회 수: 1,270 (최근 30일)
이전 댓글 표시
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
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
Walter Roberson
2022년 12월 10일
You should open your own Question, after reading http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
채택된 답변
Bruno Pop-Stefanov
2013년 11월 25일
편집: MathWorks Support Team
2022년 9월 27일
fun = @(x)x^3 - 0.165*x^2 + 3.993e-4;
x_true = fzero(fun,[0.01 0.1],optimset("Display","iter"));
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
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
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.
추가 답변 (3개)
Dhruv Bhavsar
2020년 8월 28일
- 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
Munish Jindal
2023년 2월 13일
편집: Munish Jindal
2023년 2월 13일
Got this error.
unrecognized function or variable 'NewtonRaphson_nl_print'.
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
2013년 11월 25일
you can use the following line of code;
x = fzero(@(x)x.^3 - 0.165*x.^2 + 3.993*10.^-4,0.05)
댓글 수: 0
Mohamed Hakim
2021년 5월 21일
편집: Walter Roberson
2022년 2월 12일
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
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!