 # Dragon Notes UNDER CONSTRUCTION
Latest content:
 Apr 05 Deep Learning Mar 19 Anomaly Detection - ML Mar 13 +Data Tables Mar 08 Clustering - Machine Learning Feb 28 Support Vector Machines - ML Feb 20 Regression - Data Science

# 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 …

% 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%