Printable Version of Topic

Click here to view this topic in its original format

914World.com _ 914World Garage _ Who speaks C?

Posted by: MattR Jan 22 2006, 10:48 PM

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...

Posted by: MattR Jan 22 2006, 11:03 PM

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

Posted by: john rogers Jan 22 2006, 11:04 PM

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


Posted by: MattR Jan 22 2006, 11:11 PM

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

Posted by: bd1308 Jan 22 2006, 11:16 PM

ssshhhhheeeeeeeeeeett..

i forgot about that.

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

b

Posted by: McMark Jan 22 2006, 11:17 PM

While vs. while

confused24.gif

Posted by: MattR Jan 22 2006, 11:19 PM

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


Posted by: MattR Jan 22 2006, 11:21 PM

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"?

Posted by: john rogers Jan 22 2006, 11:24 PM

Check your output formatting.

Posted by: MattR Jan 22 2006, 11:26 PM

I'm getting the error in the "sqrt" function.

I guess I'll just have to play with it some more... thanks guys!

Posted by: r_towle Jan 22 2006, 11:29 PM

out of visual studio

While (x<=1);

made me add a semi colon

Posted by: bryanc Jan 22 2006, 11: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.

Posted by: McMark Jan 23 2006, 12:11 AM

I bet NaN is "not a number". wink.gif

Posted by: MattR Jan 23 2006, 12:14 AM

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

Posted by: Hammy Jan 23 2006, 01:11 AM

blink.gif ...

Posted by: Dave_Darling Jan 23 2006, 02:06 AM

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

Posted by: reverie Jan 23 2006, 05:23 AM

Welcome to the club! wavey.gif

Posted by: jasons Jan 23 2006, 07:55 AM

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



Posted by: bd1308 Jan 23 2006, 07:59 AM

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

Posted by: Root_Werks Jan 23 2006, 10:44 AM

ohmy.gif Wow, it has been a few years for me since I have even looked at a printf statement.

Posted by: fiid Jan 23 2006, 10:52 AM

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.



Posted by: MattR Jan 23 2006, 12:11 PM

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)