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
답변 (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)
B = cumsum(A)
Or if you're not allowed to use cumsum, use a for loop and index into your variable.
댓글 수: 4
Salomon Roth
2021년 3월 13일
Walter Roberson
2021년 3월 13일
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.
Salomon Roth
2021년 3월 14일
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)
P(t,beta); %initialize
P1 = P(1)
P2 = P(2)
P10 = P(10)
release(P10)
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
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
