Changing Inputs per Time Step in ODE45

  • Follow


Hi,

I am a new MATLAB user and I am attempting to use the ODE45 solver to solve a system of 2 1st order differential equations. However, I need to vary the equation inputs, u1 and u2, at every time step. 
I have attempted to do this by using a For loop and If statement to assign constant value to the input variables u1 and u2 at each time step selected from two vectors, U1 and U2.
The code executes in MATLAB and generates outputs however the results do not appear to be correct.
If someone could review the code to check that my for and if statements are correct it would be greatly appreciated.
Thank you for your time,
Sarah

function [Store1] = ModelS1(Inputs, T1, b, c)
% Define Store input time series 
U1 = Inputs(:,1);
U2 = Inputs(:,2);
% Set the initial conditions 
Xo = [b;c];
% Define the time span over which to execute the model 
tspan = 0:1:99;
% Implementation of u1 and u2 within the model
n = length (tspan);
for a=1:n;
     if(tspan(a))
        u1 = U1(a);
        u2 = U2(a);
        [t,X] = ode45(@Store_1,tspan,Xo,[],u1,u2,T1);
     end
end
% Output
Store1 = [t X(:,1) X(:,2)];

% System of Equations
% dx1/dt = (u1 - x1)/T
% dx2/dt = ((u1*u2) - (x1*x2))/(T*x1)
function [dx_dt]= Store_1(t,x,u1,u2,T1)
% System of Differential Equations
dx_dt(1) = (u1 - x(1))/T1;
dx_dt(2) = ((u1*u2) -(x(1)*x(2)))/(T1*x(1));
% Collect  derivatives into a column vector
dx_dt = dx_dt';
return
0
Reply Sarah 7/2/2010 2:00:22 PM

"Sarah Halliday" <s.j.halliday@student.reading.ac.uk> wrote in message 
news:i0krdm$jro$1@fred.mathworks.com...
> Hi,
>
> I am a new MATLAB user and I am attempting to use the ODE45 solver to 
> solve a system of 2 1st order differential equations. However, I need to 
> vary the equation inputs, u1 and u2, at every time step.

So you're trying to solve your system of ODEs multiple times for various 
different values of u1 and u2 that you pass in as additional input 
arguments?

> I have attempted to do this by using a For loop and If statement to assign 
> constant value to the input variables u1 and u2 at each time step selected 
> from two vectors, U1 and U2.
> The code executes in MATLAB and generates outputs however the results do 
> not appear to be correct.
> If someone could review the code to check that my for and if statements 
> are correct it would be greatly appreciated.
> Thank you for your time,
> Sarah
>
> function [Store1] = ModelS1(Inputs, T1, b, c)
> % Define Store input time series U1 = Inputs(:,1);
> U2 = Inputs(:,2);
> % Set the initial conditions Xo = [b;c];
> % Define the time span over which to execute the model tspan = 0:1:99;
> % Implementation of u1 and u2 within the model
> n = length (tspan);

This looks off to me.  You shouldn't care about the length of the tspan 
vector here; you should care about how many pairs of u1 and u2 parameters 
you have to process.

for whichparams = 1:size(Inputs, 1)
    u1 = Inputs(whichparam, 1);
    u2 = Inputs(whichparam,  2);
    % I would also suggest using an anonymous function
    [t, X] = ode45(@(t, y) Store_1(t, y, u1, u2), tspan, Xo);
    % Do something with this parameter value's t and X output from ODE45
end

If instead you're using u1 and u2 as time-varying parameters, so your 
equations are:

y' = f(t, y(t), u1(t), u2(t))

then you will need to pass ALL of U1 and U2 into your ODE function so you 
can interpolate it for time values at which you don't have an actual value. 
Use INTERP1 for this.

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 


0
Reply slord (13273) 7/2/2010 2:14:57 PM


Thanks Steve, treating u1 and u2 as time-varying parameters has sorted out the probelm. 
0
Reply Sarah 7/9/2010 3:06:06 PM

"Sarah Halliday" wrote in message <i17dsu$h5l$1@fred.mathworks.com>...
> Thanks Steve, treating u1 and u2 as time-varying parameters has sorted out the probelm. 
i have vectors u1 and u2 in hand .
can you please elaborate on the following two issues
1.how to pass u1 and u2 to ode45 
2.how to handle u1 and u2 inside the function
0
Reply ravichandra_b 9/19/2011 7:23:10 AM

3 Replies
1003 Views

(page loaded in 0.089 seconds)

Similiar Articles:













7/22/2012 10:06:32 PM


Reply: