Please use MATLAB CODE and show all steps. Thanks The answer is lengthy, so don’
ID: 3599184 • Letter: P
Question
Please use MATLAB CODE and show all steps. ThanksThe answer is lengthy, so don’t upload just 3 lines of code. We are going to write a script that estimates the square root. The square root can be estimated using Newton's method which finds the zeros of a function from the following formula: 3. f(xo) Where xo is an initial guess for the zero of the function f (xo). To find the square root of 5 we would choose the function f(x)-5-12, when f(x) = 0, x = So our equation becomes Write a script that asks for an initial guess using the input () statement and then calculates the square root of 10. If f(xo) is within epsilon of 0, we will declare success and report the value of x, otherwise we will display an error message. epsilon is a variable that you will define at the beginning of the script. Pick some small value like 0.001.
Explanation / Answer
x = input("enter intial guess");
Tol = 0.0000001;
count = 0;
dx=1; %this is a fake value so that the while loop will execute
f=5-x*x;
fprintf('step x dx f(x) ')
fprintf('---- ----------- --------- ---------- ')
fprintf('%3i %12.8f %12.8f %12.8f ',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed
count = count + 1;
fprime = -2*x;
xnew = x - (f/fprime); % compute the new value of x
dx=abs(x-xnew); % compute how much x has changed since last step
x = xnew;
f = 5-x^2; % compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f ',count,x,dx,f)
end