How to interpolate a vector and work with variables (ode45)?

Hello,
I'm working with a ODE model the one I have to put a vector in a variable and then solve it, but I have some parameters which depends on some of the equations of the model, like auxiliar functions/equations but I haven't been able to make it work, this is the error I get:
Error using Hovorka2004_Prueba>fun
Too many output arguments.
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); %
ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0,
tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in Hovorka2004_Prueba (line 8)
[t,x]=ode45(@fun,tspan,x0);
this is what I have so far:
function [t,x]=Hovorka2004_Prueba
tspan=[0;100];
x0 = [0.1249; 0.0941; 0; 0;0; 0.7665;0.9519; 0.8473];
global out1
[t,x]=ode45(@fun,tspan,x0);
out1=[t,x]
assignin('base','out1',out1);
figure
subplot(2,1,1)
plot(t,x(:,1));
subplot(2,1,2)
plot(t,x(:,5));
end
function fun(t,x)
uu1 = importdata('datossinmodi.mat');
ufun = @(t) interp1(1:length(uu1), uu1, t, 'nearest', 0);
% Begining of the parameters
% Constants:
F01= 0.0097;
EGP0= 0.0161;
k12=0.066;
DG=0;
AG=0.8;
tmaxG=40;
VG= 0.16;
tmaxI=55;
Ke=0.138;
VI=0.12;
Ka1=0.006;
Ka2=0.06;
Ka3=0.03;
Kb1= 51.2e-4*Ka1;
Kb2= 8.2e-4*Ka2;
Kb3= 520e-4*Ka3;
% Auxiliar Functions/Equations:
G=x(1)/VG;
if G>=4.5
F01_C=F01;
else
F01_C=F01*G/4.5;
end
if G>=9
FR=0.003*(G-9)*VG;
else
FR=0;
end
UI=x(4)/tmaxI;
UG=(DG*AG*t*exp(-t/tmaxG))/(tmaxG)^2;
% Model:
f = @(t,x) [
-((F01_C/VG*G)*x(1))+ x(6)*x(1) + k12*x(2) - FR + UG + EGP0*(1-x(8)); %(1)
x(6)*x(1)-(k12+x(7))*x(2); %(2)
ufun(t)-(x(3)/tmaxI); %(3) IN HERE ufun(t) IS WHERE I AM TRYING TO PUT THE VECTOR
(x(3)/tmaxI)-(x(4)/tmaxI); %(4)
UI/VI - Ke*x(5); %(5)
-Ka1*x(6)+ Kb1*x(5); %(6)
-Ka2*x(7)+ Kb2*x(5); %(7)
-Ka3*x(8)+ Kb3*x(5); %(8)
];
end
How can I make it work? What's wrong with it?
Thanks a lot for your help (:

Answers (1)

You indicate that fun is your objective function in the ode45 call, but you define
function fun(t,x)
which indicates that fun does not return any values. fun must return a column vector that is the same length as x. Perhaps you want to return the result of invoking your f on t and x.

3 Comments

Hi, Walter,
I just tried to declare function as:
function [t,x]= fun(t,x)
and as:
function f= fun(t,x)
but it keeps sending me errors:
(for function [t,x]= fun(t,x))
>> Hovorka2004_Prueba
Error in Hovorka2004_Prueba>fun (line 21)
uu1 = importdata('datossinmodi.mat');
Error using feval
Output argument "dxdt" (and maybe
others) not assigned during call to
"C:\Users\AnnieA\Dropbox\Tesis (A.
Olay)\MATLAB Simulations\Lectura de
Vectores\Hovorka2004_Prueba.m>fun".
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); %
ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0,
tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in Hovorka2004_Prueba (line 8)
[t,x]=ode45(@fun,tspan,x0);
(for function f= fun(t,x))
>> Hovorka2004_Prueba
Error using odearguments (line 93)
FUN returns a vector of length 1,
but the length of initial conditions
vector is 8. The vector returned by
FUN and the initial conditions
vector must have the same number of
elements.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0,
tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in Hovorka2004_Prueba (line 8)
[t,x]=ode45(@fun,tspan,x0);
Any suggestion? :/
Your f is a single value, a function handle. Perhaps you want to return the result of invoking your f on t and x.
Or more simply, leave out the @(t,x) and then f will be a numeric array directly instead of a function handle.
If I leave out the @(t,x) it will still interpolate the vector into ufun(t), right?

Sign in to comment.

Products

Asked:

on 29 Nov 2015

Commented:

on 30 Nov 2015

Community Treasure Hunt

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

Start Hunting!