4_Quarterman_Patrick_HW09B

# 4_Quarterman_Patrick_HW09B - er=100*abs(x(i 1 x(i/abs(x(i 1...

This preview shows pages 1–2. Sign up to view the full content.

Patrick Quarterman Section 4 HW09B Script: function [Sqrt,iterations]=SquareRoot(a) %Patrick Quarterman Section 4 %Estimate the square root of a number using divide and average method %Inputs: any number %outputs estimated squre root if a<0 %test to make sure number is positive error( 'Please enter a positive number' ) else guess = a/2; %set starting values for while loop i=1; er=1; x(i)=guess; n=0; while er > 0.1 x(i+1)=(x(i)+a/x(i))/2; %square root estiamtion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: er=100*abs(x(i+1) - x(i))/abs(x(i+1)); i=i+1; n=n+1; end Sqrt = x(end); iterations = n; end Commands: >> [Sqrt,iterations]=SquareRoot(.5) Sqrt = 0.7071 iterations = 5 >> [Sqrt,iterations]=SquareRoot(25) Sqrt = 5.0000 iterations = 5 >> [Sqrt,iterations]=SquareRoot(17161) Sqrt = 131.0000 iterations = 9 >> [Sqrt,iterations]=SquareRoot(-10) ??? Error using ==> SquareRoot at 8 Please enter a positive number...
View Full Document

## This note was uploaded on 11/16/2010 for the course EGR 102 taught by Professor Hinds during the Spring '09 term at Michigan State University.

### Page1 / 2

4_Quarterman_Patrick_HW09B - er=100*abs(x(i 1 x(i/abs(x(i 1...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online