Jump to content

approximation of pi using trapezoidal rule

- - - - -

  • Please log in to reply
18 replies to this topic

#1
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Can someone give me some hints on how to do this?

Calculate pi:
-Use the formula given to calculate the numerical integration
-use the trapezoidal rule to write the program
-change the divisional number, and present the way the margin of error of the calculus numerical value changes. (if divisional number is multiplied by 2, 4, 8 times....show how the margin of error changes)

This is the formula given to calculate the numerical intergration
Attached File  pi.JPG   11.89K   15 downloads

Someone give me the guideline to do this. :(

Attached Files



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The summation is going to be implemented as a for loop. The values of x_i will need to be calculated, based on the number of x's you intend to use (they will range from x_0 to x_n with a distance of 2/n between them).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Gerg? Magyar

Gerg? Magyar

    Newbie

  • Members
  • PipPip
  • 22 posts
Try it:
#include <iostream>

#include <iomanip>


int main() {

  unsigned int decP;

  unsigned int denom=3;

  float ourPi=4.0f;

  bool addFlop=true;

  cout << "How many loop iterations? ";

  cin >> decP;

  for (unsigned int i=1;i<=decP;i++) {

    if (addFlop) {

      ourPi-=(4.0/denom);

      addFlop=false;

      denom+=2;

    } else {

      ourPi+=(4.0/denom);

      addFlop=true;

      denom+=2;

    }

  }

  cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(9);

  cout << "Pi calculated with " << decP << " iterations is: ";

  cout << ourPi << endl;

}


#4
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Gerg? Magyar said:

Try it:
#include <iostream>

#include <iomanip>


int main() {

  unsigned int decP;

  unsigned int denom=3;

  float ourPi=4.0f;

  bool addFlop=true;

  cout << "How many loop iterations? ";

  cin >> decP;

  for (unsigned int i=1;i<=decP;i++) {

    if (addFlop) {

      ourPi-=(4.0/denom);

      addFlop=false;

      denom+=2;

    } else {

      ourPi+=(4.0/denom);

      addFlop=true;

      denom+=2;

    }

  }

  cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(9);

  cout << "Pi calculated with " << decP << " iterations is: ";

  cout << ourPi << endl;

}

But I don't understand this format.I use <stdio.h>

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Are you using C or C++? cout is basically printf in C++.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Gerg? Magyar

Gerg? Magyar

    Newbie

  • Members
  • PipPip
  • 22 posts
I wrote to VS 2010.
I hope to this you thought.
#include "stdafx.h"

#include <iostream>

#include <conio.h>

#include <math.h>


using namespace std;


int main()

{

	double x=0;

	long double pi;

	double i;

	cout << " To how many terms? ";

	cin >> i;


	for (long int n = 2; n <= i; n++) { 

	  x += 1.0/(n*n);

	} 

	pi =sqrt(6*(1+x));

	cout << " Estimated PI value to " << i << " terms: "<< pi; 


	getch();

} 
Or this:
http://upload.wikime...45e9445becc.png

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
No. You are summing up 1/n^2, which has nothing to do with the function of interest.

Given that you are integrating sqrt(4-x^2), I would expect to see a reference to that SOMEWHERE in your code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

No. You are summing up 1/n^2, which has nothing to do with the function of interest.

Given that you are integrating sqrt(4-x^2), I would expect to see a reference to that SOMEWHERE in your code.

I don't learn about cout, I just learn how to use printf.
Exactly, how do I write the code for the integration of sqrt(4-x^2)??

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I would create a function called f that will accept a float x and return sqrt(4-x^2).

Do you understand what the summation formula means?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

I would create a function called f that will accept a float x and return sqrt(4-x^2).

Do you understand what the summation formula means?

Nope,I don't understand :(

#11
Gerg? Magyar

Gerg? Magyar

    Newbie

  • Members
  • PipPip
  • 22 posts
Read it:
http://educ.jmu.edu/.../more on pi.pdf
Pseudo code:
for m from 1 to 12 do

 for n from 1 to 12 do

  x:=’x’: a:=’a’: b:=’b’: c:=’c’:

  sol:=int(x^m*(1-x)^n*(a+b*x+c*x^2)/(1+x^2),x=0..1):

  out:=op(sol): eq1:=0: eq2:=0: eq3:=0:

  for i from 1 to nops(sol) do

   if type(out[i],Or(name,rational&*name,name&*rational)) then

    eq1:=eq1+out[i]:

    elif type(out[i]/Pi,Or(name,rational&*name,name&*rational)) then

    eq2:=eq2+out[i]/Pi:

   else

    eq3:=eq3+out[i]/log(2):

   end if;

  end do:

  assign(solve({eq1=355/113,eq2=-1,eq3=0})):

  assume(x>=0,x<=1); print(m,n,a,b,c,is(a+b*x+c*x^2>=0));

 end do:

end do:



#12
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Gerg? Magyar said:

Read it:
http://educ.jmu.edu/.../more on pi.pdf
Pseudo code:
for m from 1 to 12 do

 for n from 1 to 12 do

  x:=’x’: a:=’a’: b:=’b’: c:=’c’:

  sol:=int(x^m*(1-x)^n*(a+b*x+c*x^2)/(1+x^2),x=0..1):

  out:=op(sol): eq1:=0: eq2:=0: eq3:=0:

  for i from 1 to nops(sol) do

   if type(out[i],Or(name,rational&*name,name&*rational)) then

    eq1:=eq1+out[i]:

    elif type(out[i]/Pi,Or(name,rational&*name,name&*rational)) then

    eq2:=eq2+out[i]/Pi:

   else

    eq3:=eq3+out[i]/log(2):

   end if;

  end do:

  assign(solve({eq1=355/113,eq2=-1,eq3=0})):

  assume(x>=0,x<=1); print(m,n,a,b,c,is(a+b*x+c*x^2>=0));

 end do:

end do:


But where is the code for the integral function for pi given??




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users