# Specifying condition criteria for while loop iteration

5 views (last 30 days)
I want to create a script based on the Newton Raphson method that accepts starting guess values in the range ( 0 - 1 ) and gives result that fall within the range ( 0.0174533 rad - 1.0472 rad ). I managed to get a script to work, but it appears to be sensitive to the starting guess used and works for starting guess input ranging ( 0.6 - 1.3 ).
% current script works for x(1) = (0.6 : 1.3)
x = zeros( 100,1) ; % preallocation for x.
iter = 1 ; % define iteration counter
x(1) =1.1; % initial guess.
tol = 0.5 ; % initial guess for tolerance. valid as long as is greater than .00436 rad (i.e 0.25 degress)
while ( tol > 0.00436 ) % specify loop continuation prerequisites
% keep running loop while tolerance exceed .00436. (i.e >0.25 degrees)
fun = tan( pi/4+x(iter)/2 )^2 .* exp( (pi/3 + 4.*x(iter)).* tan(x(iter)) ) - 191 %[rad] function to be solved
df_fun = tan(x(iter)/2 + pi/4)^2*exp(tan(x(iter))*(4*x(iter) + pi/3))*(4*tan(x(iter)) + (tan(x(iter))^2 + 1)*(4*x(iter) + pi/3)) +...
2*tan(x(iter)/2 + pi/4)*exp(tan(x(iter))*(4*x(iter) + pi/3))*(tan(x(iter)/2 + pi/4)^2/2 + 1/2) %[rad] i.e result from symbolic diff(fun)
x(iter+1) = x(iter) - fun/ df_fun % General form of Newton Raphson method
tol = abs(x(iter+1)-x(iter))
iter = iter+1
end
This is fine if an idea of the correct result to expect is known. I figured my while loop condition criteria may be a reason for such behaviour, and tried a few variations but couldn't get it to work the way I want it to.
% Using this improved it a bit, but not exactly what I initially thought of making the script accept for the input range.
% works for x(1) = (0.6 : 1.5)
while ( tol > 0.00436 ) | (x(iter) <= 0 ) || (x(iter) > 1.0472)) % specify loop continuation prerequisites.
% or simply : while ( tol > 0.00436 | x(iter) > 1.0472)
end
Is it possible to make the script accept input range ( 0 -1 ) and give the desired output. Kindly assist with how to make the necessary changes to achieve this. I already have a solution that works , but was wondering how else it could be improved. I also noticed an issue with loop termination when Nan appeared in the output ( use 0.1 - 0.5 as starting guess in the posted script) . Any ideas on how to get the script to somehow continue in the right way after experiencing a NaN in the output.
##### 2 CommentsShowHide 1 older comment
Eric Geraldo De-Lima on 27 Jul 2021
I forgot to add the change to the script. Here it is.
% Alternative that works for what I want to do.
x = zeros( 1000,1) ; % preallocation for x.
iter = 1 ; % define iteration counter
x(1) =1.1; % initial guess.
tol = 0.5 ; % initial guess for tolerance. valid as long as is greater than .00436 rad (i.e 0.25 degress)
while tol > 0.00436 | ((x(iter) <= 0 ) || (x(iter) > 1.0472)) % specify loop continuation prerequisites
% keep running loop while tolerance exceed .00436. (i.e >0.25
% degrees). Also, check 1.0472 >= x(iter) >=0.
fun = tan( pi/4+x(iter)/2 )^2 .* exp( (pi/3 + 4.*x(iter)).* tan(x(iter)) ) - 191 %[rad] function to be solved
df_fun = tan(x(iter)/2 + pi/4)^2*exp(tan(x(iter))*(4*x(iter) + pi/3))*(4*tan(x(iter)) + (tan(x(iter))^2 + 1)*(4*x(iter) + pi/3)) +...
2*tan(x(iter)/2 + pi/4)*exp(tan(x(iter))*(4*x(iter) + pi/3))*(tan(x(iter)/2 + pi/4)^2/2 + 1/2) %[rad] i.e result from symbolic diff(fun)
x(iter+1) = x(iter) - fun/ df_fun % General form of Newton Raphson method
tol = abs(x(iter+1)-x(iter))
iter = iter+1
if x>1.6 | tol< 0.00001
x(iter) = x(iter)- 1.6
continue
else iter == 1000
Notice =msgbox(' 1.3 > x(1) > 0. If in range, increase value by 0.1 ','Check initial guess ','warn');
break
end
end
I tested the if conditions by using initial guess values that trigger the if statements, but it seems this part below doesn't work for some reason I haven't figured out yet. Any ideas would be appreciated.
% used initial guess value x(1)= 2 but this part didn't seem to get
% triggered.
if x>1.6 | tol< 0.00001
x(iter) = x(iter)- 1.6
continue

darova on 26 Jul 2021
Did you try to assume the sign of the function value? Derivative can be positive and negative regardless of the value Maybe something like this: Eric Geraldo De-Lima on 29 Jul 2021
'Derivative can be positive and negative regardless of the value,' thanks for pointing that out. I didn't think about that before. However, that doesn't seem to affect my results. The script as it is produces the acceptable result. I am wondering if it can be modified to accept initial guess values in the range 0 - 1 and still give the acceptable result.
Initial guess values in the range 0 - 0.5 results in inf / NaN. I figured a message dialogue can be added to prompt a user of the acceptable input (which works perfectly). I also considered adding an 'if' condition that restarts the loop, without further user input, by altering the intial guess value when x(iter) = inf or Nan, or when x(iter) > 1.6.
That's what I attempted to do , but it doesn't seem to be working right.

R2019a

### Community Treasure Hunt

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

Start Hunting!