Jump to content

Numerical integration: Difference between revisions

→‎{{header|J}}: Extend examples
(Add J)
(→‎{{header|J}}: Extend examples)
Line 842:
size * +/ 2 u\ a + size * i.>:steps
)
 
simpsonrectangle=: adverb define
(6u %~ [-: +/ 4&*@u@-:@(+/) , u) y
)
 
Line 851:
)
 
rectanglesimpson=: adverb define
u(6 -%~ [: +/ 4&*@u@-:@(+/) , u) y
)</lang>
'''Example usage'''<br>
 
Integrate <code>square</code> (<code>*:</code>) from 0 to pi in 10 steps using various methods.
'''Example usage'''
<lang j>
*: rectangle integrate 0 1p1 10
10.3095869962
*: trapezium integrate 0 1p1 10
10.3871026879
*: simpson integrate 0 1p1 10
10.3354255601
</lang>
Integrate <code>sin</code> from 0 to pi in 10 steps using various methods.
 
<lang j>
sin=: 1&o.
sin rectangle integrate 0 1p1 10
2.00824840791
sin trapezium integrate 0 1p1 10
1.98352353751
sin simpson integrate 0 1p1 10
2.00000678444
</lang>
Note that J has a primitive verb <code>p..</code> for integrating polynomials. For example the integral of <math>x^2</math> (which can be described in terms of its coefficients as <code>0 0 1</code>) is:
<lang> 0 p.. 0 0 1
0 0 0 0.333333333333
0 p.. 0 0 1x NB. or using rationals
0 0 0 1r3</lang>
That is <math>0x^0 + 1x^1 + 0x^2 + 0.3333x^3</math>
 
=={{header|Java}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.