|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2007
Città: Milano
Messaggi: 7103
|
[Python]Trapezium rule
A parte l'orrida forma(che poi si puo sistemare).
Sto cercando di creare una funzione per la regola del trapezio di integrazione numerica. con k=1,..,nCodice PHP:
Codice:
BEGIN
integral of sin(x) from 0 to 2*pi:
Traceback (most recent call last):
File "trapezium.py", line 37, in <module>
print trapezium_plus(sin, 0, 2*pi, 30)
File "trapezium.py", line 19, in trapezium_plus
x[n] = f(x[n])
KeyError: 30
Grazie
__________________
Apple Watch Ultra + iPhone 15 Pro Max + Rog Ally + Legion Go Ultima modifica di The_ouroboros : 28-11-2008 alle 22:35. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2007
Città: Milano
Messaggi: 7103
|
in metacodice sarebbe:
Codice:
FUNCTION trapez(f,a,b,n)
z = (b-a)/2*n
FOR i=1:n
x(i)=a+i*((b-a)/n)
END
res = f(x(1))+f(x(n))
FOR i=2:n-1
res = res + f(x(i))
END
res = res*z
RETURN res
__________________
Apple Watch Ultra + iPhone 15 Pro Max + Rog Ally + Legion Go |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jan 2002
Città: Germania
Messaggi: 26110
|
Prova così:
Codice:
def trapez(f, a, b, n):
z = (b - a) / 2 * n
for i in xrange(1, n + 1):
x[i] = a + i * ((b - a) / n)
res = f(x[1]) + f(x[n])
for i in xrange(2, n):
res += f(x[i])
return res * z
__________________
Per iniziare a programmare c'è solo Python con questo o quest'altro (più avanzato) libro @LinkedIn Non parlo in alcun modo a nome dell'azienda per la quale lavoro Ho poco tempo per frequentare il forum; eventualmente, contattatemi in PVT o nel mio sito. Fanboys |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: May 2007
Città: Milano
Messaggi: 7103
|
grazie... mi ero un po impantanato in una cavolata
__________________
Apple Watch Ultra + iPhone 15 Pro Max + Rog Ally + Legion Go |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: May 2007
Città: Milano
Messaggi: 7103
|
Codice:
#!/usr/bin/env python
def trapezium_rule(f, a, b):
"Approximate the definite integral of f from a to b by Trapezium rule."
return (b - a) * ((f(a) + f(b))/2)
"Approximate the definite integral of f from a to b(subdivided) by Trapezium rule. n is the numbers of partitions "
def trapezium_plus(f,a,b,n):
z = (b-a)/2*n
res = z
x = {0:0}
for i in range(1, n+1):
x[i]= a + i*((b-a)/n)
res = f(x[1]) + f(x[n])
for j in range(2, n):
x[j] = 2 * f(x[j])
for k in range(1, n):
res += x[k]
return res * z
"by cdimauro"
def trapez(f, a, b, n):
x = {0:0}
z = (b - a) / 2 * n
for i in xrange(1, n + 1):
x[i] = a + i * ((b - a) / n)
res = f(x[1]) + f(x[n])
for i in xrange(2, n):
res += f(x[i])
return res * z
from math import sin
from math import pi
n = input ('Number of iteration?')
print 'BEGIN'
print '\nWith', n, ' iteration'
print '\nIntegral of sin(x) from 0 to 2*pi(ref from Simpson):\t 2.5648942583e-16'
print '\nIntegral of sin(x) from 0 to 2*pi(trapezium_plus):\t' , trapezium_plus(sin, 0, 2*pi, n)
print '\nIntegral of sin(x) from 0 to 2*pi(trapez):\t' , trapez(sin, 0, 2*pi, n)
print '\nEND'
__________________
Apple Watch Ultra + iPhone 15 Pro Max + Rog Ally + Legion Go Ultima modifica di The_ouroboros : 28-11-2008 alle 23:52. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:13.










con
k=1,..,n








