How to plot a polynomials - matlab

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')

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

Airfoil plot issue

I am having an issue with plotting my airfoil on matlab. It appears that there are some abberations with only one or two values. I have checked again and again with the formulas so they should be good. I have not converted to degrees so that's not the issue.
%% setup
clc
clear all
%type of airfoil
typeNACA = '2312';
%extract values of airfoil type
M = str2double(typeNACA(1)); % max camber %chord
P = str2double(typeNACA(2)); % chordwise position
Thmax = str2double(typeNACA(3:4));
M=M/100;
P=P/100
Thmax=Thmax/100;
% gridpoints
gridpoints = 1000;
% Airfoil grid
x = linspace(0,1,gridpoints);
%camber equations
%yc1(x) = (M/P^2)*(2*P*x-x^2);
%yc(x) = (M/(1-P)^2*(1-2*P+2*Px-x^2); % (for P =< x <1)
%dyc1 = (2*M/P^2)*(P-x)
%dyc2 = (2*M)/(1-P^2)*(P-x)
%Camber and gradient
yc = ones(gridpoints,1);
dyc= ones(gridpoints,1);
theta=ones(gridpoints,1);
for i=1:gridpoints
if (x(i) >= 0 && x(i) < P)
yc(i) = (M/P^2)*(2*P*x(i)-x(i)^2); % (for 0 =< x < P)
dyc(i)= ((2*M)/P^2)*(P - x(i)); %dyc/dx
elseif (x(i) >= P && x(i) <= 1) % (for P =< x <1)
yc(i)=(M/(1-P)^2)*(1-2*P+2*P*x(i)-x(i)^2);
dyc(i) = (2*M)/((1-P)^2*(P-x(i)));
end
theta(i) = atan(dyc(i)); %angle theta
end
% thickness coefficients
a0 = 0.2969;
a1 = -0.126;
a2 = -0.3516;
a3 = 0.2843;
a4 = -0.1036; % -0.1015 for open TE -0.1036 for closed TE
%thickness distribution
yt = ones(gridpoints,1);
for i=1:1:gridpoints
yt(i) = (Thmax/0.2)*(a0*x(i)^0.5 + a1*x(i) + a2*x(i)^2 + a3*x(i)^3 + a4*x(i)^4);
end
% Upper surface points
xu = ones(gridpoints,1);
yu = ones(gridpoints,1);
for i= 1:gridpoints
xu(i) = x(i) - yt(i)*sin(theta(i));
yu(i) = yc(i) + yt(i)*cos(theta(i));
end
% Lower surface points
xl = ones(gridpoints,1);
yl = ones(gridpoints,1);
for i=1:1:gridpoints
xl(i) = x(i) + yt(i)*sin(theta(i));
yl(i) = yc(i) - yt(i)*cos(theta(i));
end
%PLOT
f1 = figure(1);
hold on; grid on;
axis equal
plot(xu,yu,'r';
plot(xl,yl,'b');
well! The first problem is you've forgotten the closing parenthesis in the second line from the end. But if you want me to check your code, I need to have your typeNACA() function .m file to revise it. Or u can post its code here.
%PLOT
f1 = figure(1);
hold on; grid on;
axis equal
plot(xu,yu,'r';
plot(xl,yl,'b');

Bifurcation diagram of discrete SIR model in MATLAB

I have a problem with my MATLAB code to display a figure of a Bifurcation diagram of discrete SIR model.
My model is:
S(n+1) = S(n) - h*(0.01+beta*S(n)*I(n)+d*S(n)-gamma*R(n))
I(n+1) = I(n) + h*beta*S(n)*I(n)-h*(d+r)*I(n)
R(n+1) = R(n) + h*(r*I(n)-gamma*R(n));
I tried out the code below but it keeps MATLAB busy for almost 30 mins and showed up no figure.
MATLAB code:
close all;
clear all;
clc;
%Model parameters
beta = 1/300;
gamma = 1/100;
D = 30; % Simulate for D days
N_t = floor(D*24/0.1); % Corresponding no of hours
d = 0.001;
r = 0.07;
%Time parameters
dt = 0.01;
N = 10000;
%Set-up figure and axes
figure;
ax(1) = subplot(2,1,1);
hold on
xlabel ('h');
ylabel ('S');
ax(2) = subplot(2,1,2);
hold on
xlabel ('h');
ylabel ('I');
%Main loop
for h = 2:0.01:3
S = zeros(N,1);
I = zeros(N,1);
R = zeros(N,1);
S(1) = 8;
I(1) = 5;
R(1) = 0;
for n = 1:N_t
S(n+1) = S(n) - h*(0.01+beta*S(n)*I(n)+d*S(n)-gamma*R(n));
I(n+1) = I(n) + h*beta*S(n)*I(n)-h*(d+r)*I(n);
R(n+1) = R(n) + h*(r*I(n)-gamma*R(n));
end
plot(ax(1),h,S,'color','blue','marker','.');
plot(ax(2),h,I,'color','blue','marker','.');
end
Any suggestions?
It was very slow because you were plotting a single value of h versus a vector S which had 7200 points. I assumed that you only want to plot the last value of S versus h. So replacing S with S(end) in the plot command changes everything. You really didn't need to use hold and it's better call plot once for each axis, so here is how I would do it:
beta = 1/300;
gamma = 1/100;
D = 30; % Simulate for D days
N_t = floor(D*24/0.1); % Corresponding no of hours
d = 0.001;
r = 0.07;
%%Time parameters
dt = 0.01;
N = 10000;
%%Main loop
h = 2:0.01:3;
S_end = zeros(size(h));
I_end = zeros(size(h));
for idx = 1:length(h)
S = zeros(N_t,1);
I = zeros(N_t,1);
R = zeros(N_t,1);
S(1) = 8;
I(1) = 5;
R(1) = 0;
for n=1:(N_t - 1)
S(n+1) = S(n) - h(idx)*(0.01+beta*S(n)*I(n)+d*S(n)-gamma*R(n));
I(n+1) = I(n) + h(idx)*beta*S(n)*I(n) - h(idx)*(d+r)*I(n);
R(n+1) = R(n) + h(idx)*(r*I(n)-gamma*R(n));
end
S_end(idx) = S(end);
I_end(idx) = I(end);
end
figure(1)
subplot(2,1,1);
plot(h,S_end,'color','blue','marker','.');
xlabel ('h');
ylabel ('S');
subplot(2,1,2);
plot(h,I_end,'color','blue','marker','.');xlabel ('h');
xlabel ('h');
ylabel ('I');
This now runs in 0.2 seconds on my computer.

How can I best represent a speed bump with heaviside?

%% 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

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