integral of sin(x)
integral of sin(x)
(OP)
Using Monte Carlos Method, how to make a program to calculate the integral points of sin(x) from 0 to pi and how to calculate the errors?
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS Come Join Us!Are you a
Computer / IT professional? Join TekTips Forums!
*TekTips's functionality depends on members receiving email. By joining you are opting in to receive email. Posting GuidelinesPromoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs 

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love TekTips Forums:
Register now while it's still free!
Already a member? Close this window and log in.
RE: integral of sin(x)
http://turing.une.edu.au/~amth142/Lectures/Lecture...
and here is the example for integral of sin(x), which you can convert into Fortran
https://answers.yahoo.com/question/index?qid=20070...
RE: integral of sin(x)
PROGRAM area_sin_monte_carlo
USE nrtype
USE nrutil
USE ran_state, ONLY: ran_seed
USE nr, ONLY: ran1
IMPLICIT none
INTEGER, PARAMETER :: u_file = 1, angle_i = 0.0, angle_f=pi, p= 5000
REAL, PARAMETER :: v_angle = (angle_f  angle_i)/REAL(p)
INTEGER ::n, k
REAL :: x, y, area_mc, area, erro, harvest, angle= angulo_i
OPEN(unit= u_file, file= "p_sen.dat", action = "write")
k = 0
DO n = 1, p
call ran1(harvest)
x = harvest*PI
y = sin(x)
WRITE(u_file, *) x, y
k = k + 1
angle= angle + v_angle
END DO
area_mc = ?????????
! area of circle by traditional method
area= cos(pi) + cos(0.0)
erro=abs((area  area_mc)/(area))*100 ! the erro
WRITE(*,*) p
WRITE(*,*)area_mc
WRITE(*,*) area
WRITE(*,*)'Erro(%): ', erro
END PROGRAM area_sin_monte_carlo
RE: integral of sin(x)
Once you have built it, examine what you are getting against what you are expecting. That will possibly tell you what is missing. Again, tell us what you are getting and what you are expecting and someone may be able to tell you how to fix it.
RE: integral of sin(x)
My code calculate just the slope and not a set of points as I was waiting, by Monte Carlos Method.
IN the program that I wrote before there was a little wront, not it is ok.
PROGRAM area_sin_monte_carlo
USE nrtype
USE nrutil
USE ran_state, ONLY: ran_seed
USE nr, ONLY: ran1
IMPLICIT none
INTEGER, PARAMETER :: u_file = 1, angle_f=pi, p= 5000
REAL, PARAMETER :: angle_i = 0.0, v_angle = (angle_f  angle_i)/REAL(p)
INTEGER ::n, k
REAL :: x, y, area_mc, area, erro, harvest, angle
OPEN(unit= u_file, file= "p_sen.dat", action = "write")
k = 0
angle= angle_i
DO n = 1, p
call ran1(harvest)
x = harvest*PI
y = sin(x)
WRITE(u_file, *) x, y
k = k + 1
angle= angle + v_angle
END DO
CLOSE(unit=u_file)
! area_mc = ?????????
! area of circle by the traditional method
area= cos(pi) + cos(0.0)
erro=abs((area  area_mc)/(area))*100 ! the erro
WRITE(*,*) p
WRITE(*,*)area_mc
WRITE(*,*) area
WRITE(*,*)'Erro(%): ', erro
END PROGRAM area_sin_monte_carlo
RE: integral of sin(x)
It says, that for a given big N, you should generate from the interval [a, b] N random points: x_{1}, x_{2}, .., x_{N}
Then the integral of the function f(x) on the interval [a, b] should be approximately given by a formula:
I(f, a, b) = (ba) / N * ( f(x_{1}) + f(x_{2}) + ... + f(x_{N}) )
RE: integral of sin(x)
Earlier I posted a code with some classical integration rules in this thread
http://www.tektips.com/viewthread.cfm?qid=1570169
Now I have added to this code the Monte Carlo method and f3(x) as the function sin(x)
Here is the code
monte_carlo_integration.f95
CODE
CODE
Note, that this is the probability method, so you cannot be sure to increase the accuracy of the result simply by increasing the number of attempts.
If you are interested see this thread
http://www.tektips.com/viewthread.cfm?qid=1730131
RE: integral of sin(x)