1

Lets say I have a function file of the ODE that goes like this

function xprime = RabbitTemp(t,X) 

% Model of Rabbit Population
% where,
%       Xo = Initial Population of Rabbits
%       X(1) = Population density of Rabbit
%       X(2) = Temperature T (that varies with time) 
%       a = test parameter

%%% ODE
a  = 10;


dx(1) = (X(1))*(1 - a*X(1) - 3*(X(2))));
dx(2) = sin(t);
%%%%%%%

xprime = [dx(1) dx(2)]';

end

But what if I would like parameter a to vary as temperature X(2) varies, as the ODE solver calculates.

I understand I would first have to create some data between a and X(2) and interpolate it. But after that I'm not very sure what's next. Could any one point me in the right direction?

Or is there any other way?

1
  • 1
    May I add two more cents? 1. You should preallocate dx as the first thing in xprime and then you can simply return dx instead of copying to xprime. No need for two different variables which are the same. 2. Consider defining index variables if your code gets any larger, like ind.pop_density = 1 and ind.temp=2. It will help you in the long run. Commented Jun 8, 2015 at 18:06

1 Answer 1

1

It really depends on the function of a, a=f(T). If you can take the derivative of f(T) I suggest you include a as another state, for instance X(3) and then you can access it from the X argument in xprime easily, plus it will help Matlab determine the proper step size during integration:

function xprime = RabbitTemp(t,X) 

% Model of Rabbit Population
% where,
%       Xo = Initial Population of Rabbits
%       X(1) = Population density of Rabbit
%       X(2) = Temperature T (that varies with time) 
%       X(3) = test parameter

%%% ODE
a  = 10;
Yo = 0.4; %Just some Constant

dx(1) = (X(1))*(1 - X(1)^(a) - Yo*(X(2))));
dx(2) = sin(t);
dx(3) = .... 
%%%%%%%

xprime = [dx(1) dx(2) dx(3)]';

end

Second method is to create a function(-handle) for a=f(T) and call it from within xprime:

function xprime = RabbitTemp(t,X) 

% Model of Rabbit Population
% where,
%       Xo = Initial Population of Rabbits
%       X(1) = Population density of Rabbit
%       X(2) = Temperature T (that varies with time) 
%       a = test parameter

%%% ODE
a  = f_a(t,X);
Yo = 0.4; %Just some Constant

dx(1) = (X(1))*(1 - X(1)^(a) - Yo*(X(2))));
dx(2) = sin(t);
%%%%%%%

xprime = [dx(1) dx(2)]';

end

function a = f_a(t,X)
  return 10;  % You might want to change this. 
end

If possible go with the first variant which is creating another state for a.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your input. Unfortunately I do not know the actual relation between a and T.All I have is a set of data (although I could try fitting that data..!), that is why I was thinking about using interp1 to generate more data points, and try and feed it into the solver correspondingly. Hmm!
In that case I would definitely create a mesh with interpolations beforehand or, like you said, try and fit a curve or something similar. The latter would even result in a differentiable function so I'd try that first :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.