HP Forums

Full Version: RPL Programming question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I have a question for guru RPL programmers. How do I pass an algebraic expression to an RPL program object so I can evaluate that expression in the object. I understand that I also need to pass the name of the variable that appear in the expression along with a value (at least one).

My goal is to be able to write different RPL program objects that perform various kinds of numerical analysis calculations for algorithms that require an expression/equation (such as root calculations, numerical integration, solving ODEs, and so on).

I appreciate any help.

Namir

If your RPL program is named FOO, and needs an algebraic expression to be passed in on the stack, you would invoke it like:

     '4*X^3-2*X^2+3*X-5' FOO

FOO could keep the algebraic object on the stack, or it might store it into a local variable:

    << -> f << blah blah 3.7 'X' STO blah f EVAL blah blah >> >>

Note that this works for using an RPL program as an argument, too:

    << X COS DUP * >> FOO

Edited: 18 Sept 2007, 2:40 p.m.

Thanks Eric!! I appreciate taking teh time to explain it for me.

Based on your example I created the following program object (and stored it as FOO):

<< -> F V << V STO F EVAL >> >>

Where F and V are variables that store the expression and the name of the variable used in that expression.

So the following sample input gives the correct result of 18:

4 'Z1*Z1+2' 'Z1' FOO

Namir

Edited: 18 Sept 2007, 4:18 p.m.

Eric,

Building on your tip, here a program object that applies Newton's root algorithm:

<< DUP2 _derivative_ -> F V D
<< V STO
DO
F EVAL D EVAL / NEG DUP V STO+
UNTIL
ABS 0.0000001 <
END
V RCL
>>
>>

Note: _derivative_ stands for the derivative operator character.

The variables are:

1. Variable F stores the expression whose root is sought.

2. Variable V stores the name of the variable.

3. Variable D store the derivative of the expression.

To use the above program, enter the input in the following sequence:

1. An initial guess for the root, such as 4.

2. The expression whose root is sought, such as 'EXP(X)-3*SQ(X)'.

3. The name of the variable in the above expression, 'X' in this case.

Invoke the name of the program object to obtain a refined guess for the root.

The beauty of the above RPL object is that it obtains the derivative symbolically and uses it to evaluate the derivative in each iteration. For once, I don't have to worry about calculating an approximation to the derivative. Wooohoooo!

Namir


Edited: 18 Sept 2007, 8:06 p.m.