Right, so I have to devise a program to numerically integrate cos(x)/sqrt(x) numerically and I have no idea how to do it... I know I need to use the trapezium rule, but obviously I will have an infinite number of trapeziums. How do I go about this?
13 replies to this topic
#1
Posted 23 March 2011 - 04:27 PM
|
|
|
#2
Posted 23 March 2011 - 04:43 PM
Do it until the total value stops changing.
#3
Posted 24 March 2011 - 05:20 AM
OK, I understand... I'm not sure how though?
#4
Posted 24 March 2011 - 07:29 AM
Ok, so I had a go at it and it keeps crashing...
I assume it's because the loop won't stop?
#include<stdio.h>
#include<math.h>
int main()
{
float x;
float y;
float z;
float a;
float h;
printf("Enter trapezium height 'h':");
scanf("%f", h);
x=0;
a=0;
do
{
y=(cos(x)/sqrt(x));
if (x!=0)
{
a=a+(0.5*h*(y+z));
}
(z=y);
(x=x+h);
}
while (z!=cos(x)/sqrt(x));
printf("integral between 0 and infinity=%f", a);
}
I assume it's because the loop won't stop?
Edited by ljl22, 24 March 2011 - 01:01 PM.
#5
Posted 24 March 2011 - 11:06 AM
scanf("%f", [COLOR="red"]&[/COLOR]h);
#6
Posted 24 March 2011 - 01:04 PM
OK thanks, it's still not doing anything though. Will the program take a long time to spit out an answer?
#7
Posted 24 March 2011 - 01:08 PM
#include<stdio.h>
#include<math.h>
#define PRECISION 0.001
int main()
{
float x;
float y;
float z;
float a;
float h;
printf("Enter trapezium height 'h':");
scanf("%f", &h);
x=0;
a=0;
do
{
y=(cos(x)/sqrt(x));
if (x!=0)
{
a=a+(0.5*h*(y+z));
}
(z=y);
(x=x+h);
}
while (z!=cos(x)/sqrt(x) - PRECISION && z>cos(x)/sqrt(x) + PRECISION);
printf("integral between 0 and infinity=%f", a);
}
of course it would be better to avoid calculating cos(x)/sqrt(x) twice
#8
Posted 24 March 2011 - 01:24 PM
Thanks again--it works, sort of. All it returns is 1.#INF00, no matter what number I enter
I'm not sure what this means. Also I'm not sure why the precision thing is needed?
I'm not sure what this means. Also I'm not sure why the precision thing is needed?
#9
Posted 24 March 2011 - 01:27 PM
I have no idea on how to fix the integral sorry , the last integral I've ever calculated was about 2 years ago.. :D
The precision thing is needed because two floats will never be EXACTLY the same. So, you should never compare two floats (or doubles).
The precision thing is needed because two floats will never be EXACTLY the same. So, you should never compare two floats (or doubles).
#10
Posted 24 March 2011 - 01:31 PM
Right OK thanks I understand that now. Cheers for the help
#11
Posted 24 March 2011 - 01:33 PM
ljl22 said:
Right OK thanks I understand that now. Cheers for the help
you'll offer me a beer when you visit Rome :D
#12
Posted 24 March 2011 - 01:37 PM
Yep, might be a bit of a wait though...
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









