Fragrancuu
Technical User
Hi does anyone know a good method of coding for the solutions of integrals of Simpsons/Trapezoidal functions or the Newton Raphson method?
thanks
thanks
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.
[COLOR=#a020f0]module[/color] integration_methods
[COLOR=#0000ff]! This module contains the integration methods [/color]
[COLOR=#a020f0]contains[/color]
[COLOR=#a020f0]subroutine[/color] trapezoid_rule(f, a, b, n, integral)
[COLOR=#2e8b57][b] real[/b][/color] :: f
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: a, b
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: n
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]out[/b][/color]) :: integral
[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]
integral [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 subroutine[/color] trapezoid_rule
[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=#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] :: integral
[COLOR=#0000ff]! integrating he function f1[/color]
[COLOR=#a020f0]call[/color] trapezoid_rule(f1, [COLOR=#ff00ff]0.0[/color], [COLOR=#ff00ff]1.0[/color], [COLOR=#ff00ff]1000[/color], integral)
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Trapezoid rule = '[/color], integral
[COLOR=#0000ff]! integrating the function f2[/color]
[COLOR=#a020f0]call[/color] trapezoid_rule(f2, [COLOR=#ff00ff]0.005[/color], [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]1000[/color], integral)
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Trapezoid rule = '[/color], integral
[COLOR=#a020f0]end program[/color] integration
$ g95 fragrancuu.f95 -o fragrancuu
$ fragrancuu
Trapezoid rule = 0.33333343
Trapezoid rule = 3.1267667