Mathematical Modeling--- Some functions used in Monte Carlo simulation

Simple thinking

Monte Carlo simulation is a simulation project implemented thousands of times on a computer, and the input value is randomly selected for each input. Since each input is often an estimation interval in itself, the computer model will randomly select any value in the interval for each input, and finally obtain a cumulative probability distribution through a large number of thousands or even millions of simulations. Picture, this is Monte Carlo simulation.

definition

Insert picture description here

Matlab

Matlab generates random numbers

  1. rand(m,n)The function generates a matrix (or called an array) with m rows and n columns consisting of random numbers uniformly distributed between [0,1].
  2. A matrix of m rows and n columns composed of uniformly distributed random numbers between [a,b]
  • a + rand(m,n)*(b-a)
  • unifrnd(a,b,m,n)
  1. Randomly extract an integer matrix of size m*n from [a,b]
  • randi([a,b],m,n)
  1. Generate a normal distribution (the MU parameter represents the mean, and the SIGMA parameter representsStandard deviation, The square root of the variance is the standard deviation) random number
  • normrnd(MU,SIGMA)
  1. Generate aExponential distribution with mean MRandom number (the corresponding parameter is λ = 1 / M \lambda=1/Mλ=1/M)
  • exprnd(M)
  1. Eliminate duplicate values ​​of a matrix or vectorAnd arrange the results in ascending order
  • unique()
unique([1 2 5; 6 8 9;2 4 6]) 

result

1
2
4
5
6
8
9
  1. Generate a random sequence of 1-m
randperm(m)

Record running time

  • ticFunctions and tocfunctions can be used to return the running time of the code.
    For example, if we want to calculate the running time of a piece of code, we can add ticit before and after this code.toc

Display long number format

Tell the result of Matlab calculation to display as long number format

format long g

Line segment connecting points

plot([1,2,6],[5,10,15],'-o') % 两个数组分别记录点的横纵坐标,依次画出线段 
text(2,10,'文本') % 在坐标(1,5)处标上文本

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43779658/article/details/108078254