June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,8 +1,16 @@
|
|||
function simpson(f, a,b, n)
|
||||
h = (b-a)/n
|
||||
s = f(a + h/2)
|
||||
function simpson(f::Function, a::Number, b::Number, n::Integer)
|
||||
h = (b - a) / n
|
||||
s = f(a + h / 2)
|
||||
for i in 1:(n-1)
|
||||
s += f(a + h*i + h/2) + 0.5 * f(a + h*i)
|
||||
s += f(a + h * i + h / 2) + f(a + h * i) / 2
|
||||
end
|
||||
h/6 * (f(a) + f(b) + 4*s)
|
||||
return h/6 * (f(a) + f(b) + 4*s)
|
||||
end
|
||||
|
||||
rst =
|
||||
simpson(x -> x ^ 3, 0, 1, 100),
|
||||
simpson(x -> 1 / x, 1, 100, 1000),
|
||||
simpson(x -> x, 0, 5000, 5_000_000),
|
||||
simpson(x -> x, 0, 6000, 6_000_000)
|
||||
|
||||
@show rst
|
||||
|
|
|
|||
88
Task/Numerical-integration/Ring/numerical-integration.ring
Normal file
88
Task/Numerical-integration/Ring/numerical-integration.ring
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
# Project : Numerical integration
|
||||
# Date : 2018/04/087
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
decimals(8)
|
||||
data = [["pow(x,3)",0,1,100], ["1/x",1, 100,1000], ["x",0,5000,5000000], ["x",0,6000,6000000]]
|
||||
see "Function Range L-Rect R-Rect M-Rect Trapeze Simpson" + nl
|
||||
for p = 1 to 4
|
||||
d1 = data[p][1]
|
||||
d2 = data[p][2]
|
||||
d3 = data[p][3]
|
||||
d4 = data[p][4]
|
||||
see "" + d1 + " " + d2 + " - " + d3 + " " + lrect(d1, d2, d3, d4) + " " + rrect(d1, d2, d3, d4)
|
||||
see " " + mrect(d1, d2, d3, d4) + " " + trapeze(d1, d2, d3, d4) + " " + simpson(d1, d2, d3, d4) + nl
|
||||
next
|
||||
|
||||
func lrect(x2, a, b, n)
|
||||
s = 0
|
||||
d = (b - a) / n
|
||||
x = a
|
||||
for i = 1 to n
|
||||
eval("result = " + x2)
|
||||
s = s + d * result
|
||||
x = x + d
|
||||
next
|
||||
return s
|
||||
|
||||
func rrect(x2, a, b, n)
|
||||
s = 0
|
||||
d = (b - a) / n
|
||||
x = a
|
||||
for i = 1 to n
|
||||
x = x + d
|
||||
eval("result = " + x2)
|
||||
s = s + d *result
|
||||
next
|
||||
return s
|
||||
|
||||
func mrect(x2, a, b, n)
|
||||
s = 0
|
||||
d = (b - a) / n
|
||||
x = a
|
||||
for i = 1 to n
|
||||
x = x + d/2
|
||||
eval("result = " + x2)
|
||||
s = s + d * result
|
||||
x = x +d/2
|
||||
next
|
||||
return s
|
||||
|
||||
func trapeze(x2, a, b, n)
|
||||
s = 0
|
||||
d = (b - a) / n
|
||||
x = b
|
||||
eval("result = " + x2)
|
||||
f = result
|
||||
x = a
|
||||
eval("result = " + x2)
|
||||
s = d * (f + result) / 2
|
||||
for i = 1 to n-1
|
||||
x = x + d
|
||||
eval("result = " + x2)
|
||||
s = s + d * result
|
||||
next
|
||||
return s
|
||||
|
||||
func simpson(x2, a, b, n)
|
||||
s1 = 0
|
||||
s = 0
|
||||
d = (b - a) / n
|
||||
x = b
|
||||
eval("result = " + x2)
|
||||
f = result
|
||||
x = a + d/2
|
||||
eval("result = " + x2)
|
||||
s1 = result
|
||||
for i = 1 to n-1
|
||||
x = x + d/2
|
||||
eval("result = " + x2)
|
||||
s = s + result
|
||||
x = x + d/2
|
||||
eval("result = " + x2)
|
||||
s1 = s1 + result
|
||||
next
|
||||
x = a
|
||||
eval("result = " + x2)
|
||||
return (d / 6) * (f + result + 4 * s1 + 2 * s)
|
||||
41
Task/Numerical-integration/Stata/numerical-integration.stata
Normal file
41
Task/Numerical-integration/Stata/numerical-integration.stata
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
mata
|
||||
function integrate(f,a,b,n,u,v) {
|
||||
s = 0
|
||||
h = (b-a)/n
|
||||
m = length(u)
|
||||
for (i=0; i<n; i++) {
|
||||
x = a+i*h
|
||||
for (j=1; j<=m; j++) s = s+v[j]*(*f)(x+h*u[j])
|
||||
}
|
||||
return(s*h)
|
||||
}
|
||||
|
||||
function log_(x) {
|
||||
return(log(x))
|
||||
}
|
||||
|
||||
function id(x) {
|
||||
return(x)
|
||||
}
|
||||
|
||||
function cube(x) {
|
||||
return(x*x*x)
|
||||
}
|
||||
|
||||
function inv(x) {
|
||||
return(1/x)
|
||||
}
|
||||
|
||||
function test(f,a,b,n) {
|
||||
return(integrate(f,a,b,n,(0,1),(1,0)),
|
||||
integrate(f,a,b,n,(0,1),(0,1)),
|
||||
integrate(f,a,b,n,(0.5),(1)),
|
||||
integrate(f,a,b,n,(0,1),(0.5,0.5)),
|
||||
integrate(f,a,b,n,(0,1/2,1),(1/6,4/6,1/6)))
|
||||
}
|
||||
|
||||
test(&cube(),0,1,100)
|
||||
test(&inv(),1,100,1000)
|
||||
test(&id(),0,5000,5000000)
|
||||
test(&id(),0,6000,6000000)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue