Aug 20, 2010

MATLAB notes.

* Adjusting a MATLAB image to full size page and export it as a .pdf file (for a known X,Y,Z vectors, in this case, velocity values ​​measured in laboratory):
>> figure;
pcolor(X,Y,Z);
grid; hold on; shading interp;
colorbar; colormap('jet');
contour(X,Y,Z,12,'k');
title('Isotach curves','FontSize',15)
xlabel('Width of the section (m)','FontSize',12)
ylabel('Heigth of the section (m)','FontSize',12)
axis equal tight
>> orient landscape
>> print -dpdf Isotach_curves.pdf


* Exporting data vectors from MATLAB to text files (for T and Sa vectors):
>> dlmwrite('spectrum.txt', T, 'delimiter', '\t', 'precision', 6)
>> dlmwrite('spectrum.txt', Sa, '-append', 'delimiter', '\t',... 'precision', 6)


* Evaluating a symbolic expression in MATLAB (subs(...)). Try this code in which I made a simple implementation of the inverse transform method to generate continuous RV of a given PDF:
syms x Fxx;
lambda = 0.5;
fx     = lambda*exp(-lambda*x);   % Desired PDF
Fx     = int(fx,x);               % CDF
n      = 1e5;                     % Number of random numbers
U      = rand(1,n);               % RN Uniformly distributed 0-1
fun    = Fx-Fxx;
X      = solve(fun,x);
Fxx    = U;
RN     = real(subs(X));

figure;
hist(RN,ceil(sqrt(n-1)));