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

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

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.

댓글을 달려면 로그인하십시오.

답변 (2개)

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

Thank you, that was able to stop the code.
But is there a way to just get one answer ?
My pleasure!
I am not certain what you are asking. The result is returned in ‘y’, so the output of your function must be ‘y’, not ‘c’ (that is never calculated).
In my while-loop, I want to compare the difference of the wavelength (L) between two iterations.
How would I do that?
Thank you.
At the end L is to equall or be 5.2154
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
David Hill 2020년 9월 1일
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));

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2020년 9월 1일

댓글:

2020년 9월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by