Showing posts with label sign function. Show all posts
Showing posts with label sign function. Show all posts

Saturday, December 5, 2020

HP 71B: Sign "Graph" of Trig Functions

 HP 71B: Sign "Graph" of Trig Functions


Introduction


The program SGNTRIG builds a 22 character binary string that depends on the function:


g(x) = 

1  if  sgn(t(x/c)) > 1

0  if  sgn(t(x/c)) ≤ 0


where:


c is a scaling factor, 


sgn(x) is the sign function,  and


t(x) represents one of three trigonometric functions, sin(x), cos(x), or tan(x).   Angles are assumed to be radians.


If we set to scale to c = 1, then g(x) takes the integer values from 1 to 22.  


The HP 71B builds the result to the string S$.   This is an aid to visualize at least some of the graph.   When the bit is 1, the graph is above the x-axis, otherwise, the graph is either on or below the x-axis.  The program presented can be expanded or modified to explore other functions.


The resulting string is a "psuedo-graph".  


Below is a graphical representation of g(x) (an HP Prime is used to graph g(x)).


HP 71B Program SGNTRIG

Size:  270 bytes


100 DESTROY S,I,N,E,C

105 S$=""

110 RADIANS

115 INPUT 'SCALE? ';C

120 DISP "S:SIN C:COS T:TAN"

125 E$=KEY$

130 IF E$="S" OR E$="C" OR E$="T" THEN 200 ELSE 120


200 FOR I=1 TO 22

205 IF E$="S" THEN N=SGN(SIN(I/C))

210 IF E$="C" THEN N=SGN(COS(I/C))

215 IF E$="T" THEN N=SGN(TAN(I/C))

220 IF N=1 THEN N=1 ELSE N=0

225 S$=S$&STR$(N)

230 DISP S$ @ BEEP 589,0.1 @ WAIT 0.2

235 NEXT I

240 DISP S$ @ BEEP 661,2


Notes:


Line 105:  creates a blank string, which is allowed on the HP 71B


Line 125:  The KEY$ function calls for a key input. S for sine, C for cosine, and T for tangent.


Lines 205 to 215: determine which trigonometric function to use


Line 220:  Sets the character to 0 if the sign function returns 0 or -1


Line 225:  &, the ampersand symbol concatenates two strings


Lines 230 and 240, respectively:  589 Hz is middle D, 661 Hz is middle E


Example


Below are results for each of the trigonometric functions when scale is set to 1.  




YouTube video: https://www.youtube.com/watch?v=GL6urOGvRGY&feature=youtu.be

Eddie


All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Friday, December 28, 2018

HP 42S/DM 42/Free42: Argument, Real Part, Imaginary Part, and Sign Function

HP 42S/DM 42/Free42: Argument, Real Part, Imaginary Part, and Sign Function

Introduction

The HP 42S* has only one extraction function for complex numbers: ABS (absolute value) by default.  To complete the list, here are some programs for ARG (argument), REAL (real part), and IMAG (imaginary part).  The three functions take a complex number, in either rectangular or polar form. 

Using stack commands and flag checks, I am able to get obtain results without affecting the stack much (although the z and t stacks will have the same value).

Also included is the SIGN (signum) function.

* This applies to the Swiss Micros DM42 (it should work), Free42 from Thomas Oakken, and any other HP 42S emulator apps.  The print out is from Free42 (and I also have a physical HP 42S).

HP 42S Program: ARG

00 { 19-Byte Prgm }
01▸LBL "ARG"
02 COMPLEX
03 X<>Y
04 FC? 73
05 →POL
06 R↑
07 STO ST Y
08 R↓
09 R↓
10 RTN
11 .END.

HP 42S Program:  REAL

00 { 21-Byte Prgm }
01▸LBL "REAL"
02 COMPLEX
03 X<>Y
04 FS? 73
05 →REC
06 X<>Y
07 R↑
08 STO ST Y
09 R↓
10 R↓
11 RTN
12 END

HP 42S Program: IMAG

00 { 20-Byte Prgm }
01▸LBL "IMAG"
02 COMPLEX
03 X<>Y
04 FS? 73
05 →REC
06 R↑
07 STO ST Y
08 R↓
09 R↓
10 RTN
11 END

Example - Rectangular Mode:

7+8i   (7 [ENTER] 8 [ (shift) ] (COMPLEX) )
XEQ ARG:  48.8141 (Degrees)
XEQ REAL: 7.0000
XEQ IMAG: 8.0000

Example - Polar Mode: (Degrees):

4 ∡ 60  (4 [ENTER] 60 [ (shift) ] (COMPLEX) )
XEQ ARG:  60.0000
XEQ REAL:  2.0000
XEQ IMAG:  3.4641

HP 42S Program:  SIGN

00 { 15-Byte Prgm }
01▸LBL "SIGN"
02 X=0?
03 RTN
04 ENTER
05 ABS
06 X<>Y
07 ÷
08 RTN
09 END

Example:

-1.4641  XEQ SIGN returns -1
5.525 XEQ SIGN returns 1
0 XEQ SIGN returns 0

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Thursday, October 6, 2016

HP 12C Programming Tricks



HP 12C Programming Tricks


These tricks can help you cut steps in your programming.  Remember that it is still important to test results.  Happy programming!

Using Last X

This can be a god-send in making programs more efficient.  Unless you are using the Platinum Edition, we only have 99 steps to work with.  The LST X function returns the x argument that is last used in a calculation.

Function
LST x Returns
One-Argument Functions:
√, LN, e^x, FRAC, INTG, n!
The x pre-calculation
Two-Argument Functions (Arithmetic):
+, -, *, ÷, y^x
The x value in the arithmetic operation

Number of Inputs

I follow a general rule when it comes to the number of inputs required on an HP 12C program.  If the number of inputs is 1 or 2, I would have the user enter inputs on the stack and run the program.  If the number of inputs is 3 or more, I have the user pre-store the inputs into registers before pressing [R/S] to run the program.  Example:  If my program calls for a, b, and c, I have the user store a in register 0, b in register 1, and c in register 2, and then have the user press [R/S].

Branching

You can branch a program to different parts of the program.  You designate a register as a choice variable.  (Rc = 0 for option 0, 1 for option 1, etc).  Examples include setting the variable for computing combinations vs permutations, set for month payments vs yearly payments.


Program format:

RCL Rc
Test value
-
X=0?
GTO (applicable line number)
(applicable section)
(commands and calculations)
GTO 00 (end the program)

Multiply x by 100

Keystroke:  [ENTER], 1, [x<>y], [%T]
Result:  The X stack has 100*x, the Y stack has x.

Dividing x by 100

Keystrokes: [ENTER], 1, [ % ]
Results:  The x stack has x/100, the Y stack has x.   

Quickly entering numbers in the form of 10^n (n is an integer)

Keystrokes:  1, [EEX], n
This is useful when n>2.  For example, entering 10000 takes five steps normally.  With this sequence, 1 [EEX] 5, you only use 3. 

Another Way to Double

Keystrokes:  [ENTER], [ + ]
Results: X Stack:  2*x.   1 program step saved.

Absolute Value of x.

Keystrokes:  2, [y^x], [ g ] [ √ ]
Result: X Stack: |x|.  It is important to square the number first since the HP 12C’s square root function returns an error on negative numbers. 

Signum Function of x.
This is the extension of the absolute value sequence shown previously.

Keystrokes:  [ENTER], 2, [y^x], [ g ] ( √ ), [ ÷ ]
Result:  X Stack: sgn(x)

Modulus (for two positive values)

Keystrokes:  a, [ENTER], b, [ ÷ ], [ g ] (LSTx), [x<>y], [ g ] (FRAC), [ * ]
Result:  X stack:  a MOD b

Comparing Values

We have a value stored in a register, call it Rc, where c obviously can stand for 0, 1, and so on. 
Keystrokes: [RCL], c, [ - ], [ g ] (x=0)

If the test is true (x = Rc), the next step is executed.  Otherwise, it is skipped.

Six Digit Approximation of π

Since the HP 12C does not have a Ï€ button, we’ll have to enter a 10 digit approximation of Ï€, which will take 11 steps.  However, if you want an approximation (given most of the time the HP 12C is used on Fix 2 mode), we can use the approximation Ï€ ≈ 355/113, which is accurate to six decimal places.

Ï€ ≈ 3.14159265359
355/113 ≈ 3.14159292035

Keystrokes:  355, [ENTER], 113, [ ÷ ]

Calculate (1 + n/100)^x without using the TVM Registers

Keystrokes:  1, [ENTER], n, [ % ], [ + ], x, [y^x]

I hope you find these tricks useful.  If you have any, please feel free to share them. 

Eddie

This blog is property of Edward Shore, 2016.

Wednesday, May 25, 2016

Absolute Value: Does |x|^2 = x^2?

Absolute Value:  Does |x|^2 = x^2?

Proof that |x|^2 = x^2, assuming x is a real number.

Note that |x| = x/sgn(x), where sgn(x) is the sign function where:

sgn(x) = -1 if x < 0,
sgn(x) = 0 if x = 0,
and sgn(x) = 1 if x > 0

Case: x = 0. 

Then:
|0| = 0 and |0|^2 = 0^2 = 0.

Case: x ≠ 0.  

Then:
|x|^2 = (x/sgn(x))^2
= x/sgn(x) * x/sgn(x)
= x^2/sgn(x)^2

If x < 0, sgn(x) = -1, and since -1 * -1 = 1, sgn(x)^2 = 1
If x > 0, sgn(x) = 1, and since 1 * 1 = 1, sgn(x)^2 = 1

Hence:
x^2/sgn(x)^2
= x^2

QED

Caution:  The statement |x|^2 = x^2 is not true for complex numbers where the imaginary part is nonzero. 

Let x = a + b*i

|x|^2 = |a + b*i|^2 = (√(a^2 + b^2))^2 = a^2 + b^2

x^2 = (a + b*i)^2 = a^2 + 2*a*b*i – b^2 ≠ a^2 + b^2   (b ≠ 0)


Conclude:  |x|^2 = x^2 only if x is a real number.


Eddie


This blog is property of Edward Shore, 2016

Monday, November 3, 2014

Derivative of abs(x) and Integrals of abs(x), abs(e^x), abs(e^(a*x) + e^(a*y))

Derivative of abs(x) and Integrals of abs(x), abs(e^x), abs(e^(a*x) + e^(a*y))

For this blog entry, assume that are functions are in terms of x and a, x, y represent real numbers.

Definition of abs(x) (also symbolized as |x|)

Piecewise definition:

abs(x) = x if x > 0
abs(x) = 0 if x = 0
abs(x) = -x if x < 0

Also:

abs(x) = x * sign(x)

Where the sign(x), sometimes labeled sgn(x), is the sign or signum function. It is defined as:

sign(x) = 1 for x > 0
sign(x) = 0 for x = 0
sign(x) = -1 for x < 0

d/dx sign(x)

We can clearly demonstrate that d/dx sign(x) = 0 since:

d/dx sign(x) = 0 for x > 0
d/dx sign(x) = 0 for x = 0
d/dx sign(x) = 0 for x < 0

d/dx abs(x)

Using the chain rule:
d/dx abs(x)
= d/dx (x * sign(x))
= d/dx (x) * sign(x) + x * d/dx (sign(x))
= sign(x)

This can also be done with the piecewise representation:

d/dx abs(x) = 1 if x > 0
d/dx abs(x) = 0 if x = 0
d/dx abs(x) = -1 if x < 0

∫ abs(x) dx

∫ abs(x) dx
= ∫ x * sign(x) dx

Using integration by parts:
where u = sign(x), dv = x dx
Then: du = 0 dx, v = x^2/2

∫ abs(x) dx
= ∫ x * sign(x) dx
= x^2/2 * sign(x) - ∫ 0 dx
= x^2/2 * sign(x) + C

C is the arbitrary integration constant

With the piecewise representation:

∫ abs(x) dx = x^2/2 if x > 0
∫ abs(x) dx = 0 if x = 0
∫ abs(x) dx = -x^2/2 if x <0

Note that this is x^2/2 * sign(x).

∫ abs(e^(a*x)) dx

∫ abs(e^(a*x)) dx
= ∫ e^(a*x) * sign(e^(a*x)) dx
= 1/a * e^(a*x) * sign(e^(a*x)) - ∫ 0 dx
= 1/a * e^(a*x) * sign(e^(a*x)) + C

∫ abs(e^(a*x) + e^(a*y)) dx

∫ abs(e^(a*x) + e^(a*y)) dx
= ∫ (e^(a*x) + e^(a*y)) * sign(e^(a*x) + e^(a*y)) dx
= (e^(a*x)/a + x*e^(a*y)) * sign(e^(a*x) + e^(a*y)) - ∫ 0 dx
= (e^(a*x) + a*x*e^(a*y))/a * sign(e^(a*x) + e^(a*y)) + C

Eddie

This blog is property of Edward Shore. 2014

(Blog Entry # 401)

Earth's Radius by Latitude

Earth's Radius by Latitude Introduction: Calculating the Earth’s Radius In quick, general calculations, we assume that the...