(50G) (49G+) Trapezoidal rule integration in RPL
11-17-2015, 11:55 AM (This post was last modified: 06-15-2017 01:57 PM by Gene.)
Post: #1 Marcio Senior Member Posts: 438 Joined: Feb 2015
(50G) (49G+) Trapezoidal rule integration in RPL
Hello,

I have been using this code for years now and due to its simplicity, it has never failed.

<< $$\rightarrow$$ M
<< M SIZE OBJ$$\rightarrow$$ DROP DROP 'p' STO 0 'A' STO
2 p FOR i 'M(i,1)' $$\rightarrow$$NUM 'M(i-1,1)' $$\rightarrow$$NUM - 'M(i,2)' $$\rightarrow$$NUM 'M(i-1,2)' $$\rightarrow$$NUM + x 'A' $$\rightarrow$$NUM + 'A' STO NEXT
'A' $$\rightarrow$$NUM 2 / "AREA" $$\rightarrow$$TAG >>
'A' 'p' PURGE PURGE
>>

Although not optimized and is actually FORTRAN translated into RPL, this code is fast calculating the area under a curve in accordance with the trapezoidal rule:

$\int_{x_1}^{x_n} y(x) dx \approx \frac{1}{2} \sum_{k=1}^{n-1} (x_{k+1}-x_{k})(y_{k+1}+y_{k})$

Marcio
11-17-2015, 05:56 PM
Post: #2
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: [HP-50G/49G+] Trapezoidal rule integration in RPL
You'd have to use a list of vectors with this program for the HP-48:

Code:
\<< 0 SWAP 2   \<< V\-> ROT V\-> ROT       + ROT ROT - * +   \>> DOSUBS   2 / \>>

Example:
{
[ 2 1 ]
[ 4 5 ]
[ 7 8 ]
}

Area = 25.5

Cheers
Thomas
11-17-2015, 06:35 PM (This post was last modified: 11-17-2015 07:08 PM by Marcio.)
Post: #3 Marcio Senior Member Posts: 438 Joined: Feb 2015
RE: [HP-50G/49G+] Trapezoidal rule integration in RPL
Shorter than I thought possible. Thanks!

Back in 2007-8 when I purchased the 50G, I had lots of data in txt files that I needed to integrate to calculate the amount of material passing through pipes etc, well, and I if remember correctly, when txt data is imported into the 50g, it is transformed into a list of vectors. As I didn't know to operate with lists, I designed a simple program to convert lists to arrays so that the program I posted would work flawlessly. Later on I merged those two into one and was very happy my 50g would handle such large amounts of data and all I needed extra was an SD card.

Marcio
11-17-2015, 07:04 PM (This post was last modified: 11-17-2015 07:10 PM by Thomas Klemm.)
Post: #4
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: [HP-50G/49G+] Trapezoidal rule integration in RPL
If you're willing to add $$[x_1, 0]$$ at the beginning and $$[x_n, 0]$$ at the end of the list you can use:

Code:
\<< 2   \<< CROSS \>>   DOSUBS \GSLIST 2 /   3 GET NEG \>>

Example:
{
[ 2 0 ]
[ 2 1 ]
[ 4 5 ]
[ 7 8 ]
[ 7 0 ]
}

Area = 25.5

Compare it to this post.

Cheers
Thomas
11-17-2015, 07:13 PM
Post: #5 Marcio Senior Member Posts: 438 Joined: Feb 2015
RE: [HP-50G/49G+] Trapezoidal rule integration in RPL
... and RPL shows its power! Not even high-level languages such as the one Matlab uses could have used so few commands to do exactly the same thing.

Thanks
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)