# HW8soln - n = n 1 term =-1)^n(2*n 1 sum = sum term error =...

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

Problem 1 function out = sailor_pier(X,Y,n) success = 0; for index = 1:n y = 0; x = 0; while ( y <= Y & x <= X ) step = rand; if ( step <= 0.5 ) x = x + 1; elseif ( step <= .75 ) y = y + 1; else y = y - 1; end end % A walk completed. Check location if x > X success= success + 1; end end out = success/n; >> y = sailor( 100,20,1000 ) y = 0.9160 >> y = sailor( 75,30,1000 ) y = 1 >> y = sailor( 100,30,1000 ) y = 0.9950

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

View Full Document
Problem 2 function out = piest( frac ) %piest( frac ) %A function to estimate pi to a precision %of frac relative error sum = 1; % the first term n = 0; error = 1+frac; % insures the next stmt. is true while ( error > frac )
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: n = n+1; term = (-1)^n/(2*n+1); sum = sum + term; error = abs(term/sum); end out = 4*sum; >> piest(0.001) ans = 3.1432 >> piest( 1e-8 ) ans = 3.1416 Problem 3 function out = mdet3( A ) y = size( A ); if ( y(1) == 3 & y(2) == 3 ) d1 = A(1,1)*A(2,2)*A(3,3)+A(1,2)*A(2,3)*A(3,1)+A(1,3)*A(2,1)*A(3,2); d2 = A(1,3)*A(2,2)*A(3,1)+A(1,1)*A(2,3)*A(3,2)+A(1,2)*A(2,1)*A(3,3); out = d1-d2; else error( 'Matrix not 3X3' ); end >> A = [ 0.333 0.456 -0.432 ; 0.584 0.731 0.222 ; -0.363 0.479 0.352 ] A = 0.3330 0.4560 -0.4320 0.5840 0.7310 0.2220 -0.3630 0.4790 0.3520 >> y = mdet3( A ) y = -0.3157 >> y = mdet3( [ 4 3 2 ; 5 4 3 ; 3 4 5 ] ) y = 0...
View Full Document

## This note was uploaded on 09/01/2011 for the course PHY 303 taught by Professor Erskine/tsoi during the Spring '08 term at University of Texas.

### Page1 / 3

HW8soln - n = n 1 term =-1)^n(2*n 1 sum = sum term error =...

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

View Full Document
Ask a homework question - tutors are online