How do I write this recursive equation? Do I need pointers? could someone please hlep

Have to write this recursive equation, anybody can help... how to write in c++

A(t+1) = K0*[1-exp(S)]+ A(t)*exp(S)

could you please help? got problems writing the same in C++...
You supposedly know about functions and recursion? That a function can call itself?

You have this A(x) = A(x-1) * a + b. Where does that end? A(1), A(0), or A(-inf)?
i would say to make it for like some 10 iterations. I understand about functions and about its time delay properties...but that is the point....how must I get this get this done?

in matlab it would go like this,

for t= 1:n
{
A(t+1) = K0*[1-exp(S)]+ A(t)*exp(S)
}


how abt in C++?
There must be something to end the recursion. If you do have
F(t+1) = a + b * F(t)
then
F(t+1)
= a + b * (a + b * F(t-1))
= a + b * (a + b * (a + b * F(t-2)))
= a + b * (a + b * (a + b * (a + b * F(t-3))))
= ...

There must be such x where F(x) does not depend on F(x-1), or the recursion is infinite.
How does Matlab do that?
okay...am wrting the code for you in MATLAB...in case you can....please help me in C++...

S = 0.7;
K0= 0.6;

endpoint = 20;
A = zeros(1, 20);
A(1) = 1;

for t=1:endpoint
A(t+1) = K0*[1-exp(S)]+ A(t)*exp(S)
end

so now i get the values of A...at each timepoint till 20 iterations...
and the eqaution must go like this

A(t+1) = K0*[1-exp(S)]+ A(t)*exp(S)
A(t+2) = K0*[1-exp(S)]+ A(t+1)*exp(S)
A(t+3) = K0*[1-exp(S)]+ A(t+2)*exp(S) .....so on....
A(1) = 1;

This is it.

However, a recursion is not what you need. Recursion would give you A(20) without explicitly calculating the previous values. Sure, you can use a recursive function to fill a series, but that is a waste of time.

What you actually want is an array of values. Your Matlab starts indexing from 1, but C++ likes 0.
1
2
3
4
5
6
7
8
const size_t Size = 20;
double A[Size] {1.0};
const double es {exp(0.7)};
const double k {0.6 * (1.0 - es)};
for ( size_t i = 1; i < Size; ++i )
{
  A[i] = k + es * A[i-1];
}

Disclaimer: untested.
Topic archived. No new replies allowed.