How can I best represent a speed bump with heaviside? - matlab

%% numerical values
% speed & time
v = 5; % m/s
t_end =10; % s
% Runge Kutta
dt = 0.001;
n = t_end/dt;
% suspended mass
ms = 300*0.7; % mass (70% of the mass moto + rider)
ks = 85000; % spring N/m
mus = 4000; % damping
% wheel
mw = 50; % mass
kw = 180000; % spring N/m
% frame
L = 0.55; % m
l = 0.15; % m
%% Bump
hs = 0.1 %height m side walk
hb = 0.05; %height m speed bump
w = 0.20; %width m
How to add the c?
%a) side walk
%r = #(t) hs*(v*t>0);
%b) speed bump
r = #(t) -4*hb/w^2*v*t.*(v*t-w).*(v*t>0 & v*t<w);
%% variables
% x(0)= position suspended mass (x_s)
% x(1) = position non-suspended mass (x_u)
% x(2) = velocity suspended mass (dx_s/dt)
% x(3) = velocity non-suspended mass (dx_u/dt)
%% differential equation
dx = #(t, x) [ x(3);
x(4);
1/ms*(-ks*(x(1)-l/L*x(2)) - mus*(x(3)-l/L*x(4)));
1/mw*(-kw*(x(2)-r(t)) - ks*(x(2)-L/l*x(1))-mus*(x(4)-L/l*x(3)))];
%% ode 45
[tt, y] = ode45(dx, [0 t_end], x0);
%% Runge-Kutta
x = zeros(n+1,4);
t = 0 : dt : t_end;
for i=1:n
k1 = dx(t(i), x(i,:))';
k2 = dx(t(i)+dt/2, x(i,:)+0.5*k1*dt)';
k3 = dx(t(i)+dt/2, x(i,:)+0.5*k2*dt)';
k4 = dx(t(i+1),x(i,:)+k3*dt)';
x(i+1,:) = x(i,:) + (0.5*k1+k2+k3+0.5*k4)/3.*dt;
end

Related

Applying ode15s to solve a spatial discretization using finite difference for multiple variables

I am trying to run the following ode15s with three variables, C1, q1 and phi1. However, I keep on receiving the following warning:
Warning: Failure at t=1.163115e-13. Unable to meet integration
tolerances without reducing the step size below the smallest value
allowed (4.038968e-28) at time t.
The simulation runs correctly if I do not include the dq1_over_dt term in the ode15s solver, so issue is definitely there. If the simulation runs correctly, the result is a Y matrix of 486X360 dimensions.
Any practical advice is welcomed.
%-------------------------------------------------------------------------%
% Chromatography Simulation 1
% By Santiago Taguado
% Chromatogram Simulation
%-------------------------------------------------------------------------%
close all; clear variables; clc;
% Global variables (meaning reported below)
global u Dax N h isopar K eps v Dphi phi_in Cin mod_gradient
%-------------------------------------------------------------------------%
% Chromatography with variable boundaries
%-------------------------------------------------------------------------%
L = 25; % Length (cm)
Q = 2.513; % Flow Rate (mL/min)
D = 0.8; % Diameter of Column (cm)
S = pi()*(D/2)^2; % Column Cross Section (cm2)
eps = 0.7; % Void Fraction
u = Q/S; % superficial velocity (cm/min)
v = u/eps; %intersitial velocity
Dax = 0.039/u; % axial dispersion (cm)
Dphi = 0.039/u; % axial dispersion (cm)
N = 120; % number of grid points (-)
alpha1 = 149421.036; % Parameter 1
alpha2 = -3.054; % Sensibility variable 1
alpha3 = 500; % Parameter 2
alpha4 = 500; % Sensibility variable 2
isopar = [alpha1 alpha2 alpha3 alpha4];
K = 3; % Linear Driving Force
eps = 0.7; % Void Fraction
%-------------------------------------------------------------------------%
% Preprocessing
%-------------------------------------------------------------------------%
h = L/(N-1); % grid spacing (cm)
nt = 3000; % number of time steps
t_load = 8; % loading time,min
t_tot = 49;
dt = t_tot/(nt-1);
%-------------------------------------------------------------------------%
% Solution via ode15s solver for Loading Phase
%-------------------------------------------------------------------------%
C1 = [0; zeros(N-1,1)]; % Initial Conditions
q1 = [0;zeros(N-1,1)];
phi = [0;zeros(N-1,1)];
Y = [C1;q1;phi];
phi_in = 0.02;
Cin = 1.085; % initial concentration (mg/mL)
mod_gradient = 0;
tspan = 0:dt:t_load;
[t, Y] = ode15s(#ODESystem, tspan, Y);
%-------------------------------------------------------------------------%
% ODE system
%-------------------------------------------------------------------------%
function dY = ODESystem(t,Y)
global u Dax N h Dphi v phi_in Cin mod_gradient eps isopar
C1 = Y(1:N);
q1 = Y(N+1:N*2);
phi = Y(N*2+1:N*3);
dC1_over_dt = zeros(N,1);
dq1_over_dt = zeros(N,1);
dphi_over_dt = zeros(N,1);
% Boundary # x=0
dC1_over_dt(1) = (Cin(1) + Dax/u/h*C1(2))/(1+Dax/u/h);
dq1_over_dt(1) = 0;
dphi_over_dt(1) = (phi_in + mod_gradient*t + Dphi/v/h*phi(2))/(1+Dphi/v/h);
% Internal points
for i=2:N-1
% Isotherm Value
H1 = isopar(1)*phi(i)^isopar(2); H1(isinf(H1)) = 0;
q_inf1 = isopar(3)*H1/(1+H1); q_inf1(isnan(q_inf1)) = 0;
denom = 1 + C1(i)*H1/q_inf1; denom(isnan(denom)) = 1;
qstar1 = C1(i)*H1/denom;
dq1_over_dt(i) = eps/(1-eps)*(qstar1 - q1(i));
% Species, 1
dC1_over_dx = (C1(i-1)-C1(i))/(h);
d2C1_over_dx2 = (C1(i+1)-2.*C1(i)+C1(i-1))/h^2;
% Modifier,1
dphi_over_dx = (phi(i-1)-phi(i))/(h);
d2phi_over_dx2 = (phi(i+1)-2.*phi(i)+phi(i-1))/h^2;
dphi_over_dt(i) = u*dphi_over_dx + ...
Dphi*d2phi_over_dx2;
dC1_over_dt(i) = v*dC1_over_dx + Dax*d2C1_over_dx2;
end
% Boundary # x=L
dC1_over_dt(N) = dC1_over_dt(N-1);
dq1_over_dt(N) = dq1_over_dt(N-1);
dphi_over_dt(N) = dphi_over_dt(N-1);
dY = [dC1_over_dt;dq1_over_dt;dphi_over_dt];
end

How to plot a polynomials

I'm trying to plot these polynomials shown in the image, but I keep receiving errors, and I'm not if my code is correct or not.
Could you please help?
Regards
Polynomials:
CODE:
clear all
close all
clc
%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = 0;
c = 1+i;
P(1) = 1;
Q(1) = 1;
P(2) = P(1) + exp(i*(2^(0))*t)*Q(1);
Q(2) = P(1) - exp(i*(2^(0))*t)*Q(1);
P(3) = P(2) + exp(i*(2^(1))*t)*Q(2);
Q(3) = P(2) - exp(i*(2^(1))*t)*Q(2);
P(4) = P(3) + exp(i*(2^(2))*t)*Q(3);
Q(4) = P(3) - exp(i*(2^(2))*t)*Q(3);
for m=1:16
x = x +c*exp(i*2*pi*m*t).*P(m);
end
figure
subplot(2,2,1)
plot(t,P(3))
title('signal')
Here's a quick fix to your code as written.
clear all
close all
clc
%%
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1024; % Length of signal
t = 2*(0:L-1)*T; % Time vector
x = zeros(1,L);
c = 1+1i;
P = zeros(16,L); Q= zeros(16,L);
P(1,:) = 1;
Q(1,:) = 1;
for j = 1:16
P(j+1,:) = P(j,:) + exp(1i*(2^j)*t).*Q(j,:);
Q(j+1,:) = P(j,:) - exp(1i*(2^j)*t).*Q(j,:);
end
for m=1:16
x = x +c*exp(i*2*pi*m*t).*P(m,:);
end
figure
subplot(2,2,1)
plot(t,P(3,:))
title('signal')

How to use fast Fourier transform for the complex values in MATLAB

I solved a differential equation and the solutions of that are the complex values in the time domain. I have to transform it to the frequency domain with FFT. I have used FFT in MATLAB, but the answers are not correct. How can I choose my interval of frequency?
The time domain is between -10 and 60 and the number of steps is 1000.
function r = fur()
clc
clear
format long
a = -10; % tmin
b = 60; % tmax
m = 1000; % Number of steps
t = zeros(1, m);
y = zeros(1, m);
t(1) = a; % Boundary condition
y(1) = 0; % Boundary condition
t0 = 0.01;
h = (b-a)/m;
for j=1:m
T = t(j); Y = y(j);
k1 = h*Fun(T, Y);
k2 = h*Fun(T + h/2, Y + k1/2);
k3 = h*Fun(T + h/2, Y + k2/2);
k4 = h*Fun(T + h, Y + k3);
y(j+1) = Y + (k1 + 2*k2 + 2*k3 + k4)/6;
t(j+1) = a + h*(j);
end
% real_y = real(y);
% imag_y = imag(y);
y;
%% Fast Fourier transformation for P(W)
NFFT = length(y);
fs = 2*pi/h;
X = fftshift(fft(y, NFFT));
fVals = (0:NFFT-1)*fs/NFFT;
figure(1)
plot(fVals, abs(X), '-b');
title('Fast Fourier transform');
xlabel('Frequency (THz)')
ylabel('p(w)');
hold on
%% Fast Fourier transformation for E(W)
NFFT = length(y);
fs = 2*pi/h;
Z = fftshift(fft(Et(t, t0), NFFT));
fVals = (0:NFFT-1)*fs/NFFT;
figure (2)
plot(fVals, abs(Z), '-r');
title('Fast Fourier Transform');
xlabel('Frequency (THz)')
ylabel('E(w)');
hold on
%% Linear susceptibility
f = X./Z;
f_imag = imag(f);
f_real = real(f);
fVal = (0:NFFT-1)*fs/NFFT;
figure(3)
plot(fVal, f_real, '-r');
title('total part of susceptibility');
xlabel('Frequency (THz)')
ylabel('Kappa(w)');
hold on
figure(4);
plot(fVals, f_imag, '-r');
title('imaginary part of susceptibility');
xlabel('Frequency (THz)')
ylabel('kappa(w)');
hold on
And this is the Fun.m file:
function F = Fun(t, y)
format long
E_g = 1.52; % Binding energy for GaAs
gamma = 0.1*E_g;
t0 = 0.01; % This is a damping operator
k = 1;
F = -i*((((k^2)-i*gamma)*y)-Et(t, t0)); % F = -i*((-i*gamma)*y-1/2);;
And this is the Et.m file:
function e = Et(t, t0)
format long
t0 = 0.1;
e = (1/2)*(exp(-(t/t0).^2));
end

Find positive solutions in undetermined system of equations (revisited)

I tried to apply the method posted in Find positive solutions to undetermined linear system of equations to the set
A=[0 0.0992 0.315 0.619 1; 0 0.248 0.315 0.248 0]; b=[0.1266 0.4363].
It is supposed that there exists a positive and constrained solution to this problem. The worst thing is that I have an answer code but I can't make it work because my Matlab version doesn`t recognize the anonymous function call and some instructions are obscure for me.
Here is the code:
% example_pt_source_atmos_setup.m
% determine geometry
D2 = 0.5; % diameter of the observation aperture [m]
wvl = 1e-6; % optical wavelength [m]
k = 2*pi / wvl; % optical wavenumber [rad/m]
Dz = 50e3; % propagation distance [m]
% use sinc to model pt source
DROI = 4 * D2; % diam of obs-plane region of interest [m]
D1 = wvl*Dz / DROI; % width of central lobe [m]
R = Dz; % wavefront radius of curvature [m]
% atmospheric properties
Cn2 = 1e-16; % structure parameter [m^-2/3], constant
% SW and PW coherence diameters [m]
r0sw = (0.423 * k^2 * Cn2 * 3/8 * Dz)^(-3/5);
r0pw = (0.423 * k^2 * Cn2 * Dz)^(-3/5);
p = linspace(0, Dz, 1e3);
% log-amplitude variance
rytov = 0.563 * k^(7/6) * sum(Cn2 * (1-p/Dz).^(5/6) ...
.* p.^(5/6) * (p(2)-p(1)));
% screen properties
nscr = 11; % number of screens
A = zeros(2, nscr); % matrix
alpha = (0:nscr-1) / (nscr-1);
A(1,:) = alpha.^(5/3);
A(2,:) = (1 - alpha).^(5/6) .* alpha.^(5/6);
b = [r0sw.^(-5/3); rytov/1.33*(k/Dz)^(5/6)];
% initial guess
x0 = (nscr/3*r0sw * ones(nscr, 1)).^(-5/3);
% objective function
fun = #(X) sum((A*X(:) - b).^2);
% constraints
x1 = zeros(nscr, 1);
rmax = 0.1; % maximum Rytov number per partial prop
x2 = rmax/1.33*(k/Dz)^(5/6) ./ A(2,:);
x2(A(2,:)==0) = 50^(-5/3)
[X,fval,exitflag,output] ...
= fmincon(fun,x0,[],[],[],[],x1,x2)
% check screen r0s
r0scrn = X.^(-3/5)
r0scrn(isinf(r0scrn)) = 1e6;
% check resulting r0sw & rytov
bp = A*X(:); [bp(1)^(-3/5) bp(2)*1.33*(Dz/k)^(5/6)]
[r0sw rytov]
Thanks for your attention.
Carolina Rickenstorff

Projectile Motion with Drag Force Matlab

I'm trying to model projectile motion with air resistance.
This is my code:
function [ time , x , y ] = shellflightsimulator(m,D,Ve,Cd,ElAng)
% input parameters are:
% m mass of shell, kg
% D caliber (diameter)
% Ve escape velocity (initial velocity of trajectory)
% Cd drag coefficient
% ElAng angle in RADIANS
A = pi.*(D./2).^2; % m^2, shells cross-sectional area (area of circle)
rho = 1.2 ; % kg/m^3, density of air at ground level
h0 = 6800; % meters, height at which density drops by factor of 2
g = 9.8; % m/s^2, gravity
dt = .1; % time step
% define initial conditions
x0 = 0; % m
y0 = 0; % m
vx0 = Ve.*cos(ElAng); % m/s
vy0 = Ve.*sin(ElAng); % m/s
N = 100; % iterations
% define data array
x = zeros(1,N + 1); % x-position,
x(1) = x0;
y = zeros(1,N + 1); % y-position,
y(1) = y0;
vx = zeros(1,N + 1); % x-velocity,
vx(1) = vx0;
vy = zeros(1,N + 1); % y-velocity,
vy(1) = vy0;
i = 1;
j = 1;
while i < N
ax = -Cd*.5*rho*A*(vx(i)^2 + vy(i)^2)/m*cos(ElAng); % acceleration in x
vx(i+1) = vx(i) + ax*dt; % Find new x velocity
x(i+1) = x(i) + vx(i)*dt + .5*ax*dt^2; % Find new x position
ay = -g - Cd*.5*rho*A*(vx(i)^2 + vy(i)^2)/m*sin(ElAng); % acceleration in y
vy(i+1) = vy(i) + ay*dt; % Find new y velocity
y(i+1) = y(i) + vy(i)*dt + .5*ay*dt^2; % Find new y position
if y(i+1) < 0 % stops when projectile reaches the ground
i = N;
j = j+1;
else
i = i+1;
j = j+1;
end
end
plot(x,y,'r-')
end
This is what I am putting into Matlab:
shellflightsimulator(94,.238,1600,.8,10*pi/180)
This yields a strange plot, rather than a parabola. Also it appears the positions are negative values. NOTE: ElAng is in radians!
What am I doing wrong? Thanks!
You have your vx and vy incorrect... vx= ve*sin(angle in radians) and opposite for vy. U also u do not need a dot in between ur initial velocity and the *... That is only used for element by element multiplication and initial velocity is a constant variable. However the dot multiplier will not change the answer, it just isn't necessary..