Help, How do I stop my iteration from going when it gets the answer?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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
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
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));
댓글 수: 0
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!