Tuesday, February 13, 2018

HP Prime: TrigPlot App (HP Prime Custom app Tutorial)

HP Prime: TrigPlot App  (HP Prime Custom app Tutorial)

Introduction

Here is another example custom app, the TrigPlot, which plots the following equation:

y = A * sin(Bx + C) + D * cos(Ex + F) + G * sin(Hx + I)^2 + J * cos(Kx + L)^2

where all angles are in radians.  The app uses the global variables A through L. 

Keys

[ Num ]:  The user enters values for A – L.  They must be real (not complex or have a (-1) component)

[ Plot ]:  Enter plot parameters and choose the color of the plot (purple, blue, green, gold, red).  The plot is against a black background.

[ View ], Erase A-L:  Sets the variables A through L to 0 (zero).

[ Info ]:  Displays information about the app

Notes



The Info section introduces the user to the app’s concept:

Info()
BEGIN
// This is when the user presses
// Shift+Apps (Info), use Print
// commands

PRINT();

PRINT("TrigPlot App");
PRINT("------------");
PRINT("By Edward Shore");
PRINT("------------");
PRINT("This app plots the equation");
PRINT("Y = A*SIN(B*X+C) + D*COS(E*X+F) +");
PRINT("G*SIN(H*X+I)^2 + J*COS(K*X+L)^2");
PRINT("------------");
PRINT("February 2018");

END;


To ensure the app always runs in Radians mode, I store the value 1 to AAngle during startup (START).  In addition, I added the Info(); to show the Info screen.

START()
BEGIN
// This happens when the app is started.

// Set the app to radians angle mode.
AAngle:=1;
// This is a specific app mode setting
// unlike HAngle

// Also run the Info screen
Info();

END;

The Plot portion of app is based on the Pixel Plot program (see http://edspi31415.blogspot.com/2018/02/hp-prime-pixel-plot-how-to-change.html ) where pixels are plotted.

HP Prime Program:  TrigPlot (in app TrigPlot)

#pragma mode( separator(.,;) integer(h32) )

//Symb()
//BEGIN
// MSGBOX("Symb");
//END;

Plot()
BEGIN
// runs the Plot routine
// set color scheme
LOCAL col1;
col1:={#BF00FFh,#7DF9FFh,
#00FF00h,#D4AF37h,#FF0000h};

// Radians
HAngle:=0;

// localize variables
LOCAL xm,xn,ym,yn;
LOCAL xs,ys,xp,yp;
LOCAL x,y,ch;

// input screen
INPUT({
xm,xn,ym,yn,
{ch,
{"Purple","Blue","Green",
"Gold","Red"}}},
"TrigPlot Setup: Plot",
{"x-min: ","x-max: ",
"y-min: ","y-max: ",
"Color: "});

// set black background
RECT_P(0);

// calulate the scale
xs:=(xn-xm)/320;
ys:=(yn-ym)/−220;

// drawing

// color choice
LOCAL c1:=col1[ch];

// axis information
LOCAL st1,st2;
st1:="x:["+xm+","+xn+"]";
st2:="y:["+ym+","+yn+"]";
TEXTOUT_P(st1,0,0,2,#C0C0C0h);
TEXTOUT_P(st2,0,20,2,#C0C0C0h);

// function
FOR x FROM xm TO xn STEP xs DO

// function
y:=A*SIN(B*x+C)+D*COS(E*x+F)+
G*SIN(H*x+I)^2+J*COS(K*x+L)^2;

// point→pixel, plot
xp:=(x-xm)/xs;
yp:=(y-yn)/ys;
PIXON_P(xp,yp,c1);

END;
// freeze screen
FREEZE;

END;

Num()
BEGIN
// This is where all the variables are
// entered, in one easy spot. A-L are
// global in this application.
INPUT({A,B,C,D,E,F,G,H,I,J,K,L},
"TrigPlot Variables");
END;

//SymbSetup()
//BEGIN
// MSGBOX("SymbSetup");
//END;

//PlotSetup()
//BEGIN
// MSGBOX("PlotSetup");
//END;

//NumSetup()
//BEGIN
// MSGBOX("NumSetup");
//END;

Info()
BEGIN
// This is when the user presses
// Shift+Apps (Info), use Print
// commands

PRINT();

PRINT("TrigPlot App");
PRINT("------------");
PRINT("By Edward Shore");
PRINT("------------");
PRINT("This app plots the equation");
PRINT("Y = A*SIN(B*X+C) + D*COS(E*X+F) +");
PRINT("G*SIN(H*X+I)^2 + J*COS(K*X+L)^2");
PRINT("------------");
PRINT("February 2018");

END;

START()
BEGIN
// This happens when the app is started.

// Set the app to radians angle mode.
AAngle:=1;
// This is a specific app mode setting
// unlike HAngle

// Also run the Info screen
Info();

END;

//RESET()
//BEGIN
//MSGBOX("RESET");
//END;

//VIEW "Views", Views()
//BEGIN
// MSGBOX("Views");
//END;

VIEW "Erase A-L", ERASEAL()
BEGIN
A:=0; B:=0; C:=0; D:=0; E:=0; F:=0;
G:=0; H:=0; I:=0; J:=0; K:=0; L:=0;
END;

Examples

Example 1:  y = 2 sin(x + 2) – 3 cos x
A = 2, B = 1, C = 2, D = -3, E = 1 (the rest are zero)



Example 2:  y = 2 cos(2x) + 1.5 * cos^2 x
D = 2, E = 2, J = 1.5, K = 1 (the rest are zero)



Eddie


This blog is property of Edward Shore, 2018.

No comments:

Post a Comment

HP 35S: Trapezoid Analysis (Daniel Pedro Levia)

HP 35S: Trapezoid Analysis (Daniel Pedro Levia) Have some time to squeeze in a post.   Much thanks to Daniel Pedro Levia. Introdu...