The question is: Write a newsqrt function to compute the square root of a positive number, using Newton’s method, using while... end. An upper limit on the number of iterations and an acceptable error value should be included in the input arguments. Allow for default values if the user decides not to give any or one of the input parameters.
I wrote the following code for this problem:
function sqrt_val = newsqrt(x, max_iter, error_tol)
if margin < 3
error_tol = 1e-6;
end
if margin < 2
max_iter = 1000;
end
rk = 1;
iter = 0;
while iter < max_iter
rk_next = 0.5 * (rk + x / rk);
if abs(rk_next - rk) < error_tol
break;
end
rk = rk_next;
iter = iter + 1;
end
sqrt_val = rk;
end
But it is showing me the following error:
arning: function name 'newsqrt' does not agree with function filename '/tmp/main.m'
warning: called from
/opt/run_user_code.m at line 1 column 1
error: 'x' undefined near line 17, column 17
error: called from
main at line 17 column 17
/opt/run_user_code.m at line 1 column 1
See this bug report or the octave docs, you should just be able to begin the script with another token.
The example they give is: