Finding the roots of equation by newton-raphson method?

31 vues (au cours des 30 derniers jours)
Asecern
Asecern le 28 Avr 2019
Modifié(e) : Jim Riggs le 4 Mai 2019
According to the question that I attached, I am trying to solve the equation by newton-ralphson method. I have created the following structure, but I have few questions.
how will I decide to first x0 value? I really did not understand the logic of it.
and what should I do for the "ydx" line ? Should I evaluate the derivative by myself?
should I write all the operations with the '.' like '.^'?
and lastly is this the right structure for this question?
please do not use unknown commands, and stay in 'newton-ralphson' method.
thank you so much!
x0=2
y=(1-x)*sqrt(x+3)/(x*sqrt(x+1))-3.06;
tol=1e-4;
i=1
while y>tol
yx=(1-x0)*sqrt(x0+3)/(x0*sqrt(x0+1))-3.06;
ydx=
x1=x0-(yx/ydx)
y=(1-x1)*sqrt(x1+3)/(x1*sqrt(x1+1))-3.06;
x0=1
i=i+1
end
x0
i
y

Réponse acceptée

Jim Riggs
Jim Riggs le 29 Avr 2019
Modifié(e) : Jim Riggs le 29 Avr 2019
I would approach the problem like this (this is a very simple implementation)
Define an annonymous function for your equation:
func = @(x) (1-x).* (3+x).^.5 ./(x.*(1+x).^.5)
(I have used the .*, .^ and ./ so it will work on vector inputs,)
Now you should generate a plot of the function so that you understand it's shape and approximately where the root(s) are.
x = 0.01:0.01:1; % plotting the function from near-zero to 1 (function is undefined at x=0)
y = func(x);
figure;
plot(x,y,'r')
grid on;
Zoom in on the figure, and you can see that the root you are seeking (F(x) = 3.06) is between 0.3 and 0.4
Now write a search loop to locate the root numerically, using the Newton-Raphson method. (I will use a numerical approximation to the function derivative) The approximation for the function derivative is done as:
smallstep = 0.001;
dydx = (func(x+smallstep) - fun(x))/smallstep; % delta y over delta x
Use this equation to approximate the function derivative at x.
Putting it all together:
func = @(x) (1-x).*(3+x).^.5 ./ (x.*(1+x).^.5);
smallstep = 0.001; % this is a small x-step used to approximate the function derivative
Rootval = 3.06; % This is the function value that you seek
ytol = 1e-4; % this is the convergence tolerance
xguess = 0.3; % this is the initial guess for the location of the root
% set the starting function value to the initial guess, and compute the initial error
x=xguess;
yerr = Rootval-func(x);
while abs(yerr)>ytol
dydx = (func(x+smallstep)-func(x))/smallstep;
dx = yerr/dydx;
x=x+dx;
yerr = Rootval - func(x)
end
  2 commentaires
Asecern
Asecern le 4 Mai 2019
thanks for the answer but I did not understand why you did put two dots for the operations ?
'.^.' instead of '.^'?
how will I decide the correct way of writing functions?
And how would be the structure if we use the iteration method? like:
i=1
while y>tol
......
i=i+1
end
because it will be more compherensible to me to see the process of finding roots.
by the way, can I use the diff command to find the derivative?
thanks!
Jim Riggs
Jim Riggs le 4 Mai 2019
Modifié(e) : Jim Riggs le 4 Mai 2019
"I did not understand why you did put two dots for the operations ? '.^.' instead of '.^'?"
The second "." belongs to the number that follows it: ".5" it's exponentiation (.^) of one half (.5)
As for your second question, this is an itterative method (the "while abs(yerr)>ytol" is a loop - it continues until abs(yerr) is smaller than ytol). The only thing that the i variable accomplishes in your loop is it counts the number of itterations. You can add that to my loop also if you like. But if you code it the way I have shown (with no semicolon after the yerr calculation) it will display the yerr value each pass through the loop. This allows you to watch the answer converge. You can see the rate of convergence, whether or not it changes sign, and how many steps it takes.
One other comment; You want the value to converge within +/- tol, therefore, you have to use the absolute value of the Y-error { abs(yerr) } , because if the error is negative, it will pass the Y<tol test, even if the error is very large.
Also, I use the notation "yerr" in stead of "y", because it is not the Y value of the function, but the difference between the function value and the root you are seeking. This is simply a matter of preference, intended to clarify what I am doing. I think of "X" and "Y" as belonging to the function, and so I define the Y-error term, yerr, to be the difference between the root value and the function value (Root - Y).
Also note that you can define yerr either as (Root-Y) or (Y-Root), the differenc will be the sign of the error, and this affects whether you add or subtract the dx (i.e. "delta-x") correction.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by