Nov 21, 2013 at 11:39pm UTC
I am trying write code which compute a recursive function, which is used in another function.
the recursive function is.
V[n_] = 2 Cos[(2*\[Pi]*k)/205]*V[n - 1] - V[n - 2] +
sin(1000*2*pi*1/8000 * n) // N
What i've tried until now is this. V[0] and V[-1] is 0.
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
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
void Vk(int a)
{
i
double k = floor((double ) 1000/8000*205);
double vk = 2*cos((2*(22/7)* k)/205) * Vk(a-1)*Vk(a - 2)+sin((double ) 1000*(2*(22/7))*1/8000*a);
return vk;
}
int _tmain(int argc, _TCHAR* argv[])
{
Vk(0) = 0;
Vk(-1) = 0;
return 0;
}
But it wont work.
Last edited on Nov 21, 2013 at 11:44pm UTC
Nov 21, 2013 at 11:52pm UTC
Describe the "won't work" in more detail. I bet your compiler does point some reasons why it does not accept your syntax.
What does your function return? Hint: see line 9.
You must test within the function whether 'a' is 0 or -1, and act accordingly.
Nov 21, 2013 at 11:54pm UTC
i doesn't appear to be set to anything.
you are also trying to return a value from a void function.
Maybe try switching it to
double Vk(double a)
you are going to want to put a .0 after each of the numbers also so the compiler won't treat them as integers.
Last edited on Nov 21, 2013 at 11:58pm UTC
Nov 21, 2013 at 11:56pm UTC
You are also doing integer division. so probably get a 0 every time.
Last edited on Nov 21, 2013 at 11:57pm UTC
Nov 22, 2013 at 12:11am UTC
The function has to be called recursivly.. So if I in my main write vk(5) it should
cout vk(5).
Vk(0) = 0;
Vk(-1) = 0;
are my initial value declaration...
I would think the way i've declared them is wrong, but how else should i declare them?
Last edited on Nov 22, 2013 at 12:15am UTC
Nov 22, 2013 at 12:16am UTC
Did you read garions post? You are trying to return a double when it the return type is set to void change it to a double. Then I mentioned earlier you are doing integer division so you are more than likely to get 0 when dividing by 8000+ too lazy to do the math by hand but just a . or .0 or .f or .0f at the end or cast them to double/float
Nov 22, 2013 at 7:47am UTC
Yes.. I typecasted all my variables Double. and added the. .0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
double Vk(int a)
{
double k = floor((double ) 1000.0/8000.0*205.0);
double vk = 2.0*cos((2.0*(22.0/7.0)* k)/205.0) * Vk(a-1)*Vk(a - 2.0)+sin((double ) 1000.0*(2.0*(22.0/7.0))*1.0/8000.0*a);
return vk;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Last edited on Nov 22, 2013 at 7:48am UTC
Nov 22, 2013 at 2:26pm UTC
As it is now the function don't know what it should return for a == 0, or -1.
I tried writing my initial declaration in my function, but I get an error during runtime, which say stackoverflow.
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
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <cmath>
using namespace std;
double Vk(int a)
{
Vk(0) == 0;
Vk(-1) == 0;
double k = floor((double ) 1000.0/8000.0*205.0);
double vk = 2.0*cos((2.0*(22.0/7.0)* k)/205.0) * Vk(a-1)*Vk(a - 2.0)+sin((double ) 1000.0*(2.0*(22.0/7.0))*1.0/8000.0*a);
return vk;
}
int _tmain(int argc, _TCHAR* argv[])
{
Vk(5);
system("pause" );
return 0;
}
Visual studio 2010 needs, STDafx.h to compile, so i can't remove that. windows.h was added so i could use Sleep for debugging.
Last edited on Nov 22, 2013 at 2:30pm UTC
Nov 22, 2013 at 3:17pm UTC
Q. How is expression Vk(0) == 0;
evaluated?
A. First the function Vk is called with parameter 0. The return value of the function call is compared against 0. The result of comparison is true
or false
.
The problem should be obvious: the called function has this same expression, which calls function, which calls function, which ...
That is an endless recursion.
Returning 0 from function is simple: return 0.0;
When to return 0? if ( a < 1 )
Nov 22, 2013 at 7:20pm UTC
Ahh.. yeah it makes sense....
Last edited on Nov 22, 2013 at 7:27pm UTC