Skip to content

Instantly share code, notes, and snippets.

View mick001's full-sized avatar

Michy mick001

View GitHub Profile
@mick001
mick001 / copulas_revised_1_7.R
Last active March 13, 2016 22:20
How to fit a copula model in R [heavily revised]. Part 1: basic tools. Full article at http://firsttimeprogrammer.blogspot.com/2016/03/how-to-fit-copula-model-in-r-heavily.html
par(mfrow = c(1, 3))
# 3D plain scatterplot of the density, plot of the density and contour plot
scatterplot3d(u[,1], u[,2], pdf_, color="red", main="Density", xlab ="u1", ylab="u2", zlab="dCopula", pch=".")
persp(mycopula, dCopula, main ="Density")
contour(mycopula, dCopula, xlim = c(0, 1), ylim=c(0, 1), main = "Contour plot")
par(mfrow = c(1, 3))
# 3D plain scatterplot of the CDF, plot of the CDF and contour plot
scatterplot3d(u[,1], u[,2], cdf, color="red", main="CDF", xlab = "u1", ylab="u2", zlab="pCopula",pch=".")
persp(mycopula, pCopula, main = "CDF")
@mick001
mick001 / copulas_revised_1_8.R
Last active March 13, 2016 22:20
How to fit a copula model in R [heavily revised]. Part 1: basic tools. Full article at http://firsttimeprogrammer.blogspot.com/2016/03/how-to-fit-copula-model-in-r-heavily.html
frank <- frankCopula(dim = 2, param = 3)
clayton <- claytonCopula(dim = 2, param = 1.2)
gumbel <- gumbelCopula(dim = 2, param = 1.5)
par(mfrow = c(1, 3))
# Density plot
persp(frank, dCopula, main ="Frank copula density")
persp(clayton, dCopula, main ="Clayton copula density")
persp(gumbel, dCopula, main ="Gumbel copula density")
% Closed loop transfer function with proportional regulator
k = 50;
R = k;
L = R*G;
F = L/(1+L);
step(F)
% Root locus (k > 0)
rlocus(G)
% Step response
step(G)
% Impulse response
impulse(G)
% System response when u=sin(wt) and w=wc
w = 0.441;
H = G;
t = [0:0.01:500];
u = sin(w*t);
[y t x] = lsim(H,u,t);
plot(t,y,'red')
@mick001
mick001 / mass_spring2.m
Last active March 13, 2019 08:15
Mass attached to a spring Matlab simulation part 2. Full article at http://www.firsttimeprogrammer.blogspot.com/2016/02/simulating-mass-attached-to-spring-with.html
% Initialize Laplace domain variable
s = tf('s');
% Transfer function
G = C*inv(s*eye(2)-A)*B+D;
% Bode diagram
bode(G)
@mick001
mick001 / mass_spring1.m
Last active March 13, 2019 08:15
Mass attached to a spring Matlab simulation part 1. Full article at http://www.firsttimeprogrammer.blogspot.com/2016/02/simulating-mass-attached-to-spring-with.html
%Mass attached to the spring (Kg)
m = 10;
% Friction coefficient
rho = 0.8;
% Spring constant (N/m)
k = 2;
% State space representation
@mick001
mick001 / matlabrlc.m
Last active March 13, 2019 08:15
Matlab RLC circuit frequency response simulation. Full article at http://firsttimeprogrammer.blogspot.com/2015/12/step-response-of-rlc-series-circuit.html
% Inductance (H)
l = 0.010;
% Capacitance (F)
c = 1*10^(-8);
% Resistance (Ohm)
r = 250;
% Generalized gain
m = 1
@mick001
mick001 / highpass.m
Last active March 13, 2019 08:15
Frequency response RC circuit matlab. High pass filter. Check the article at http://firsttimeprogrammer.blogspot.it/2015/12/how-to-simulate-simple-low-high-pass.html
% Capacitance
C = 10*10^(-6);
% Time constant
tau = R*C;
% Define the transfer function
sys = tf([1/R 0],[1 1/(R*C)]);
% Plot the frequency response
@mick001
mick001 / copulas_example.R
Last active April 15, 2023 09:17
Modelling dependence with copulas. Full article at: http://datascienceplus.com/modelling-dependence-with-copulas/
#Load library mass and set seed
library(MASS)
set.seed(100)
# We are going to use 3 random variables
m <- 3
# Number of samples to be drawn
n <- 2000