Skip to content

Instantly share code, notes, and snippets.

View Hugo-Diaz-N's full-sized avatar

Hugo Diaz Norambuena Hugo-Diaz-N

  • Delaware, USA
View GitHub Profile
@Hugo-Diaz-N
Hugo-Diaz-N / SmoothSpline.m
Created October 19, 2018 01:43
Smoothing Spline Matlab Code (This sparse assembly could be simplified)
function [mu,m]=SmoothSpline(X,f,p)
% [mu,m]=SmoothSpline(X,f,p)
%
% This code compute the parameter of "the smoothing natural spline"
% for the data (x_i,f_i) with weights {p_i} i.e. the solution of
%
% \min\Biggl\{ \sum_{i=0}^N p_i\left(\hat{f}(x_i)-f_i \right)^2
% +\int_{a}^{b} \bigl|\hat{f}''(x)\bigr|^2dx \Biggr\}
% Input:
@Hugo-Diaz-N
Hugo-Diaz-N / ChebyshevInterp.m
Created October 19, 2018 01:32
Lagrange interpolation with Chebyshev nodes
function p=ChebyshevInterp(f,n,xx)
n = n-1; % Number intervals
Xn = cos((pi/(n+1))*(0.5+(0:n))); % Chebyshev Nodes
f_X = f(Xn); % f at Chebyshev nodes,
omega = BaryWeigths(Xn); % Barycentric Weigths.
p = zeros(1,length(xx)); % Setting interpolant
is = ismember(xx,Xn); % Position of a member of Xn at xx.
p(is) = f(xx(is)); % p(x_k) =f(x_k).
xx = setdiff(xx,Xn); % xx \setminus Xn.
A = repmat(Xn',1,100);
@Hugo-Diaz-N
Hugo-Diaz-N / RationalCode.m
Created October 19, 2018 01:26
Rational Root-Finding Method, the approximation in this case is not linear but rational f(x)~ a+b/(x+c) (hypothesis: f'(x_k)*f''(x_k)\neq 0 )
function [flag,xk1] = RationalCode(x0,f,df,d2f,TOL,MaxIt)
%[flag,xk] = RationalCode(x0,f,df,d2f,TOL,MaxIt)
%
% This code approximate a root of f using a rational fuction approx.
% Input:
% x0 := initial value.
% f := function whose roots will be approximated.
% df := derivative of f.
% d2f := second derivative of f.
@Hugo-Diaz-N
Hugo-Diaz-N / position3D.m
Created October 19, 2018 01:17
Movement particle on a sphere surface under a force F
% Problem: find u s.t.
% \frac{d}{dt}{\bf u}(t)+\alpha {\bf u}(t)\times \frac{d}{dt}{\bf u}(t)&={\bf u}(t)\times {\bf F}(t)
% initial position u_0=0.5*\sqrt(2)(0 1 1)^t
F=[0 0 10]'; % constant case
u0=0.5*sqrt(2)*[0 1 1]';
a=0.1; % alpha in the PDE
b=(1/(1+a*a*norm(u0,2)^2));
f=@(t,u) b*(cross(u,F)-a*(F'*u)*u+a*norm(u)^2*F);
[t,w]=ode23s(f,[0,10],u0);
@Hugo-Diaz-N
Hugo-Diaz-N / DualVeerlet.m
Last active October 19, 2018 01:06
Code to model a spacecraft is given by two 2nd-order equations for the position of the body (x(t), y(t)) --- Numerical Method: Störmer-Verlet (Dual) -- Matlab
%---------------- Setting Problem --------------------%
x0 = 0.994; % x(0) Initial position
y0 = 0.0; % y(0) Initial position
dx0 = 0.0; % x'(0) Initial velocity
dy0 = -2.031732629557; % y'(0) Initial velocity
%-----------------------------------------------------%
%---------------- Setting Model ----------------------%
mu1 = 0.012277471; % Moon mass, M_m/(M_e+M_m)
mu2 = 1-mu1; % Earth mass, M_e/(M_e+M_m)
%-----------------------------------------------------%