Matlab
From TechWiki
The Matlab computing environment. The syntax is compatible with GNU's Octave
Contents |
[edit]
MySQL
- Resources: http://mmf.utoronto.ca/resrchres/mysql/
For MySQL 5, use this (recompiled) file mysql.mexglx.
[edit]
Examples
[edit]
Get data from the database and compute probability density function:
% Get data
mysql( 'open', 'localhost', 'usr', 'pwd');
mysql('use db');
% k_in
owner = mysql('SELECT owner_id FROM firmfirm;');
mysql( 'close')
% Start
sowner = sort(owner); % List of owner id's from the relational table
% PDF
y = histc(sowner,unique(sowner)); % Counting the number of occurences (ownerships) of id's
X=unique(y); % x-axis: number of shareholdings
% Degree distribution
Y=histc(y, unique(y)); % y-axis: number of companies with X shareholdings
%P=Y/max(Y); % Normalized
tot=sum(Y);
P=Y/tot; % Percentage
loglog(X,P,'o')
xlabel('k_{in}')
ylabel('Probability')
% Compute slope of power law
x1=1;
x2=30;
x=find(X>=x1 & X<=x2);
x=sort(X(x));
x_min=min(x);
n=length(x);
gamma=1+n*(sum(log(x/x_min)))^(-1)
% Check
%C=(gamma-1)*x_min^(gamma-1);
%hold
%x=[x1:1:x2];
%loglog (x,x.^(-gamma)*C)
%title('Firm-Firm PDF')
%hold
[edit]
CDF
Get data from the database and compute cumulative distribution function:
% Get data
mysql( 'open', 'localhost', 'usr', 'pwd');
mysql('use db');
% k_in
owner = mysql('SELECT owner_id FROM firmfirm;');
mysql( 'close')
% Start
sowner = sort(owner); % List of owner id's from the relational table
% Histogram
y = histc(sowner,unique(sowner)); % Counting the number of occurences (ownerships) of id's
%X=unique(y); % x-axis: number of shareholdings
% Compute CDF
x=sort(y);
n=length(x);
p=[];
x_unique=unique(x);
for I=1:length(x_unique)
p(I)=length(find(x>=x_unique(I)))/n;
end
Xc = unique(x);
Yc = p;
h=loglog(unique(x),p,'.r');
xlabel('k_{out}')
ylabel('Cumulative Distribution')
[edit]
Some Graphs
%3-D Graph xx=-5:0.2:5; yy=xx; [x,y]=meshgrid(xx,yy); z=0.5*y.^2-cos(x); mesh(x,y,z) colormap(hsv) view(55,40) figure xx=-20:0.8:20; yy=xx; [x,y]=meshgrid(xx,yy); z=x.^4+y.^4+4*x.*x.*y.*y; mesh(x,y,z) colormap(hsv) figure xx=-5:0.2:5; yy=xx; [x,y]=meshgrid(xx,yy); z=sin(x.*x+y.*y)+2*(cos(x)+cos(y)); mesh(x,y,z) colormap(hsv) figure xx=-2.5:0.1:2.5; yy=xx; [x,y]=meshgrid(xx,yy); z=cos(3*x-2*y)-cos(5*x+y); mesh(x,y,z) colormap(hsv) figure xx=-10:0.4:10; yy=xx; [x,y]=meshgrid(xx,yy); z=x.^3-2*x.*y+y.^3; mesh(x,y,z) colormap(hsv) figure xx=-10:0.4:10; yy=xx; [x,y]=meshgrid(xx,yy); z=(x.^2+y.^3+x-y)./(x.^2.*y.^2+2); mesh(x,y,z) colormap(hsv) figure xx=-25:0.9:25; yy=xx; [x,y]=meshgrid(xx,yy); z=2*x.^4+y.^4-2*x.^2-2*y.^2; mesh(x,y,z) colormap(hsv) figure xx=-10:0.4:10; yy=xx; [x,y]=meshgrid(xx,yy); z=(sin(2*sqrt(x.^2+y.^2)))./sqrt(x.^2+y.^2); mesh(x,y,z) colormap(hsv) figure xx=-10:0.4:10; yy=xx; [x,y]=meshgrid(xx,yy); z=0.5*x.^2-x.*y+y; mesh(x,y,z) colormap(hsv) %surfc(x,y,z) %shading interp
% exp plot and general expansion and parabeln %eq: B^E=exp(E*lnB) l=50; %range %EG: B fixed, E variable, eg: B^x , ie x= 2 doubling, 3: tripling, ... %i.e. exp B=2; %CONTROL-PARAMETER r=log(B); %expansion rate, i.e. factor to get exp to fit x^n plot y=[]; n=0:.1:l; y=exp(n*r); hold; plot(n,y,'r.'); hold; %EG: E fixed, B variable, eg: x^E, ie E= 2,3,... parablen l=1232; E=1.29415813; %CONTROL-PARAMETER Y=[]; X=1:1:l; Y=exp(E*log(X)); %figure; %hold; %plot(X,Y,'g.'); %hold;
x = cell(lgth, 1);
y = cell(lgth, 1);
for I = 1 : lgth
string = fEval('load', country{I}, 'h n hhat', [data, directory]);
eval(string);
x{I} = h;
y{I} = n;
hat{I} = hhat;
lab{I} = country{I};
end
figure
markersize = 5.5;
%h=semilogx(x{1}, y{1}, 'ks', x{2}, y{2}, 'k^', x{3}, y{3}, 'kx', x{4}, y{4}, 'k+', x{5}, y{5}, 'kh', x{6}, y{6}, 'k*', x{7}, y{7}, 'kd', x{8}, y{8}, 'kp');
semilogx(x{1}, y{1}, 'k:s', 'MarkerSize', markersize)
hold on
semilogx(x{2}, y{2}, 'k:+', 'MarkerSize', markersize);
semilogx(x{3}, y{3}, 'k:x', 'MarkerSize', markersize);
semilogx(x{4}, y{4}, 'k:^', 'MarkerSize', markersize);
semilogx(x{5}, y{5}, 'k:o', 'MarkerSize', markersize);
semilogx(x{6}, y{6}, 'k:*', 'MarkerSize', markersize);
semilogx(x{7}, y{7}, 'k:d', 'MarkerSize', markersize);
semilogx(x{8}, y{8}, 'k:v', 'MarkerSize', markersize);
hold off
leg=legend([lab{1}], [lab{2}], [lab{3}], [lab{4}], [lab{5}], [lab{6}], [lab{7}], [lab{8}]);
fs = 14; % Fontsize
set(gca, 'fontsize', 14);
xlabel('XLAB', 'fontsize',fs);
ylabel('YLAB', 'fontsize',fs);
title('TITLE');
hold on
xx=[0.0000001:0.00001:1];
g =plot(xx,0.8*ones(length(xx),1),'k-');
set(g, 'linewidth', 2)
set(gca, 'linewidth', 1)
set(gca, 'xlim', [7*10^-5,1], 'XTick', [10^-5,10^-4,10^-3,10^-2,10^-1,10^0])
set(leg, 'Location', 'SouthEast')
if(saveFigs)
set(gcf, 'PaperPositionMode', 'manual');
%set(gcf, 'PaperUnits', 'inches');
%set(gcf, 'PaperPosition', [2 1 4 2]);
%set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperType', 'A3');
%set(gcf, 'PaperUnits', 'inches');
%set(gcf, 'PaperSize', [80 35]);
set(gcf,'PaperOrientation', 'landscape');
set(0, 'DefaultFigurePaperOrient', 'landscape')
str = ['print -depsc2 ', [saveFigsDir, 'cc.eps']];
eval(str)
end
[edit]
Figures
function make_figures(savestr,pos) set(gcf,'Position',pos,'PaperPositionMode','auto') print(savestr,'-deps2c') eval(['! epstopdf ' savestr '.eps'])
[edit]
Stuff
[edit]
Curve Fitting Toolbox
cftool
