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: Changing Inputs per Time Step in ODE45 - comp.soft-sys.matlab ...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 ... ode45 smallest step size value - comp.soft-sys.matlabChanging Inputs per Time Step in ODE45 - comp.soft-sys.matlab ... ode45 smallest step size value - comp.soft-sys.matlab I am running ode45 nested within a for loop varying ... how convert sampled scope data into dBV/sqrt(Hz)? - comp.dsp ...Changing Inputs per Time Step in ODE45 - comp.soft-sys.matlab ... how convert sampled scope data into dBV/sqrt(Hz)? - comp.dsp ... If I have an analog signal input (mostly ... Laser rate equation solving using ODE45 - comp.soft-sys.matlab ...-Id is input bias current -e electron ... dont know how to apply this into ODE45 function. The time of every integration step ... and S will start from 0, by time change, N ... M-file PID control of mass-spring-damper - comp.soft-sys.matlab ...... rhsfun = @(t,p) sos(t,p); [t,p] = ode45(rhsfun,[0 sim_time],p0,options); figure('Name','System Response to Step Input ... Tpaps grid density change - comp.soft-sys ... ode23 - comp.soft-sys.matlab... forward in > time using a large time-step ... are for > > decreasing the time? How should I change my ... function in simulink for mutiple inputs and ... ode23, ode45 ... ODE with variable coefficients - comp.soft-sys.matlab... having a problem in getting MATLAB to change these coefficients at each time step. ... Using ode45 to solve transient response of ... comp.soft-sys.matlab... with Matrix Inputs ... Differential Equation with Matrix Inputs - comp.soft-sys.matlab ...... set up the code to solve for Mach number step ... of area at each point dA = 40x1 matrix of change ... equations for symbolic ..... which takes time and for a different input ... Help to 'pretty'; replace WITHIN file without TEMP file?? - comp ...... etc.) Currently I loop, changing one set of data. Move temp file into 'input' file for next step of ... awk95 doesn't seem to handle time ... Now, there is nothing per se ... ERROR Message in Proce report - comp.soft-sys.sasNOTE: The SAS System stopped processing this step ... NOTE: PROCEDURE REPORT used (Total process time ... the run-time version of the report and try to change the input ... Changing Inputs per Time Step in ODE45 - comp.soft-sys.matlab ...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 ... Changing Inputs per Time Step in ODE45 - Newsreader - MATLAB CentralHi, 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 ... 7/22/2012 10:06:32 PM
|