(50g) Simpson's rule for f(x,y)
|
11-17-2018, 09:27 PM
(This post was last modified: 11-18-2018 11:14 PM by peacecalc.)
Post: #1
|
|||
|
|||
(50g) Simpson's rule for f(x,y)
Hello friends,
like Eddie Shore showed us HERE an algorithm for integration a function with two variables with the simpson rule and a matrix. He implemented this for the HP 71B. I do the same thing for the HP 50G but not so elegant, it is brute force: I implementated this formular: \[ F = \int_a^b\int_c^d f(t,s)dtds \\ \sim \frac{ha}{3} \left( \frac{hi}{3}\left( f(a,c) + f(b,c) + \sum_{j=1}^{k-1}\left( 2f(t_j,c) + 4f(t_j,c)\right) + f(a,d) + f(b,d) + \sum_{j=1}^{k-1}\left( 2f(t_j,d) + 4f(t_j,d)\right) + \\ \sum_{i=1}^{m-1}\left(2\left(f(a,s_i)+f(b,s_i) + \sum_{j=1}^{k-1}\left( 2f(t_j,s_i) + 4f(t_j,s_i)\right)\right) + \\ 4\left(f(a,s_i)+f(b,s_i) + \sum_{j=1}^{k-1}\left( 2f(t_j,s_i) + 4f(t_j,s_i)\right)\right)\right)\right)\right) \] That looks horrible, but I used the stack to sum up all function values and multiplied them afterwards with 2 or 4. And the indices in formular above has to be disdinguish between even or odd numbers (only values with odd indices has to be multiplied be 4 and even with 2). I used in the FOR loops no integer values but the values for the variables (the hp 50g is very happy to use a real variable in the FOR loop. For instance I used my little program for estimate antiderivatives with harmonic sphere function multiplied with a light function to geht the coeffecients.The one angle goes from 0 to pi, the other one from 0 to 2pi. With N = 15 the hp 50g has to calculate 30*60 = 180 function values and it takes 2 minutes at average. That seems to be very long, but it is faster as you take the built-in function \[ \int \]. I have the impression that the built in function works then (when you have more variables) with recursion. Code:
|
|||
11-17-2018, 10:31 PM
Post: #2
|
|||
|
|||
RE: (50g) Simpson's rule for f(x,y)
.
Hi, peacecalc: (11-17-2018 09:27 PM)peacecalc Wrote: like Eddie Shore showed us HERE an algorithm for integration a function with two variables with the simpson rule and a matrix. He implemented this for the HP 71B. I didn't see Eddie's post at the time but he's wrong on one count, namely (my bolding): Eddie W. Shore Wrote:On the HP 71B, matrices cannot be typed directly, elements have to be stored and recalled on element at a time. The program presented does not use modules. That's not correct. HP-71B's BASIC language allows for filling in all elements of an arbitrary size matrix at once by including the values in one or more DATA statements and then reading them all into the matrix using a single READ statement, no extra ROM modules needed. Thus, this lengthy initialization part in Eddie's code: 14 DIM I(5,5) 20 I(1,1) = 1 21 I(1,2) = 4 22 I(1,3) = 2 23 I(1,4) = 4 24 I(1,5) = 1 25 I(2,1) = 4 26 I(2,2) = 16 27 I(2,3) = 8 28 I(2,4) = 16 29 I(2,5) = 4 30 I(3,1) = 2 31 I(3,2) = 8 32 I(3,3) = 4 33 I(3,4) = 8 34 I(3,5) = 2 35 I(4,1) = 4 36 I(4,2) = 16 37 I(4,3) = 8 38 I(4,4) = 16 39 I(4,5) = 4 40 I(5,1) = 1 41 I(5,2) = 4 42 I(5,3) = 2 43 I(5,4) = 4 44 I(5,5) = 1 can be replaced by this much shorter, much faster version (OPTION BASE 1 assumed): 14 DATA 1,4,2,4,1,4,16,8,16,4,2,8,4,8,2,4,16,8,16,4,1,4,2,4,1
20 DIM I(5,5) @ READ I where the READ I fills in all data into the matrix with a single statement, no individual assignments or loops needed, thus it's much faster and uses less program memory. Notice that this also works for arbitrary numerical expressions in the DATA, i.e.: the following hipothetical code would work Ok: 10 DATA 5,-3,2.28007e20,X,2*Z+SIN(Y),FNF(X+Y,X-Y),FNZ(2+FNF(C+D),3/FNF(6,8)),43
20 DIM M(2,4) @ READ M Anyway, Simpson's rule is suboptimal for integration purposes, either one-dimensional or multi-dimensional. There are much better methods providing either significantly increased accuracy for the same number of function evaluations or the same accuracy with fewer evaluations. V. All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
11-19-2018, 08:33 PM
Post: #3
|
|||
|
|||
RE: (50g) Simpson's rule for f(x,y)
Hello Valentin,
I second your statement: Quote:Anyway, Simpson's rule is suboptimal for integration purposes, either one-dimensional or multi-dimensional. There are much better methods providing either significantly increased accuracy for the same number of function evaluations or the same accuracy with fewer evaluations. But as I wrote with double integrals the build-in solution for the hp 50g is slower then the brute force simpson rule. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)