I am trying to solve these time-dependent second order differential equations using ode45. My differential equations are
Coef1, coef2, coef3 are coefficients defined as arrays of real numbers. is also an array of real numbers. γ,ω,Ω are constants.
% ω in above differential equation
omega = 2262.72;% Ω in above differential equation
Omega = 148.328;% γ in above differential equation
gamma = 1.519;tau = 0.1;topt = 3;tTHz = 5;ts = linspace(0,10,1e5);alpha = 191.7789;beta = 0.82481;m2 = 3.745755e-5;erfnc = -sqrt(2*pi)*exp(-2*tTHz*1i-1/2)/32 * ( exp(4*tTHz*1i) + erf( real(sqrt(2).*ts - sqrt(2)*tTHz + sqrt(2)*1i/2) ) - exp(4*tTHz*1i) + erf( real(-sqrt(2).*ts + sqrt(2)*tTHz + sqrt(2)*1i/2) ) + 2*exp(2*tTHz*1i + 1/2) + 2*erf( real(sqrt(2).*ts - sqrt(2)*tTHz ) )*exp(2*tTHz*1i + 1/2)+1 ) ;ts1 = ts;coef1 = real( ( -sqrt(2)*exp(-2*tTHz*1i-1/2)/32 .* ( 2*sqrt(2)*exp( -(sqrt(2).*ts - sqrt(2)*tTHz + sqrt(2)*1i/2).^2 ) + 2*sqrt(2)*exp(4*tTHz*1i).*exp( -(-sqrt(2).*ts + sqrt(2)*tTHz + sqrt(2)*1i/2).^2 ) + 4*sqrt(2)*exp(2*tTHz*1i+1/2).*exp(-(sqrt(2).*ts-sqrt(2)*tTHz).^2))) ./ (erfnc + m2) );ts2 = ts;coef2 = real( alpha ./ (erfnc + m2) );ts3 = ts;coef3 = real( beta ./ (erfnc + m2) );%Eopt is the last term in my differential equation expressed above.
Eopt = sin(omega.*ts).*exp(-4*log(2).*(ts-topt).^2 / tau.^2)
To solve the ODE I follwed the instruction described here https://www.mathworks.com/help/matlab/ref/ode45.html under ODE with Time-Dependent Terms. I wrote a function named "myode" and saved it in the same folder as rest of code.
function g = myode(t, X, omega, Omega, gamma, Eopt, ts1, coef1, ts2, coef2, ts3, coef3)coef1 = interp1(ts1,coef1,t); % Interpolate the data set (ts1,coef1) at time t
coef2 = interp1(ts2,coef2,t); % Interpolate the data set (ts2,coef2) at time t
coef3 = interp1(ts3,coef3,t); % Interpolate the data set (ts3,coef3) at time t
g = {X(2) ; -coef1 *X(2) - coef2 * X(1) + coef3 * X(3) - gamma * X(2) + Eopt ; X(4) ; -omega^2 * X(3) + Omega^2 * X(1) - gamma * X(4)} ;
Now I solve for and .
% Time interval
tspan = 0 : 1/Fs :15;% Initial condition
conds = [0;0;0;0];[t,X] = ode45( @(t,X) myode(t, X, omega, Omega, gamma, Eopt, ts1, coef1, ts2, coef2, ts3, coef3), tspan, conds);
When I run this code I get erros saying
Error using odearugementsInputs must be floats, namely single or double.Error in ode45odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Please help me why I get these errors and how to fix it. If there is a better method to solve these differential equations please let me know.
Best Answer