MatLab Problem Can Someone help me with this problem Thanks

MatLab Problem. Can Someone help me with this problem? Thanks

The \"divide and average\" method, an old-time method for approximating the squareroot of any positive number a, can be formulated as x = x + a/x/2 Write a well-structured M-file function based on the while ... break loop structure to implement this algorithm. Use proper indentation so that the structure is clear. At each step estimate the error in your approximation as epsilon = |X_new - X_old/X_new| Repeat the loop until epsilon is less than or equal to a specified value. Design your program so that it returns both the result and the error. Make sure that it can evaluate the squareroot of numbers that are equal to and less than zero. For the latter case, display the result as an imaginary number. For example, the squareroot of -4 would return 2i. Test your program by evaluating a = 0, 2, 10 and -4 for epsilon = 1 times 10^-4.

Solution

clc
clear all
format long

%%

a=2 ; % number whose sq. root desired
x0=1 ; % initial guess

tol=1e-4; % tolerance

while true
x=(x0+abs(a)/x0)/2;
esp=abs((x-x0)/x);
if esp<tol
break;
end
x0=x;
end

if a<0
x=complex(x);
end


fprintf(\'square root of %f is \',a)
x
fprintf(\'and tolerance is %f\ \', eps);

MatLab Problem. Can Someone help me with this problem? Thanks The \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site