Cause of: "Error: Inputs must be floats, namely single or double."

34 views (last 30 days)
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 odearugements
Inputs must be floats, namely single or double.
Error in ode45
odearguments(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.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Nov 2019
It is not valid to return a cell array from your ode function.
You are trying to return a series of values of different sizes. You must instead return exactly the same number of values as you have initial conditions (4 in your case)
I suspect you should be interpolating Eopt by time, not just coef1, coef2, coef3 . All of those appear to be using the same time base, so it is not clear why you are complicating matters by using different variables for the time bases.
  1 Comment
Mai Sakuragi
Mai Sakuragi on 4 Nov 2019
Edited: Mai Sakuragi on 4 Nov 2019
Thank you for your answer.
I want to use just a single variable for the time base since ts1, ts2, ts3 are all the same, but I don't really understand how the function "myode" works. How should I list the arguments in myode so that I can just use a single time base ?
Interpolating Eopt at time t did not fix the error.

Sign in to comment.

More Answers (1)

Fabio Freschi
Fabio Freschi on 4 Nov 2019
What I can see is
1) Fs is missing. In any case, tspan shpuld be a 2 entries array with initial and final values
tspan = [0,15];
2) inside myode the output must be an double array, not a cell array.
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)] ;
The problem is that if I change the curly brackets with square brackets, another error appears, because Eopt is a vector and not a scalar. Could you please check how you can change your Eopt?
  1 Comment
Walter Roberson
Walter Roberson on 4 Nov 2019
It is perfectly fine to pass a vector of length greater than 2 as tspan: it tells ode45 to return values at exactly those times, whereas with a tspan that is a vector of length 2, it would return whatever time values it felt like.

Sign in to comment.

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by