There is nothing special about 6 - it is just the last number in your sequence. It will be the value of num in the function call; you don't need the last else{}.
I have absolutely no idea why you need the extra ComplexVector.
You must be very careful with whether it is n, n-1 or n+1 in your recursion formula.
Your original post (I'm having to assume that was correct) said
f(n+1) = [ (2n+4n i) / (7+5n
2 i) ] * f(n)
(and I coded this directly in my earlier non-recursive code).
This is equivalent to (on replacing n by n-1):
f(n) = [ (2(n-1)+4(n-1) i) / (7+5(n-1)
2 i) ] * f(n-1)
which it needs to be written as for the correct recursion. This is what I have coded below.
At the moment output goes to stream cout. If you want any other then, with a recursive code, you would have to pass the stream reference as a function argument (as @mbozzi does in the Coliru link) or make it a global variable.
This is the nearest attempt I can get to your function. Since you won't tell us your original problem we are having to second-guess it from non-working code. The code below produces the same numbers (less prettily formatted) as my earlier non-recursive code.
Our codes show just ideas for you to consider. You will have to write your own code for your own particular problem. I'll stand by my original contention that recursive functions just don't seem right for this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <complex>
using namespace std;
#define Complex complex<double> // so that I can try to mimic your classes
Complex recursive_equation( int n )
{
Complex b;
if ( n == 1 )
{
b = Complex( 1.0, 1.0 );
}
else
{
b = Complex( 2.0*(n-1), 4.0*(n-1) ) / Complex ( 7.0 , 5.0*(n-1)*(n-1) ); // BE CAREFUL!!! n -> n-1
b = b * recursive_equation( n - 1 );
}
cout << b << endl;
return b;
}
int main()
{
recursive_equation( 6 );
}
|
(1,1)
(0.216216,0.702703)
(0.128092,0.28267)
(0.0612953,0.0678346)
(0.018252,0.00903443)
(0.0036325,0.000188769) |