Some trouble with difference equations

Hey,
I'm not a heavy matlab user and started programming on ml this weak...
I've got the following problem (whicht i can't solve despite heavy use of google):
I got the following equation system which i want to solve nummeric over time
r(t)=-a_r-b_r*y(t-1)-c_r*ir(t-1)-d_r*lamda(t-1)+r(t-1)
y(t)=a_y+b_y*(ir(t)-pi(t))+c_y*r(t)
pi(t)=a_pi+b_pi*y(t)+c_pi*r(t)
ir(t)=a_i+b_i*y(t)+c_i*pi(t)+d_i*r(t)+e_i*lamda(t)
lamda(t)=a_l+b_l*y(t)+c_l*ir(t)+d_l*pi(t)
the variables indicated with t are the one of interest and start values are given...
the variables a,b,c,d,e are constant coefficients given as well... The problem is to solve for one period and then in the next step for arbitraly many.
I have tried with loops (the problem was to integrate the break condition properly) and fsolve (here i failed to add time index) but came to no conclusion.
I would be very gratefully for any help
lg

2 Comments

What are your starting values for r, y, etc.?
What is your criterion for stopping? Is it a fixed number of time intervals, e.g., 0, dt, 2*dt, ..., n*dt - or perhaps convergence?

Answers (1)

Please try:
% Number of time steps:
M = 200;
% Number of state variables:
N = 5;
% Pre-allocate data array:
x = zeros(M,N);
% Coefficients for difference equations:
a = [ ... ];
b = [ ... ];
c = [ ... ];
d = [ ... ];
% List of names for the state variables:
varNames = { 'r' 'y' 'pi' 'ir' 'lambda' };
% Time increment:
dt = ... ; % <---- replace ... with desired value
% Time domain:
t = dt*(0:M-1)';
% Initial values:
x(1,:) = [ ... ]; % <---- replace ... with five initial values
% Main Loop - update state variables using difference equations:
for k = 2:M
x(k,1) = ... ;
x(k,2) = ... ;
x(k,3) = ... ;
...
...
end
% Plot results:
figure;
plot(t,x(:,1));
title(varNames{1});
HTH.

6 Comments

what is meant
dt = ... ;
t = dt*(0:M-1)'; here (derivatives of all equations)
has t, dt to be defined in advance?
lg
I have marked up the code, and included some additional code, in my answer (see above). HTH.
Anywhere that you see "..." in the code above is a place where you need to put a numerical value or an expression to compute a result.
dt = 0.03817; %your time increment in seconds
t would be calculated from dt, which in Rick's code would be given in advance. Rick has given a variable for the number of time steps to do, M, which would also be given in advance for his code.
It is not strictly necessary to define the number of time steps in advance, and it is not strictly necessary to use the same time step each time. You will need an _initial_ time step in order to calculate the value at the beginning of the second period. You can vary the time step as you go.
Memory use in the program can be more efficient if you can at least estimate the number of time steps you will be doing. But that is efficiency, not a requirement.
What you will need is a way to judge whether to stop or not. In Rick's code, that judgment is based upon the number of time steps done. In your code, the judgment could be based upon (say) the number of times that '#matlab' gets tweeted. Whatever seems appropriate.
Yes, Walter makes several good points. The code I posted is meant to provide a template that you can use as a starting point. Once you get something that works, you can tweak the code for speed, memory, precision, convergence rate, etc.
Additional note that becomes obvious once you know it:
current time (t) is always the initial time plus the sum of the time steps that have been taken.

This question is closed.

Asked:

on 15 Feb 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!