|
-
March 18th, 2009, 02:55 PM
#1
writing a formula with powers and trig in C++ borland builder 4
Hi,
dont really know much about programming or anything like this.
But basicly in my program i am trying to write the following formula:

(linked formula as a picture to keep its format)
basicly does anyone have any ideas how i would write this?
my guess was Y=(sin((acos(x))^(2/N)))^(2/N);
but i dont think ive done the powers right, ive read somewere that you have to use pow(i,j) for powers?
any help would be apreciated, thanks
-
March 18th, 2009, 03:55 PM
#2
Re: writing a formula with powers and trig in C++ borland builder 4
Yes, operator ^ means XOR. For power there is a library function called pow(), that you can find in <cmath>.
-
March 18th, 2009, 04:11 PM
#3
Re: writing a formula with powers and trig in C++ borland builder 4
i had a go using the pow notation,
Y = pow(sin(pow(acosx,2/N),2/N);
does that look right at all?
-
March 18th, 2009, 04:14 PM
#4
Re: writing a formula with powers and trig in C++ borland builder 4
There is something wrong there. You open 3 parenthesis and close 2.
Code:
y = pow(sin(pow(acos(x), 2/N)), 2/N);
-
March 18th, 2009, 04:21 PM
#5
Re: writing a formula with powers and trig in C++ borland builder 4
yea sorry typo
* Y=pow(sin(pow(acos(x), 2/N)), 2/N);
there u go, does this look like it might work though now?
-
March 18th, 2009, 04:32 PM
#6
Re: writing a formula with powers and trig in C++ borland builder 4
-
March 18th, 2009, 04:34 PM
#7
Re: writing a formula with powers and trig in C++ borland builder 4
can't, lol dont have the program,
will try it tommorrow when i can get to the computer labs.
Thanks for your help bud
-
March 18th, 2009, 06:12 PM
#8
Re: writing a formula with powers and trig in C++ borland builder 4
 Originally Posted by benbridle38
can't, lol dont have the program,
will try it tommorrow when i can get to the computer labs.
Download and install a C or C++ compiler, as the case may be. Search for GCC and/or MSVC.
-
March 18th, 2009, 06:17 PM
#9
Re: writing a formula with powers and trig in C++ borland builder 4
Well,
You are writing a higher-level code such as Mathematica or Maple code.
C++ code has a different syntax.
Guru cilu really pointed you in the right direction.
If his hints weren't enough, then take a look at the sample shown below.
Try to understand everything that I have written, but within a C++ context.
Sincerely, Chris.
Code:
#include <iostream>
#include <cmath>
double trig_function(const double x, const unsigned int N)
{
static const double two_pi = 2.0 * 3.141592653589793238462643383;
if(x > two_pi)
{
return 0.0;
}
const double two_over_n = 2.0 / static_cast<double>(N);
const double sin_acos_term = sin(acos(x));
return pow(sin_acos_term, two_over_n);
}
int main(void)
{
std::cout << trig_function(0.5, 3) << std::endl;
}
Last edited by dude_1967; March 18th, 2009 at 06:18 PM.
Reason: details, details...
You're gonna go blind staring into that box all day.
-
March 19th, 2009, 06:13 AM
#10
Re: writing a formula with powers and trig in C++ borland builder 4
oky, really really stuck now,
this is my code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
for(float N=0.5;N<10;N=N+0.5)
{
for(int Z=0;Z<=1000000;Z++)
{
x=random(10000)*0.0001;
y=random(10000)*0.0001;
Y=pow(sin(pow(acos(x), 2.0/N)), 2.0/N);
if(y<=Y)
{
P=P+1;
}
}
A=P/250000.0;
cout <<"For value of N = "<<N<<" Area eclosed A = "<<A<<endl;
}
getch();
return 0;
}
trying to run on c++ borland, and it doesn't run.
any ideas?
-
March 19th, 2009, 06:42 AM
#11
Re: writing a formula with powers and trig in C++ borland builder 4
 Originally Posted by benbridle38
trying to run on c++ borland, and it doesn't run.
any ideas?
Is there something wrong with your compiler? A program should run after it's been compiled properly, but I don't see how that program could be compiled as it's missing some variable definitions among other things.
-
March 19th, 2009, 04:34 PM
#12
Re: writing a formula with powers and trig in C++ borland builder 4
 Originally Posted by benbridle38
trying to run on c++ borland, and it doesn't run.
any ideas?
Yes Ben, it looks like there might be various slight points of confusion here.
Are you sure that the code compiled? I do not know if the function random(...) is actually defined anywhere. Furthermore your code snippet contains the use of many variables which are undefined.
It looks like you are simply trying to do some kind of a standard assignment which uses random numbers to calculate the area under some kind of curve.
You seem to be getting off to a rough start. Try some of these suggestions.
- Make sure all of your variables are properly defined and initialized.
- Write the loop over N using integers.
- Make the loop over Z much smaller during the development phase allowing for code-verification with a short run-time. As soon as the algorithm functions properly, extend to the full length calculation, increasing by successive factors of 10 so you can see how the run-time develops with increasing order of magnitude of the loop.
Chris.
You're gonna go blind staring into that box all day.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|