Solution to 2nd order ode containing time varying coeficients using ode45

1 view (last 30 days)
Hello,
I am trying to solve a 2nd order Initial value problem using ode45. I have a time varying coefficient P2 which I am importing from an excel file. Then I am using the interp1 command to interpolate its values over the span of time the ode functions. My code is as follows:
function dx=gglpr9(t,x,ft,P2);
ft=(1:1:100);
y=xlsread('AA.xlsx',1);
tspan=[0 100];
P2=interp1(ft,y(:,4),tspan);
m=0.2;
C=0.2;
Pa=30e5;
Aa=1440e-6;%actuator effective bellow area in m^2.
As=762e-6;%sensing side bellow effective area in m^2
Ka=12*9.81*1000*1.1;%actuator side below stiffness
Ks=14.8*9.81*1000*1.1;%sensing bellow stiffness
Ke=Ka+Ks;
dx=[x(2);1/m*((P2*As)-(Pa*Aa)-(C*x(2))-(Ke*x(1)))];
I am running the above first. Then I am typing the following commands one by one in the command window.
tspan=[0 100];
options=odeset('Nonnegative',1);
[t x]=ode45(@gglpr9,tspan,[0,0],options);
I am getting the following error message:
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Request your help regarding this.
Thanks and regards.

Answers (2)

J. Alex Lee
J. Alex Lee on 2 Aug 2020
can you use the code editor functionality to display code?
Multiple issues:
  1. you don't need to "run" the gglpr9 function...is there something else you mean by that?
  2. your references to gglpr9 with @gglpr9 doesn't include your additional parameters, you need @(t,x)gglpr9(t,x,ft,P2) - this is probably causing your error with odearguments
  3. though supplying ft and P2 to gglpr9, you are overwriting them - you can probalby just delete them from the input arguments...
  4. ...but, that would be silly, to keep reading a file and interpolating within your odefun. Take that out! Don't pass ft and P2, pass another function handle to evaluate the interpolation (look at griddedinterpolant)
In summary, figure out how to effectively use function handles, avoid evaluating interpolant on every call to your odefun.

Star Strider
Star Strider on 2 Aug 2020
It is not possible to run yoiur code without ‘AA.xlsx’, however one problem is obvious.
The ‘P2’ assignment must return only 1 value, corresponding to the the particular evaluation time:
P2=interp1(ft,y(:,4),t);
and your ODE function only needs the first 2 arguments, since the others are generated internally:
function dx=gglpr9(t,x)
.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!