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

2 ビュー (過去 30 日間)
Nicholas Deosaran
Nicholas Deosaran 2020 年 9 月 1 日
コメント済み: Star Strider 2020 年 9 月 1 日
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 件のコメント
Mario Malic
Mario Malic 2020 年 9 月 1 日
編集済み: Mario Malic 2020 年 9 月 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 件)

Star Strider
Star Strider 2020 年 9 月 1 日
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 件のコメント
Nicholas Deosaran
Nicholas Deosaran 2020 年 9 月 1 日
At the end L is to equall or be 5.2154
Star Strider
Star Strider 2020 年 9 月 1 日
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));

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by