I'm trying to finish up some homework. Its not last minute (due tomorrow night), but I'm stuck. Britt and I have been trying to debug for a little while but with no luck.
The problem:
I'm trying to solve the integral 2*sqrt(1-x*x) from -1 to 1 using the trapezoidal rule. Basically I have to solve the equation:
h[f(x1) + f(x2) + ... + f(x n-1)] where x1, x2, etc. is the function evaluated at the point x1, x2, etc. along the x axis. the distance between x1 and x2 is a-b/n, which is the distance of the interval. I will need to input the number of intervals and be given an approximation for the function.
Here is my code;
#include
#include
int main(void)
{
int n;
int f=0;
int J;
int x=-1;
printf("Please enter the number of subdivisions n:");
scanf("%d",&n);
While (x<=1)
{
x = x+2/n;
f = f+2*sqrt(1-x*x);
}
J=2/n*f;
printf("The approximate integration by Trapezoidal Rule is: %d \n",J);
return 0;
}
Does anybody see anything thats glaring? I'm getting an error in line 23 (after the While (x<=1) that wont stop...
error codes:
trapezoidal4.c: In function `main':
trapezoidal4.c:23: error: parse error before '{' token
trapezoidal4.c: At top level:
trapezoidal4.c:28: error: `n' undeclared here (not in a function)
trapezoidal4.c:28: error: `f' undeclared here (not in a function)
trapezoidal4.c:28: warning: data definition has no type or storage class
trapezoidal4.c:30: error: parse error before string constant
trapezoidal4.c:30: warning: conflicting types for built-in function `printf'
trapezoidal4.c:30: warning: data definition has no type or storage class
Well, you did not say which compiler you are using and just by looking at the code when declaring an "int" that usually means an unsigned integer which has a range of 0 to 32767, so the negative 1 won't work. It has been a while since I did any integrals in c++ but here is a reference page that might help?
http://okmij.org/ftp/packages.html
Yeah, I'm using GCC compiler... sorry, i shoulda included that.
ssshhhhheeeeeeeeeeett..
i forgot about that.
random positive integers are assigned to rand numbers, and "int" assigned vars.
b
While vs. while
QUOTE (McMark @ Jan 22 2006, 09:17 PM) |
While vs. while |
Okay, now I dont get any errors in GCC
but when I run the program, I enter the number of subdivisions and the result I get back is "NaN"
The program looks like this:
Please enter the number of subdivisions n:1000
The approximate integration by Trapezoidal Rule is: NaN
Which really confuses me. The result should be J (which is 2/n * f). What is "NaN"?
Check your output formatting.
I'm getting the error in the "sqrt" function.
I guess I'll just have to play with it some more... thanks guys!
out of visual studio
While (x<=1);
made me add a semi colon
For this problem, all of the variables should be either double or float. for example, x will be -1, 0, 1.
Oh yeah, and change the %d in the printf to %f.
I bet NaN is "not a number".
QUOTE (McMark @ Jan 22 2006, 10:11 PM) |
I bet NaN is "not a number". |
...
Like they said, check your argument types and return types.... Those are the most common errors with using library functions. Did you remember to include math.h or wherever the sqrt function comes from?
--DD
Welcome to the club!
QUOTE (bryanc @ Jan 22 2006, 09:36 PM) |
For this problem, all of the variables should be either double or float. for example, x will be -1, 0, 1. Oh yeah, and change the %d in the printf to %f. |
doh!
that was the first thing that popped into my mind when i looked at your program this morning.
W
sorry buddy, I wished I could have helped more, but I was tyring to read this Edgar Allan Poe story....
dang it...oh well... i'm glad you got most of the errors down.
one thing: you needed to watch out for the conflicting numerical output types.
I believe you got the sqrt() function down correctly, but something doesnt like being square-rooted.
anyway...if you need more help, lemme knwo.
b
Wow, it has been a few years for me since I have even looked at a printf statement.
I think you need to use printf("%f\n"... instead of %d. %d is for various integer types.
I would also stick to using lower case for variable names (actually in C - I think convention is to use lower case for EVERYTHING except #define statements. You can use camelCase for varible names: example: incrementCounter.
You can do a ton of formatting conversions on the $d - i.e. if you only want 5 decimal places you can do %.5d
It's also possible to get scientific notation out of it. If you're on a unix box try "man 3 printf" and it will describe it all for you.
Okay guys,
Ive fixed a BUNCH of the problems you've pointed out. I also spoke with my professor this morning and he pointed out the bounds of integration. I was taking the square root of a negative number because my upper bound was 1+2/n. I'm going to fix it tonight after work.
Thanks so much!
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)