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

1 view (last 30 days)
Nicholas Deosaran on 1 Sep 2020
Commented: Star Strider on 1 Sep 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
Mario Malic on 1 Sep 2020
Edited: Mario Malic on 1 Sep 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.

Star Strider on 1 Sep 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.
Star Strider on 1 Sep 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.

David Hill on 1 Sep 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));

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by