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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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: &gt;&gt; [Sqrt,iterations]=SquareRoot(.5) Sqrt = 0.7071 iterations = 5 &gt;&gt; [Sqrt,iterations]=SquareRoot(25) Sqrt = 5.0000 iterations = 5 &gt;&gt; [Sqrt,iterations]=SquareRoot(17161) Sqrt = 131.0000 iterations = 9 &gt;&gt; [Sqrt,iterations]=SquareRoot(-10) ??? Error using ==&gt; SquareRoot at 8 Please enter a positive number...
View Full Document

Page1 / 2

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online