70
Monte Carlo Analysis Jake Blanchard University of Wisconsin - Madison Spring 2010

11 MonteCarlo

Embed Size (px)

DESCRIPTION

Monte Carlo curves

Citation preview

Fitting Curves to Data

Monte Carlo AnalysisJake BlanchardUniversity of Wisconsin - MadisonSpring 20101IntroductionMonte Carlo analysis is a common way to carry out uncertainty analysisThere are tools you can add in to Excel, but we will start by doing some of this on our own.I will use Matlab, but other tools work as well.2Monte Carlo AnalysisThe general idea is to sample the inputs, run a model, and thus get sampled outputWe can then look at averages, variances, probability distributions, etc., for the outputDecisions can then be made from these results3An example: diceHow can we simulate the rolling of a dice?If we could simulate such a thing, what questions could we answer?What is probability of rolling a 6?What is the probability of rolling snake eyes?What is the probability of two dice summing to 7?Simple Code for Diced=randperm(6)orn=1000000;roll1=ceil(6*rand(n,1));roll2=ceil(6*rand(n,1));tot=roll1+roll2;six=numel(find(roll1==6));prob=six/nsnakeeyes=numel(find(tot==2));prob=snakeeyes/nsevens=numel(find(tot==7));prob=sevens/nOther questionsWhat is accuracy as function of number of samples?Plot

Code for Error Plotclear alln=1000;ntries=100;roll1=ceil(6*rand(n,ntries));roll2=ceil(6*rand(n,ntries));tot=roll1+roll2;for i=1:ntries sevens=numel(find(tot(:,i)==7)); prob(i)=sevens/n;endmean(prob)std(prob)More Monte CarloMonte Carlo approaches are also applicable to other types of problems:Particle transportRandom walkNumerical integration (especially many-dimensional)9An ExampleRandom walkAssume path length per step is fixedRandomly sample angle at which step is takenRepeat many times and study resulting pathThis is not the only algorithm for random walk. Many limit to finite number of directions and vary length from jump to jump10Sampleclear allsteplen=1;startx=0;starty=0;nsteps=100;angle=2*pi*rand(nsteps,1);dx=steplen*cos(angle);dy=steplen*sin(angle);x(1)=startx;y(1)=starty;for i=2:nsteps x(i)=x(i-1)+dx(i-1); y(i)=y(i-1)+dy(i-1);endplot(x,y,0,0,'ro','LineWidth',2)114 Runs for 5 Steps Each

4 Runs for 50 Steps Each

Another ExampleNumerical integration (2-D, in this case)Draw area within a squareRandomly locate points within the squareCount up the number of points (N) within the areaArea=area of square*number points inside area/N14Finding the Area of a Circle

15Exampleclear allsquaresidelength=2;area=squaresidelength.^2;samples=100000;x=squaresidelength*(-0.5+rand(samples,1));y=squaresidelength*(-0.5+rand(samples,1));outside=floor(2*sqrt(x.^2+y.^2)/squaresidelength);circarea=(1-sum(outside)/samples)*area16Another ExampleIf we have 20 people in a room, what is the probability that at least two will have birthdays on the same day17Sourcenump=23;samples=10000;birthd=ceil(365*rand(nump,samples));count=0;for j=1:samples if numel(birthd(:,j))-numel(unique(birthd(:,j))) >0 count=count+1; endendprobab=count/samples;18Characteristics of Monte Carlo ApproachesWe have to sample enough times to get reasonable resultsAccuracy only increases like sqrt(N)Computation times are typically longDevelopment time is typically relatively shortThese are a trade-off19Our Case StudyConsider a cantilever beam of length L with a circular cross section of radius RThe deflection of such a beam, loaded at the end, is given by

LF20ParametersF varies from 600 N to 800 N (uniformly)R varies from 2 cm to 2.4 cm (uniformly)E varies from 185 to 200 GPa (uniformly)L varies from 1 m to 1.05 m (uniformly)What is average displacement?What does probability distribution look like?

21Uniform DistributionsMost codes produce random numbers (Rn) between 0 and 1 with uniform distributionsTo get a uniform distribution from a to b, you can use

22Normal DistributionsThese are like the well-known bell curveCodes often give normal distribution with mean of 0 and standard dev. of 1We can use the following formula to generate a normal distribution with mean of M and standard dev. of

23MatlabMatlab has several tools for random number generationRAND() produces matrices of uniform numbersRANDN() produces matrices of random numbers with normal distributions24Using MatlabPut random numbers in a vectorUse mean function

a=2b=7randnumbers=a+(b-a)*rand(5,1)mean(randnumbers)25Basic Analytical Functionsmeanstd standard deviationhist(v,n) gives histogram of set of numbers in vector v, using n bins26ExampleGenerate 1,000 random numbers uniformly distributed between 10 and 12 and calculate meanRepeat for 104, 105, and 106 samplesPlot histogram for last caseNote: previous code wasa=2b=7randnumbers=a+(b-a)*rand(5,1)mean(randnumbers)27Case StudyComplete case study for beam deflectionsDownload the file beam.m and adapt to find mean deflection and histogram of deflections

n=100;f=600+200*rand(n,1);r=0.02+0.004*rand(n,1);emod=(185+15*rand(n,1))*1e9;l=1+0.05*rand(n,1);inert=pi*r.^4/4;delta=f.*l.^3/3./emod./inert;mm=mean(delta);hist(delta,60)

28Result

Other Sampling ApproachesDistributionEquationLimitsSamplingUniforma