Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
recursion on function defined as nested integral

Subject: recursion on function defined as nested integral

From: Juan Miguel

Date: 18 Mar, 2013 16:15:06

Message: 1 of 2

suppose I have a positive real number T, and I a have a vector of parameters "a" with N elements, and a function f(k,t) on t defined recursively in k, for k<N as follows

(the following is NOT matlab code, it is just pseudo code to illustrate what I need to do, since i am not sure what Matlab data structure to use for the function)

f(0,t) = 1 a constant for all t

f(1,t) = @(t) integral( @(s) exp(-a(1)*s),t,T);
f(2,t) = @(t) integral( @(s) f(1,s)*exp(-a(2)*s),t,T);
.
.
.
f(k,t) =@(t) integral( @(s) f(k-1,s)*exp(-a(k-1)*s),t,T);

So if I knew what value "k" is beforehand I can just probably write everything down from 0 to k, maybe the f's are functions stored as a cell array in 'k'.

But I want to generalize this so that I compute f(k,t) for various "k" without having to write everything down. Is this possible? I may want to go for k up to say 50, ultimately I want to be able to go to k in the order of hundreds. Is it better to do this not with anonymous functions perhaps write a matlab function which calls itself recursively? or with f eval and a for lop that writes the entire string of commands for all the steps from 1 to k, with variable k?

Should I do this in another programming language, that is, would Matlab have fundamental limitations that could make such computation difficult? (and if so what could be the programming language that handles this best).

Subject: recursion on function defined as nested integral

From: Roger Stafford

Date: 22 Mar, 2013 22:08:14

Message: 2 of 2

"Juan Miguel" wrote in message <ki7eia$2h7$1@newscl01ah.mathworks.com>...
> suppose I have a positive real number T, and I a have a vector of parameters "a" with N elements, and a function f(k,t) on t defined recursively in k, for k<N as follows
>
> (the following is NOT matlab code, it is just pseudo code to illustrate what I need to do, since i am not sure what Matlab data structure to use for the function)
>
> f(0,t) = 1 a constant for all t
>
> f(1,t) = @(t) integral( @(s) exp(-a(1)*s),t,T);
> f(2,t) = @(t) integral( @(s) f(1,s)*exp(-a(2)*s),t,T);
> .
> .
> .
> f(k,t) =@(t) integral( @(s) f(k-1,s)*exp(-a(k-1)*s),t,T);
> ........
- - - - - - - -
  It isn't necessary to do iteration (recursion) to find your f(n,t) for a given n. It can be expressed directly as a linear combination of n+1 exponential functions. The following matlab code evaluates numerically the necessary parameters for such an expression.

- - - - - -
clear

% Let a be an n-element column vector
a = .05+.1*rand(10,1); % <-- Choose your 'a' vector

% Use a to compute the scalar b and n+1-element row vectors c and d.
n = size(a,1);
p = tril(reshape(1:n*(n+1),n,n+1));
p = p(p>0);
q = (n+1)*p-(n*(n+1)+1)*floor(p/(n+1));
M = cumsum(tril(repmat(a,1,n+1)));
c = M(n,:);
b = c(1);
M(q) = M(p);
d = prod(M).*((-1).^(0:n));

% Let T be a scalar and t an m-element vector
T = rand;
m = 100;
t = T-linspace(1,2,m);

% Compute the corresponding values of f(n,t) and plot it.
fn = zeros(size(t));
for ix = 1:m
 fn(ix) = exp(-b*T)*sum(exp(c*(T-t(ix)))./d);
end
plot(t,fn,'y-') % <-- Plot of f(n,t) versus t
- - - - - -

  For example the corresponding expression for f(3,t) would be this:

 f(3,t) = exp(-(a1+a2+a3)*T) * ...
  ( +exp((a1+a2+a3)*(T-t))/a1/(a1+a2)/(a1+a2+a3) ...
    -exp((a2+a3)*(T-t))/a1/a2/(a2+a3) ...
    +exp(a3*(T-t))/a2/(a1+a2)/a3 ...
    -1/a3/(a2+a3)/(a1+a2+a3) )

where a1 = a(1), a2 = a(2), and a3 = a(3).

  It is only fair to warn you that with a large value for n, even with double precision floating point accuracy, these numerical computations may encounter serious relative round off errors for values of t near T since it involves a linear combination of sizable exponential quantities whose sum should cancel out to nearly zero. The only remedy I can see for that is to use higher precision packages from the file exchange to do the equivalent of the above algorithm with the appropriate accuracy. If you try to carry this out using the symbolic toolbox, the expressions would have an order O(n^3) character-length which means that if n is in the hundreds, the expression would have a character-length in the millions!

  One other warning for this algorithm is that no sequence of consecutive values from vector 'a' should have a sum of zero since that would yield an indeterminate ratio at some point which would result in a NaN value. That is, in the above example with n = 3, none of the values a1, a2, a3, a1+a2, a2+a3, and a1+a2+a3 can be zero.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us