Jump to content

Numerical integration to infinity

- - - - -

  • Please log in to reply
13 replies to this topic

#1
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
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?

#2
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Do it until the total value stops changing.

#3
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
OK, I understand... I'm not sure how though?

#4
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok, so I had a go at it and it keeps crashing...

#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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
scanf("%f", [COLOR="red"]&[/COLOR]h);


#6
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
OK thanks, it's still not doing anything though. Will the program take a long time to spit out an answer?

#7
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
#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
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
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?

#9
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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).

#10
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
Right OK thanks I understand that now. Cheers for the help

#11
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

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
ljl22

ljl22

    Newbie

  • Members
  • PipPip
  • 20 posts
Yep, might be a bit of a wait though...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users