Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
j0zn said:I can't transforma the information in the following link you sugested me --> Link in a fortran code.
[COLOR=#a020f0]module[/color] integration_methods
[COLOR=#0000ff]! This module contains the integration methods [/color]
[COLOR=#a020f0]contains[/color]
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] trapezoid_rule(f, a, b, n)
[COLOR=#0000ff]! Approximate the definite integral of f from a to b by the[/color]
[COLOR=#0000ff]! composite trapezoidal rule, using N subintervals[/color]
[COLOR=#0000ff]! see: <a href="[URL unfurl="true"]http://en.wikipedia.org/wiki/Trapezoid_rule">http://en.wikipedia.org/wiki/Trapezoid_rule</a>[/URL][/color]
[COLOR=#0000ff]! function arguments ---------------------[/color]
[COLOR=#0000ff]! f: real function[/color]
[COLOR=#2e8b57][b] real[/b][/color] :: f
[COLOR=#0000ff]! [a, b] : the interval of integration[/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: a, b
[COLOR=#0000ff]! n : number of subintervals used[/color]
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: n
[COLOR=#0000ff]! ----------------------------------------[/color]
[COLOR=#0000ff]! temporary variables[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: k
[COLOR=#2e8b57][b] real[/b][/color] :: s
s [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
s [COLOR=#804040][b]=[/b][/color] s [COLOR=#804040][b]+[/b][/color] f(a [COLOR=#804040][b]+[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a)[COLOR=#804040][b]*[/b][/color]k[COLOR=#804040][b]/[/b][/color]n)
[COLOR=#804040][b]end do[/b][/color]
trapezoid_rule [COLOR=#804040][b]=[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a) [COLOR=#804040][b]*[/b][/color] (f(a)[COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]2[/color] [COLOR=#804040][b]+[/b][/color] f(b)[COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]2[/color] [COLOR=#804040][b]+[/b][/color] s) [COLOR=#804040][b]/[/b][/color] n
[COLOR=#a020f0]end function[/color] trapezoid_rule
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] simpson_rule(f, a, b, n)
[COLOR=#0000ff]! Approximate the definite integral of f from a to b by the[/color]
[COLOR=#0000ff]! composite Simpson's rule, using N subintervals[/color]
[COLOR=#0000ff]! see: <a href="[URL unfurl="true"]http://en.wikipedia.org/wiki/Simpson%27s_rule">http://en.wikipedia.org/wiki/Simpson%27s_rule</a>[/URL][/color]
[COLOR=#0000ff]! function arguments ---------------------[/color]
[COLOR=#0000ff]! f: real function[/color]
[COLOR=#2e8b57][b] real[/b][/color] :: f
[COLOR=#0000ff]! [a, b] : the interval of integration[/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: a, b
[COLOR=#0000ff]! n : number of subintervals used[/color]
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: n
[COLOR=#0000ff]! ----------------------------------------[/color]
[COLOR=#0000ff]! temporary variables[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: k
[COLOR=#2e8b57][b] real[/b][/color] :: s
s [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
s [COLOR=#804040][b]=[/b][/color] s [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b]**[/b][/color]([COLOR=#008080]mod[/color](k,[COLOR=#ff00ff]2[/color])[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]*[/b][/color] f(a [COLOR=#804040][b]+[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a)[COLOR=#804040][b]*[/b][/color]k[COLOR=#804040][b]/[/b][/color]n)
[COLOR=#804040][b]end do[/b][/color]
simpson_rule [COLOR=#804040][b]=[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a) [COLOR=#804040][b]*[/b][/color] (f(a) [COLOR=#804040][b]+[/b][/color] f(b) [COLOR=#804040][b]+[/b][/color] s) [COLOR=#804040][b]/[/b][/color] ([COLOR=#ff00ff]3[/color][COLOR=#804040][b]*[/b][/color]n)
[COLOR=#a020f0]end function[/color] simpson_rule
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] monte_carlo_integration(f, a, b, n)
[COLOR=#0000ff]! Approximate the definite integral of f from a to b by the[/color]
[COLOR=#0000ff]! Monte Carlo Method[/color]
[COLOR=#0000ff]! function arguments ---------------------[/color]
[COLOR=#0000ff]! f: real function[/color]
[COLOR=#2e8b57][b] real[/b][/color] :: f
[COLOR=#0000ff]! [a, b] : the interval of integration[/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: a, b
[COLOR=#0000ff]! n : number of random points used[/color]
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: n
[COLOR=#0000ff]! ----------------------------------------[/color]
[COLOR=#0000ff]! temporary variables[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: k
[COLOR=#2e8b57][b] real[/b][/color] :: s, x
s [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
[COLOR=#0000ff]! generate random number from [0, 1][/color]
[COLOR=#0000ff]!x = rand()[/color]
[COLOR=#008080]call[/color] [COLOR=#008080]random_number[/color](x)
[COLOR=#0000ff]! compute function value in random point of interval [a, b] [/color]
s [COLOR=#804040][b]=[/b][/color] s [COLOR=#804040][b]+[/b][/color] f(a [COLOR=#804040][b]+[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a)[COLOR=#804040][b]*[/b][/color]x)
[COLOR=#804040][b]end do[/b][/color]
monte_carlo_integration [COLOR=#804040][b]=[/b][/color] (b[COLOR=#804040][b]-[/b][/color]a) [COLOR=#804040][b]*[/b][/color] s [COLOR=#804040][b]/[/b][/color] n
[COLOR=#a020f0]end function[/color] monte_carlo_integration
[COLOR=#a020f0]end module[/color] integration_methods
[COLOR=#a020f0]module[/color] user_functions
[COLOR=#0000ff]! Define here your own functions to be integrated [/color]
[COLOR=#a020f0]contains[/color]
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] f1(x)
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: x
f1 [COLOR=#804040][b]=[/b][/color] x[COLOR=#804040][b]*[/b][/color]x
[COLOR=#a020f0]end function[/color] f1
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] f2(y)
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: y
f2 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color][COLOR=#804040][b]/[/b][/color](y[COLOR=#804040][b]-[/b][/color]([COLOR=#ff00ff]0.55[/color][COLOR=#804040][b]*[/b][/color]y[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]2[/color] [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]0.0165[/color][COLOR=#804040][b]*[/b][/color]y [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]0.00012375[/color]))
[COLOR=#a020f0]end function[/color] f2
[COLOR=#2e8b57][b] real[/b][/color] [COLOR=#a020f0]function[/color] f3(x)
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: x
f3 [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]sin[/color](x)
[COLOR=#a020f0]end function[/color] f3
[COLOR=#a020f0]end module[/color] user_functions
[COLOR=#a020f0]program[/color] integration
[COLOR=#a020f0]use[/color] integration_methods
[COLOR=#a020f0]use[/color] user_functions
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: pi[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]3.141592653589793d0[/color]
[COLOR=#2e8b57][b] real[/b][/color] :: integral
[COLOR=#0000ff]! integrating he function f1[/color]
integral[COLOR=#804040][b]=[/b][/color]trapezoid_rule(f1, [COLOR=#ff00ff]0.0[/color], [COLOR=#ff00ff]1.0[/color], [COLOR=#ff00ff]1000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Trapezoid rule = '[/color], integral
integral[COLOR=#804040][b]=[/b][/color]simpson_rule(f1, [COLOR=#ff00ff]0.0[/color], [COLOR=#ff00ff]1.0[/color], [COLOR=#ff00ff]10000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Simpson's rule = "[/color], integral
integral[COLOR=#804040][b]=[/b][/color]monte_carlo_integration(f1, [COLOR=#ff00ff]0.0[/color], [COLOR=#ff00ff]1.0[/color], [COLOR=#ff00ff]10000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Monte Carlo method = "[/color], integral
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#0000ff]! integrating the function f2[/color]
integral[COLOR=#804040][b]=[/b][/color]trapezoid_rule(f2, [COLOR=#ff00ff]0.005[/color], [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]1000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Trapezoid rule = '[/color], integral
integral[COLOR=#804040][b]=[/b][/color]simpson_rule(f2, [COLOR=#ff00ff]0.005[/color], [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]1000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Simpson's rule = "[/color], integral
integral[COLOR=#804040][b]=[/b][/color]monte_carlo_integration(f2, [COLOR=#ff00ff]0.005[/color], [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]10000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Monte Carlo method = "[/color], integral
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#0000ff]! integrating the function f3[/color]
integral[COLOR=#804040][b]=[/b][/color]trapezoid_rule(f3, [COLOR=#ff00ff]0.0[/color], pi, [COLOR=#ff00ff]1000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Trapezoid rule = '[/color], integral
integral[COLOR=#804040][b]=[/b][/color]simpson_rule(f3, [COLOR=#ff00ff]0.0[/color], pi, [COLOR=#ff00ff]1000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Simpson's rule = "[/color], integral
integral[COLOR=#804040][b]=[/b][/color]monte_carlo_integration(f3, [COLOR=#ff00ff]0.0[/color], pi, [COLOR=#ff00ff]100000[/color])
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Monte Carlo method = "[/color], integral
[COLOR=#a020f0]end program[/color] integration
$ gfortran monte_carlo_integration.f95 -o monte_carlo_integration
$ monte_carlo_integration
Trapezoid rule = 0.333333433
Simpson's rule = 0.333333194
Monte Carlo method = 0.337485731
Trapezoid rule = 3.12676668
Simpson's rule = 3.12673712
Monte Carlo method = 3.14724326
Trapezoid rule = 1.99999881
Simpson's rule = 2.00000048
Monte Carlo method = 2.00680566