Help - Search - Members - Calendar
Full Version: Who speaks C?
914World.com > The 914 Forums > 914World Garage
MattR
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...
MattR
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
john rogers
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

MattR
Yeah, I'm using GCC compiler... sorry, i shoulda included that.
bd1308
ssshhhhheeeeeeeeeeett..

i forgot about that.

random positive integers are assigned to rand numbers, and "int" assigned vars.

b
McMark
While vs. while

confused24.gif
MattR
QUOTE (McMark @ Jan 22 2006, 09:17 PM)
While vs. while

confused24.gif

omg........

that did it


ar15.gif ar15.gif ar15.gif

i also changed int to float

MattR
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"?
john rogers
Check your output formatting.
MattR
I'm getting the error in the "sqrt" function.

I guess I'll just have to play with it some more... thanks guys!
r_towle
out of visual studio

While (x<=1);

made me add a semi colon
bryanc
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.
McMark
I bet NaN is "not a number". wink.gif
MattR
QUOTE (McMark @ Jan 22 2006, 10:11 PM)
I bet NaN is "not a number". wink.gif

Yeah. The problem is with that sqrt function. I dont know how it works.. the program works fine when I take out the squareroot thing. I'll ask a TA tomorrow or something... I'm tired of messing with it tonight.

Time to study circuits... damn I hate electrical engineering classes sad.gif
Hammy
blink.gif ...
Dave_Darling
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
reverie
Welcome to the club! wavey.gif
jasons
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.

agree.gif

You are using int types and division. When you divide int's that should result in fractions, you will truncate the fraction.
sqrt probably will not work well with int's either. Its doing some kind of series division itself. Use floats or doubles.

Example:

3/5 will result in 0
5/3 will in 1

Also, are you sure about the precedence of this operation... J=2/n*f; ?

/ and * have the same precedence so your they will operate left to right. Your result is this J=(2/n)*f;
If you want this J=2/(n*f); You need to use ()'s


bd1308
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.... yawn.gif


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
Root_Werks
ohmy.gif Wow, it has been a few years for me since I have even looked at a printf statement.
fiid
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.


MattR
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!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.