English
Source / code snippets

Newton-Cotes Integration, QANC8-Algorithmus

 

p.specht

an XProfan-11-Umsetzung the QUANC8-Algorithmus, Translated from Fortran-90.
Window Title "QANC8 Integration"
'{----------------------------------------------------------
' Program to speedy näherungsweisen Berechnung of Integralen
' through the https://de.wikipedia.org/wiki/Newton-Cotes-Formeln .
' in the item becomes The Formelordnung between 2 and 6 explains.
' the nachstehende Program uses a Formel 8. Order with
' of users vorgebbarer absolute and relativer accuracy.
' From Fortran-90 to XProfan11 2016-10 by P.woodpecker, Vienna/Austria/EU
' No however geartete Gewähr! Möglicherweise rights Third!
' ------------------------------------------------------------------
' Program to integrate a user-defined function f(x) from x1 to x2 by
' the QANC8 subroutine with control of abs. and relative precisions
' ------------------------------------------------------------------
' SAMPLE RUN:
'  (Integrate function cos(x) - 2 sin(x) from x=0 to x=1,
'   with a precision <= 1e-10):
'
'  Integral Value = -7.792440345582408E-002
'  Estimated error =  1.041851518709824E-017
'  Error code =  0.000000000000000E+000
'  Number of function evaluations =          33
' ------------------------------------------------------------------
' Reference: From Numath Library By Tuan Dang Trong in Fortran 77
'                              F90 Release 1.0 By J-P Moreau, Paris
' F90 translated to XProfan 11.2a by P.woodpecker, Vienna /Austria.
' As based on piecewise polynomial approximation, quanc8 is hardship
' designed to lever certain kinds of integrals (e.g. functions f(x)
' where derivatives < 10th order are unbound or do hardship exist).
'}---------------------------------------------------------------
' PROGRAM TQANC8  'Test Quanc8
Windowstyle 24:Cls rgb(200,200,220):Font 2
Declare  AERROR!,CODE![0],Error![0],RERROR!,X1!,X2!,Valu![0],NBRF&[0]
X1!=0
X2!=1
AERROR!=Val("1e-9")
RERROR!=Val("1e-10")
QANC8(X1!,X2!,AERROR!,RERROR!,Valu![],Error![],NBRF&[],CODE![])
print "\n Integral  Value =", stature$("%e",Valu![0])
Print "\n Estimated error =", stature$("%e",Error![0])
Print "\n Error code =", stature$("%g",CODE![0])
Print "\n Number of function evaluations =", stature$("%u",NBRF&[0])
Beep
WaitInput
End
'--------------------------------------------------------------
'  To integrierende function:
'--------------------------------------------------------------

Proc FCT : Parameters X!

    ' for a allgemeintes Intervall [x1,x2] are
    ' as Stützstellen x = x1 +(x2-x1)*x To take!
    Declare fct!
    FCT!=Cos(X!)-2*Sin(X!)
    Return FCT!

ENDPROC

proc DMAX1 :parameters z1!,z2! :return if(z1!>z2!,z1!,z2!)

endproc

'{--------------------------------------------------------------
' QUICK APPROXIMATION using NEWTON COTES of order 8:
' Proc  QANC8 :Parameters A!,B!,AERR!,RERR!,RES!,ERR!,NBF&,FLG!)
'
'     INTEGRATE A REAL FUNCTION FCT(X) FROM X=A TO X=B, WITH
'     GIVEN ABSOLUTE AND RELATIVE PRECISIONS, AERR, RERR.
'     INPUTS:
'     FCT     EXTERNAL USER-DEFINED FUNCTION FOR ANY X VALUE
'             IN INTERVAL (A,B)
'     A,B     LIMITS OF INTERVAL
'     AERR,RERR   RESPECTIVELY ABSOLUTE ERROR AND RELATIVE ERROR
'                 REQUIRED BY USER
'     OUTPUTS:
'     RES     VALUE OF INTEGRAL
'     ERR     ESTIMATED ERROR
'     NBF     NUMBER OF NECESSARY FCT(X) EVALUATIONS
'     FLG     INDICATOR
'             = 0.0       CORRECT RESULT
'             = NNN.RRR   NO CONVERGENCE DU TO A SINGULARITY.
'             THE SINGULAR POINT ABCISSA IS GIVEN BY FORMULA:
'             XS = B-.RRR*(B-A)
' Ref.: FORSYTHE,G.E. (1977) COMPUTER METHODS FOR MATHEMATICAL
'       COMPUTATIONS. PRENTICE-HALL, INC.
' ------------------------------------------------------------
'}    IMPLICIT REAL *8 (A-H,O-Z)

Proc  QANC8 :Parameters A!,B!,AERR!,RERR!,RES![],ERR![],NBF&[],FLG![]

    Declare LMIN&,LMAX&,LOUT&,NMAX&,NFIN&,W0!,W1!,W2!,W3!,W4!
    Declare Cor!,Sum!,L&,NIM&,X0!,QP!,PAS!,PAS1!,I&,J&,F0!
    Declare QL!,QN!,QD!,ERR1!,Tol1!,TEMP!
    Declare QR![31],F![16],X![16],FS![8,30],XS![8,30]
    LMIN& = 1
    LMAX& = 30
    LOUT& = 6
    NMAX& = 5000
    NFIN& = NMAX&-8*(LMAX&-LOUT&+2^(LOUT&+1))
    W0!  =   3956/14175
    W1!  =  23552/14175
    W2!  =  -3712/14175
    W3!  =  41984/14175
    W4!  = -18160/14175
    FLG![0] = 0
    RES![0] = 0
    COR! = 0
    ERR![0] = 0
    SUM! = 0
    NBF&[0] = 0
    Case  A!=B!: Return
    L& = 0
    NIM& = 1
    X0!  = A!
    X![16] = B!
    QP! = 0
    F0!   = FCT(X0!)
    PAS1!  = (B!-A!)/16
    X![8]  = (X0!+X![16])/2
    X![4]  = (X0!+X![8])/2
    X![12] = (X![8]+X![16])/2
    X![2]  = (X0!+X![4])/2
    X![6]  = (X![4]+X![8])/2
    X![10] = (X![8]+X![12])/2
    X![14] = (X![12]+X![16])/2

    Whileloop 2,16,2:j&=&Loop

        F![J&] = FCT(X![J&])

    EndWhile

    NBF&[0] = 9
    L30:
    X![1]  = (X0!+X![2])/2
    F![1] = FCT(X![1])

    WhileLoop 3,15,2:J&=&Loop

        X![J&]  = (X![J&-1]+X![J&+1])/2
        F![J&] = FCT(X![J&])

    EndWhile

    L35:
    NBF&[0] = NBF&[0]+8
    PAS! = (X![16]-X0!)/16
    QL!  = (W0!*(F0!+F![8])+W1!*(F![1]+F![7])+\
    W2!*(F![2]+F![6])+W3!*(F![3]+F![5])+W4!*F![4])*PAS!
    QR![L&+1] = (W0!*(F![8]+F![16])+W1!*(F![9]+F![15])+\
    W2!*(F![10]+F![14])+W3!*(F![11]+F![13])+W4!*F![12])*PAS!
    QN! = QL! + QR![L&+1]
    QD! = QN! - QP!
    SUM! = SUM! + QD!
    ERR1! = Abs(QD!)/1023
    TOL1! = DMAX1(AERR!, RERR!*Abs(SUM!)) * (PAS!/PAS1!)
    Case L&<LMIN&:Goto "L50"
    Case L&>=LMAX&:Goto "L62"
    Case NBF&[0]>NFIN&:Goto "L60"
    Case ERR1!<=TOL1!:Goto "L70"
    L50:
    NIM& = 2*NIM&
    L& = L&+1

    WhileLoop 8:i&=&Loop

        FS![I&,L&] = F![I&+8]
        XS![I&,L&] = X![I&+8]

    EndWhile

    L52:
    QP! = QL!

    WhileLoop 8:i&=&Loop

        F![18-2*I&] = F![9-I&]
        X![18-2*I&] = X![9-I&]

    EndWhile

    L55:
    Goto "L30"
    L60:
    NFIN& = 2*NFIN&
    LMAX& = LOUT&
    FLG![0] = FLG![0] + (B!-X0!)/(B!-A!)
    Goto "L70"
    L62:
    FLG![0] = FLG![0] + 1
    L70:
    RES![0] = RES![0] + QN!
    ERR![0] = ERR![0] + ERR1!
    COR! = COR! + QD!/1023
    L72:
    ' (A!-H!,O!-Z!)
    Case NIM&=(int(NIM&/2)*2):Goto "L75"'straight
    NIM& = NIM&/2
    L& = L&-1
    Goto "L72"
    L75:
    NIM& = NIM&+1
    Case L&<=0:Goto "L80"
    QP! = QR![L&]
    X0! = X![16]
    F0! = F![16]

    WhileLoop 8:i&=&Loop

        F![2*I&] = FS![I&,L&]
        X![2*I&] = XS![I&,L&]

    Endwhile

    Goto "L30"
    L80:
    RES![0] = RES![0] + COR!
    Case ERR![0]=0: RETURN
    L82:
    TEMP! = Abs(RES![0]) + ERR![0]
    Case TEMP!<>Abs(RES![0]): RETURN
    ERR![0] = 2*ERR![0]
    Goto "L82"

ENDPROC

 
XProfan 11
Computer: Gerät, daß es in Mikrosekunden erlaubt, 50.000 Fehler zu machen, zB 'daß' statt 'das'...
05/22/21  
 



Zum Quelltext


Topictitle, max. 100 characters.
 

Systemprofile:

no Systemprofil laid out. [anlegen]

XProfan:

 Posting  Font  Smilies  ▼ 

Please register circa a Posting To verfassen.
 

Topic-Options

1.284 Views

Untitledvor 0 min.
Erhard Wirth06/14/24
Stringray01/05/22
p.specht11/20/21
Uwe Lang11/20/21
More...

Themeninformationen

this Topic has 1 subscriber:

p.specht (1x)


Admins  |  AGB  |  Applications  |  Authors  |  Chat  |  Privacy Policy  |  Download  |  Entrance  |  Help  |  Merchantportal  |  Imprint  |  Mart  |  Interfaces  |  SDK  |  Services  |  Games  |  Search  |  Support

One proposition all XProfan, The there's!


My XProfan
Private Messages
Own Storage Forum
Topics-Remember-List
Own Posts
Own Topics
Clipboard
Log off
 Deutsch English Français Español Italia
Translations

Privacy Policy


we use Cookies only as Session-Cookies because of the technical necessity and with us there no Cookies of Drittanbietern.

If you here on our Website click or navigate, stimmst You ours registration of Information in our Cookies on XProfan.Net To.

further Information To our Cookies and moreover, How You The control above keep, find You in ours nachfolgenden Datenschutzerklärung.


all rightDatenschutzerklärung
i want none Cookie