# HP Forums

Full Version: (50G) (49G+) Trapezoidal rule integration in RPL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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
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
... 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
Reference URL's
• HP Forums: https://www.hpmuseum.org/forum/index.php
• :