# HW1soln - Problem 1(a Mean function ret=mymean(a...

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

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

View Full Document

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: Problem 1: (a) Mean: function ret=mymean(a) ret=sum(a)/length(a); endfunction Mode: function ret=mymode (a) z=zeros(1, length(a)); for ii=1:length(a) for jj=1:length(a) if ((a(jj)-a(ii))==0) z(ii)=z(ii)+1; end end end [w,iw]=max(z); ret=a(iw); endfunction Median: function ret=mymedian(a) asort=sort(a); N=length(a); if ceil(N/2)==floor(N/2) ret=(asort(N/2)+asort(N/2+1))/2; else ret=asort(ceil(N/2)); end endfunction Variance: function ret=myvariance(a) ret= sum((a-mymean(a)).^2)/(length(a)-1); endfunction Standard Deviation: function ret=mystdev(a) ret=sqrt(myvariance(a)); endfunction a=[0.91595 0.35290 0.35692 0.18598 0.67537 0.92017 0.98268 0.44933 0.27089 0.81826]; mymean(a) ans = 0.59284 mymode(a) ans = 0.91595 mymedian(a) ans = 0.56235 mystdev(a) ans = 0.30261 myvariance(a) ans = 0.091573 a1000=rand(1000,1); mymean(a1000) ans = 0.50032 mymode(a1000) ans = 0.19736 mymedian(a1000) ans = 0.50370 mystdev(a1000) ans = 0.28774 myvariance(a1000) ans = 0.082792 (b) Generate random numbers from Gaussian PDF: function ret=mygaussian(xmean, xsigma) ret=; while (isempty(ret)) x=xmean+(0.5-rand)*8*xsigma; %4 sigma range if rand<(e^(-((x-xmean)^2)/(2*xsigma^2))) ret=x; end end endfunction Show that this works: for ii=1:1000 g(ii)=mygaussian(100,10); end hist(g) Demonstrate that the standard deviation of the mean tends to zero as the sample size increases: clear; xmean=100; xsigma=10; Ns=5:5:100; kk=1; for N=Ns jj=1; for samples=1:25 %num. of samples to calculate stdev from for ii=1:N a(ii)=mygaussian(xmean,xsigma);...
View Full Document

## This note was uploaded on 12/29/2011 for the course PHYSICS 375 taught by Professor Eno during the Spring '11 term at Maryland.

### Page1 / 8

HW1soln - Problem 1(a Mean function ret=mymean(a...

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

View Full Document
Ask a homework question - tutors are online