Help, How do I stop my iteration from going when it gets the answer?

2 visualizaciones (últimos 30 días)
Nicholas Deosaran
Nicholas Deosaran el 1 de Sept. de 2020
Comentada: Star Strider el 1 de Sept. de 2020
Hello good day, below is my code and I am using Netwons method of iteration.
I have the code typed out, but I dont know how to stop the code when it gets its answer...it keeps running with the answer 25.1327 over and over.
Thank you
function c = newton(f,fd,x0,tol)
%Given information
g = 9.81; % Gravity
h = 1; % Water depth in meters
H = .2; % Wave height in meters
T = 2; % Wave period in seconds
f = @(L) ((L-(g/(2*pi)))*(T^2)*(tanh((2*pi*h)/L))); % Linear dispersion
fd = @(L) 1+(((g*h*T^2)/(L^2))*sech((2*pi*h)/(L).^2)); % First derivative of Linear dispersion
tol = 0.0001 % how much tollarance
x0 = 0.001 % inital guess
L =x0;
y = f(L);
while abs (y) > tol % While loop to do the iterative method
L =L - y/fd(L); % Newtons iterative method.
y = f(L)% function of Linear dispersion
end
end
  1 comentario
Mario Malic
Mario Malic el 1 de Sept. de 2020
Editada: Mario Malic el 1 de Sept. de 2020
while abs (y) > tol % While loop to do the iterative method
L =L - y/fd(L); % Newtons iterative method.
y = f(L)% function of Linear dispersion
end
Loop does what you tell it to do. Is abs(y) bigger than tol? According to your result, 25.1327 is much higher than tol obviously. Something's missing in your code/implementation of iterative method, and maybe it's L that does not get updated.

Iniciar sesión para comentar.

Respuestas (2)

Star Strider
Star Strider el 1 de Sept. de 2020
Put in a condition that compares subsequent results with ‘tol’. Change the loop to something like this:
yprev = Inf;
yd = Inf;
while abs (yd) > tol % While loop to do the iterative method
L =L - y/fd(L); % Newtons iterative method.
y = f(L)% function of Linear dispersion
yd = y-yprev;
yprev = y;
end
so the full code is then:
%Given information
g = 9.81; % Gravity
h = 1; % Water depth in meters
H = .2; % Wave height in meters
T = 2; % Wave period in seconds
f = @(L) ((L-(g/(2*pi)))*(T^2)*(tanh((2*pi*h)/L))); % Linear dispersion
fd = @(L) 1+(((g*h*T^2)/(L^2))*sech((2*pi*h)/(L).^2)); % First derivative of Linear dispersion
tol = 0.0001 % how much tollarance
x0 = 0.001 % inital guess
L =x0;
y = f(L);
yprev = Inf;
yd = Inf;
while abs (yd) > tol % While loop to do the iterative method
L =L - y/fd(L); % Newtons iterative method.
y = f(L)% function of Linear dispersion
yd = y-yprev;
yprev = y;
end
This worked when I ran it. Check to be certain it gives the correct result.
Note that ‘c’ is never calculated. It would likely be best for your function to return the actual result you want.
  6 comentarios
Nicholas Deosaran
Nicholas Deosaran el 1 de Sept. de 2020
At the end L is to equall or be 5.2154
Star Strider
Star Strider el 1 de Sept. de 2020
Nothing with respect to ‘L’ was ever stated.
I have absolutely no idea what you are doing.
I would change the loop to something like this, storing the value of ‘L’ in each iteration:
yprev = Inf;
yd = Inf;
k = 1;
while abs (yd) > tol % While loop to do the iterative method
L = L - y/fd(L); % Newtons iterative method.
y = f(L); % function of Linear dispersion
yd = y-yprev;
yprev = y;
Lv(k) = L;
k = k + 1;
end
Lv
Note that ‘L’ quickly becomes negative. I leave the rest to you, since this appears to be homework.

Iniciar sesión para comentar.


David Hill
David Hill el 1 de Sept. de 2020
If you graph the function you will see the problem at L=0
g = 9.81;
h = 1;
T = 2;
f=@(L) L-g/(2*pi)*T^2*tanh(2*pi*h./L);
L=-.0001:.000000001:.0001;
plot(L,f(L));

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by