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 ).
x = zeros( 100,1) ;
iter = 1 ;
tol = 0.5 ;
while ( tol > 0.00436 )
fun = tan( pi/4+x(iter)/2 )^2 .* exp( (pi/3 + 4.*x(iter)).* tan(x(iter)) ) - 191
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)
x(iter+1) = x(iter) - fun/ df_fun
tol = abs(x(iter+1)-x(iter))
iter = iter+1
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.
while ( tol > 0.00436 ) | (x(iter) <= 0 ) || (x(iter) > 1.0472))
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.