Dragon Notes

i

\( \newcommand{bvec}[1]{\overrightarrow{\boldsymbol{#1}}} \newcommand{bnvec}[1]{\overrightarrow{\boldsymbol{\mathrm{#1}}}} \newcommand{uvec}[1]{\widehat{\boldsymbol{#1}}} \newcommand{vec}[1]{\overrightarrow{#1}} \newcommand{\parallelsum}{\mathbin{\|}} \) \( \newcommand{s}[1]{\small{#1}} \newcommand{t}[1]{\text{#1}} \newcommand{tb}[1]{\textbf{#1}} \newcommand{ns}[1]{\normalsize{#1}} \newcommand{ss}[1]{\scriptsize{#1}} \newcommand{vpl}[]{\vphantom{\large{\int^{\int}}}} \newcommand{vplup}[]{\vphantom{A^{A^{A^A}}}} \newcommand{vplLup}[]{\vphantom{A^{A^{A^{A{^A{^A}}}}}}} \newcommand{vpLup}[]{\vphantom{A^{A^{A^{A^{A^{A^{A^A}}}}}}}} \newcommand{up}[]{\vplup} \newcommand{Up}[]{\vplLup} \newcommand{Uup}[]{\vpLup} \newcommand{vpL}[]{\vphantom{\Large{\int^{\int}}}} \newcommand{lrg}[1]{\class{lrg}{#1}} \newcommand{sml}[1]{\class{sml}{#1}} \newcommand{qq}[2]{{#1}_{\t{#2}}} \newcommand{ts}[2]{\t{#1}_{\t{#2}}} \) \( \newcommand{ds}[]{\displaystyle} \newcommand{dsup}[]{\displaystyle\vplup} \newcommand{u}[1]{\underline{#1}} \newcommand{tu}[1]{\underline{\text{#1}}} \newcommand{tbu}[1]{\underline{\bf{\text{#1}}}} \newcommand{bxred}[1]{\class{bxred}{#1}} \newcommand{Bxred}[1]{\class{bxred2}{#1}} \newcommand{lrpar}[1]{\left({#1}\right)} \newcommand{lrbra}[1]{\left[{#1}\right]} \newcommand{lrabs}[1]{\left|{#1}\right|} \newcommand{bnlr}[2]{\bn{#1}\left(\bn{#2}\right)} \newcommand{nblr}[2]{\bn{#1}(\bn{#2})} \newcommand{real}[1]{\Ree\{{#1}\}} \newcommand{Real}[1]{\Ree\left\{{#1}\right\}} \newcommand{abss}[1]{\|{#1}\|} \newcommand{umin}[1]{\underset{{#1}}{\t{min}}} \newcommand{umax}[1]{\underset{{#1}}{\t{max}}} \newcommand{und}[2]{\underset{{#1}}{{#2}}} \) \( \newcommand{bn}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{bns}[2]{\bn{#1}_{\t{#2}}} \newcommand{b}[1]{\boldsymbol{#1}} \newcommand{bb}[1]{[\bn{#1}]} \) \( \newcommand{abs}[1]{\left|{#1}\right|} \newcommand{ra}[]{\rightarrow} \newcommand{Ra}[]{\Rightarrow} \newcommand{Lra}[]{\Leftrightarrow} \newcommand{rai}[]{\rightarrow\infty} \newcommand{ub}[2]{\underbrace{{#1}}_{#2}} \newcommand{ob}[2]{\overbrace{{#1}}^{#2}} \newcommand{lfrac}[2]{\large{\frac{#1}{#2}}\normalsize{}} \newcommand{sfrac}[2]{\small{\frac{#1}{#2}}\normalsize{}} \newcommand{Cos}[1]{\cos{\left({#1}\right)}} \newcommand{Sin}[1]{\sin{\left({#1}\right)}} \newcommand{Frac}[2]{\left({\frac{#1}{#2}}\right)} \newcommand{LFrac}[2]{\large{{\left({\frac{#1}{#2}}\right)}}\normalsize{}} \newcommand{Sinf}[2]{\sin{\left(\frac{#1}{#2}\right)}} \newcommand{Cosf}[2]{\cos{\left(\frac{#1}{#2}\right)}} \newcommand{atan}[1]{\tan^{-1}({#1})} \newcommand{Atan}[1]{\tan^{-1}\left({#1}\right)} \newcommand{intlim}[2]{\int\limits_{#1}^{#2}} \newcommand{lmt}[2]{\lim_{{#1}\rightarrow{#2}}} \newcommand{ilim}[1]{\lim_{{#1}\rightarrow\infty}} \newcommand{zlim}[1]{\lim_{{#1}\rightarrow 0}} \newcommand{Pr}[]{\t{Pr}} \newcommand{prop}[]{\propto} \newcommand{ln}[1]{\t{ln}({#1})} \newcommand{Ln}[1]{\t{ln}\left({#1}\right)} \newcommand{min}[2]{\t{min}({#1},{#2})} \newcommand{Min}[2]{\t{min}\left({#1},{#2}\right)} \newcommand{max}[2]{\t{max}({#1},{#2})} \newcommand{Max}[2]{\t{max}\left({#1},{#2}\right)} \newcommand{pfrac}[2]{\frac{\partial{#1}}{\partial{#2}}} \newcommand{pd}[]{\partial} \newcommand{zisum}[1]{\sum_{{#1}=0}^{\infty}} \newcommand{iisum}[1]{\sum_{{#1}=-\infty}^{\infty}} \newcommand{var}[1]{\t{var}({#1})} \newcommand{exp}[1]{\t{exp}\left({#1}\right)} \newcommand{mtx}[2]{\left[\begin{matrix}{#1}\\{#2}\end{matrix}\right]} \newcommand{nmtx}[2]{\begin{matrix}{#1}\\{#2}\end{matrix}} \newcommand{nmttx}[3]{\begin{matrix}\begin{align} {#1}& \\ {#2}& \\ {#3}& \\ \end{align}\end{matrix}} \newcommand{amttx}[3]{\begin{matrix} {#1} \\ {#2} \\ {#3} \\ \end{matrix}} \newcommand{nmtttx}[4]{\begin{matrix}{#1}\\{#2}\\{#3}\\{#4}\end{matrix}} \newcommand{mtxx}[4]{\left[\begin{matrix}\begin{align}&{#1}&\hspace{-20px}{#2}\\&{#3}&\hspace{-20px}{#4}\end{align}\end{matrix}\right]} \newcommand{mtxxx}[9]{\begin{matrix}\begin{align} &{#1}&\hspace{-20px}{#2}&&\hspace{-20px}{#3}\\ &{#4}&\hspace{-20px}{#5}&&\hspace{-20px}{#6}\\ &{#7}&\hspace{-20px}{#8}&&\hspace{-20px}{#9} \end{align}\end{matrix}} \newcommand{amtxxx}[9]{ \amttx{#1}{#4}{#7}\hspace{10px} \amttx{#2}{#5}{#8}\hspace{10px} \amttx{#3}{#6}{#9}} \) \( \newcommand{ph}[1]{\phantom{#1}} \newcommand{vph}[1]{\vphantom{#1}} \newcommand{mtxxxx}[8]{\begin{matrix}\begin{align} & {#1}&\hspace{-17px}{#2} &&\hspace{-20px}{#3} &&\hspace{-20px}{#4} \\ & {#5}&\hspace{-17px}{#6} &&\hspace{-20px}{#7} &&\hspace{-20px}{#8} \\ \mtxxxxCont} \newcommand{\mtxxxxCont}[8]{ & {#1}&\hspace{-17px}{#2} &&\hspace{-20px}{#3} &&\hspace{-20px}{#4}\\ & {#5}&\hspace{-17px}{#6} &&\hspace{-20px}{#7} &&\hspace{-20px}{#8} \end{align}\end{matrix}} \newcommand{mtXxxx}[4]{\begin{matrix}{#1}\\{#2}\\{#3}\\{#4}\end{matrix}} \newcommand{cov}[1]{\t{cov}({#1})} \newcommand{Cov}[1]{\t{cov}\left({#1}\right)} \newcommand{var}[1]{\t{var}({#1})} \newcommand{Var}[1]{\t{var}\left({#1}\right)} \newcommand{pnint}[]{\int_{-\infty}^{\infty}} \newcommand{floor}[1]{\left\lfloor {#1} \right\rfloor} \) \( \newcommand{adeg}[1]{\angle{({#1}^{\t{o}})}} \newcommand{Ree}[]{\mathcal{Re}} \newcommand{Im}[]{\mathcal{Im}} \newcommand{deg}[1]{{#1}^{\t{o}}} \newcommand{adegg}[1]{\angle{{#1}^{\t{o}}}} \newcommand{ang}[1]{\angle{\left({#1}\right)}} \newcommand{bkt}[1]{\langle{#1}\rangle} \) \( \newcommand{\hs}[1]{\hspace{#1}} \)

  UNDER CONSTRUCTION

MATLAB


Matlab


Subtopics


General Simulations

Animated plot

function animplot(t,f,ymin,ymax,dt,num_iters,T)

% ANIMPLOT(t,f,ymin,ymax,dt,num_iters) -- f must be defined w/ handle, % e.g. f = @(t)sin(t); default T = 5, num_iters = 500, dt = .05, % (ymax - ymin) = 1.4*range.

switch nargin case 6; T = 5; case 5; T = 5; num_iters = 500; case 4; T = 5; num_iters = 500; dt = .05; case 2; T = 5; num_iters = 500; dt = .05; ymin = 1.2*min(f(t)) - max(f(t))/5; ymax = 1.2*max(f(t)) - min(f(t))/5; end dT = T/num_iters; h = plot(0,0,'LineWidth',2,'color','b');

% set initial handle

set(h, 'Xdata',t, 'Ydata', f(t));

% initialize curve plot

axis([min(t) max(t) ymin ymax]); tic % start timer for i=2:num_iters pause(dT) set(h, 'Ydata', f(t-dt*i)) end toc % end timer, return time elapsed since 'tic' end

t = 0:.01:2*pi;
f = @(t)sin(t);
animplot(t,f)
[Code]: animplot.m

Animated spiral

anim_spiral.m

t = 0:.01:pi; e = 2.718281828; num_iters = 770; T = 10; dT = T/num_iters; t0 = -20; x1 = @(t)e.^(.05*t).*sin(t); x2 = @(t)e.^(.05*t).*cos(t); h = plot(0,0,'b','LineWidth',2);

% set initial handle

hold on h1 = plot(0,0,'.r','MarkerSize',15); set(h, 'Xdata',t, 'Ydata', x2(t));

% initialize curve plot

set(h1,'Xdata',0, 'Ydata', 0); axis([-18 18 -18 18]); for i=2:num_iters pause(dT) set(h, 'Xdata',x1(t0:.1:t0+.1*i),'Ydata',x2(t0:.1:t0+.1*i)) set(h1, 'Xdata',x1(t0+.1*i),'Ydata',x2(t0+.1*i)) end hold off

\(\up\)[Code]: anim_spiral.m


Animated noise





Dipole fixed point
Plot the phase portrait of the following system:

\(\ds \begin{align}\dot{x}&=2xy \\ \dot{y}&=y^2-x^2\end{align}\)
>>[x,y] = meshgrid(-5:0.5:5,-5:0.5:5);
u = 2*x.*y;
v = y.^2 - x.^2;
>> quiver(x,y,u,v,'LineWidth',1.5)

% generate 2D grid coordinates for x,y from -5 to 5 w/ step-size 0.5 % define x' % define y' % generate a (velocity) vector field with components (u,v) at points .. % .. (x,y), set vector display thickness to 1.5 (1 = default)



Probability Simulations

Random variables

Geometric random variable
If \(X \vplup\) is a geometric r.v. with \(p=0.25\), what is the probability that \(X\geq 4\)?

We seek to simulate
\(\hspace{250px} \ds \sum_{k=1}^{\infty} (1-p)^{k-1}p,\quad p=.25,\)
then sum # of outcomes for \(k\geq 4\) and divide by the set # of trials to obtain \(P(X\geq 4)\). Use \(k=100\) for \(\infty\).
>> p = .25; T = 0;
for t=1:1000 
u = rand; 
A = 0; B = 0; 
 for k=1:100 
 B = A; A = A + p*(1-p)^(k-1); 
 if (u > B) && (u < A)
 S(k,t) = 1;
end; end; end

>> for t=1:1000 
for k=4:size(S,1)
 if S(k,t) > 0 
 T = T + 1; 
end; end; end
>> Prob = T/t 
Prob =
   0.4210

% set up 1000 trials % RNG between 0 & 1 % reset A & B for each trial % sample k 1->100 % lower & upper bounds; increment A by next prob val, set B to previous A % if RNG is between A & B % note: only one row per column in S is =1 per trial % check through all columns (trials) % check through all rows, starting with 4th % if column (trial) is non-empty … % … add to total % obtain P(X≥4) as a summed average over t trials


Jointly continuous rv vector
\(\vplup\)Simulate vector realizations of rv's \((X,Y)\) with standard bivariate Gaussian PDF for \(\rho=0.9\).

We know that \(Y|(X=x)\sim N(\rho x,1-\rho^2)\) and \(X\sim N(0,1)\). Using conditioning, we can use the fact that
\(p_{X,Y}(x,y)=p_{Y|X}(y|x)p_X(x)\) to generate realizations of \((X,Y)\) as follows:
[Step 1]: Generate \(X=x\) according to \(N(0,1)\)
[Step 2]: Generate \(Y|(X=x)\) according to \(N(\rho x,1-\rho^2)\)
>> randn('state',0)
rho = 0.9;
M = 1000;
for m = 1:M
x(m,1) = randn(1,1);
ygx(m,1) = rho*x(m)+sqrt(1-rho^2)*randn(1,1);
end
>> plot(x,ygx,'.')

% initialize RNG % # of realizations to generate % generate realization of N(0,1) rv [Step 1] % generate Y|(X=x) [Step 2] % plot the PDF; sample below




Basic events

Unfair coin: 100 tosses with heads odds = 40%, tails = 60%

>> h = 0; hTotal = 0;
for t=1:100 
RNG = rand;
 if RNG < 0.40
 h = 1;
 else
 h = 0;
 end
hTotal = hTotal + h;
end
>> Prob = hTotal/t
Prob =
   0.4200

% set up 100 tosses % generate random number between 0 and 1 % head is tossed; prob = 40% % tail is tossed; prob = 60% % update heads count % average over # of trials



Dragon Notes,   Est. 2018     About

By OverLordGoldDragon