Function containing symbolic, numeric and previous (period) information

I have a relatively simple expression that is a combination of symbolic and numeric expressions that also uses information from the previous period starting in the second period. I would like to define a function that can be used to create this variable, since it has the same structure from the second period for all following periods. In the end, parts of the parameters of this function are to be optimized and therefore it would be great to have an expression that can be used in an optimization procedure.
I am fairly new to Matlab and am wondering what is the best way to go about this.
The initial condition is:
t = sym("t");
beta = randn(1,1);
P1 = t * beta
From the second period onwards, the information from the previous period is included:
P2 = t * beta + P1
P3 = t * beta + P2

Answers (1)

Don't make variables named P1, P2, P3, etc. If you had a million time steps would you want to have a million variables in the workspace?
Instead, make one variable named P. If you do this, the cumsum function will be of use.
A = randi(10, 1, 10)
A = 1×10
3 1 2 10 9 10 5 4 7 9
B = cumsum(A)
B = 1×10
3 4 6 16 25 35 40 44 51 60
Or if you're not allowed to use cumsum, use a for loop and index into your variable.

4 Comments

Thank you very much for your reply. Unfortunately, I have to admit that I can't quite see how this helps me with my problem.
syms n
P(n) = t*beta*n;
Then P(1) = t*beta, and P(2) is t*beta larger so it is P(1)+t*beta and so on.
@Walter Roberson Thanks for your reply. Is there no general form to specify a function in a way such as P(n) = t * beta + P(n-1)?
Not if you proceed symbolically. You cannot do recursion symbolically -- at least not without dipping into the details of the internal language used for symbolic computation (and if you did that you would end up with something that the MATLAB interface could not display.)
You could work with a true function
t = sym("t");
beta = round(randn(1,1),1)
beta = 0.9000
P(t,beta); %initialize
P1 = P(1)
P1 = 
P2 = P(2)
P2 = 
P10 = P(10)
P10 = 
release(P10)
ans = 
function Pn = P(varargin)
persistent t beta
syms t
n = varargin{1};
if nargin == 2
t = varargin{1}; beta = varargin{2};
Pn = 0;
elseif n == 0
Pn = 0;
elseif n == 1
Pn = int(t*beta, t, 0, 1, 'hold', true);
else
Pn = int(t*beta, t, 0, 1, 'hold', true) + int(P(n-1), t, 0, 1, 'hold', true);
end
end
The integration that shows up there is a taking advantage of a pretty new (R2019b) way of "holding" a symbolic calculation. There is no general way to express something like x + f(x-1) without having the f(x-1) fully evaluated. But there happens to be a way to postpone integration. So we can cheat on holding calculations by phrasing the calculations in terms of an equivalent integration. For any calculation f(x) we can hold that by expressing it as int(f(x),SOMEOTHERVARIABLE,0,1) marking the integration to hold the calculation until it is released, at which point f(x)*SOMEVARIABLE would be calculated and evaluated at SOMEVARIABLE=1 and SOMEVARIABLE=0 and subtracted, giving you a net of f(x), except with the f(x) calculation having been delayed.
This is sort of like you asking for P(10) and being told it was equal to (beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t)+(beta*t) without the beta*t being calculated out

Sign in to comment.

Products

Asked:

on 13 Mar 2021

Commented:

on 15 Mar 2021

Community Treasure Hunt

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

Start Hunting!