Showing posts with label LSQ. Show all posts
Showing posts with label LSQ. Show all posts

Thursday, February 14, 2019

TI-86: Angle Between Vectors, Vandermonde Matrix, Least Squares Algorithm, Chebyshev Polynomials (1st Kind)


TI-86: Angle Between Vectors, Vandermonde Matrix, Least Squares Algorithm, Chebyshev Polynomials (1st Kind)

The TI-86 has one of the best interfaces for a graphing calculator I ever had the joy to work with. The TI-86 is an update of the TI-85. Here are some programs for the TI-86.

Angle Between Vectors

The program vangle calculates the angle between two angles. The angle is calculated in degrees.



TI-86 Program vangle
(75 bytes)

Prompt V1
Prompt V2
Disp cos⁻¹ (dot(V1,V2)/(norm V1*V2))

Example: [2, -3, 4] and [8, 1, -2]
Angle: 83.5823268926°

Vandermonde Matrix

The program vander creates a matrix based on a list of coefficients.

Example: {x, y, z} produces the matrix

[ [x^0, x^1, x^2], [y^0, y^1, y^2], [z^0, z^1, z^2] ]



TI-86 Program vander
(125 bytes)

Input “List: “, L1
dimL L1 → N
{N, N} → dimL MA
For(R, 1, N)
For(C, 1, N)
L1(R)^(C-1) → MA(R,C)
End
End
Disp “MA=”
Pause MA

Example: {2, 4, 7}
Result: [ [1, 2, 4], [1, 4, 16], [1, 7, 49] ]

Least Square Algorithm

The program LSQ taxes the matrices X and Y (Y is a one-column matrix), and calculates
(X^T X)^-1 (X^T Y).

LSQ is used to fit statistical fits with least squares.



TI-86 Program LSQ
(118 bytes)

Disp “Least Squares”
Input “Matrix X: “,MX
Input “Matrix Y: “,MY
(MX^T * MX)^-1 * (MX^T * MY) → ML
Disp “ML= “
Pause ML

Example:
MX = [ [1, 3, 2.0], [1, 4, 2.3], [1, 5, 2.6], [1, 8, 2.9] ]
MY = [ [1.6], [1.8], [2.1], [2.3] ]

Results:
ML = [ [-0.14444444449], [-1.666666667E-2], [0.88888888889] ]

Chebyshev Polynomials (1st Kind)

The program tcheby calculates the numerical value of the Chebyshev polynomials of the 1st Kind given its point, X, and the order, N.

TI-86 Program tcheby
(127 bytes)

Prompt X,N
If X>1 : Goto A
If X<-1 :="" b="" goto="" span="">
cos(N * cos⁻¹ X) → A
Goto C
Lbl A
cosh(N * cosh⁻¹ X) → A
Goto C
Lbl B
(-1)^N * cosh(N * cosh⁻¹ X) → A
Lbl C
Disp A

Example:
X = -2.5, N = 4, Result: 263.5
X = 0.5, N = 4, Result: -0.5
X = 2.5, N = 4, Result: 263.5

Eddie

All original content copyright, © 2011-2019. 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.

Tuesday, December 16, 2014

HP Prime and HP 50g: Using the LSQ Function to find the Moore-Penrose Inverse of a Matrix

HP Prime and HP 50g:  Using the LSQ Function to find the Moore-Penrose Inverse of a Matrix

  
Recall that the Moore-Penrose Inverse of a Matrix A, A^+, with r rows and c columns is:

If r ≤ c and rank(A) = r  (horizontal matrix), then:
A^+ = A^T * (A*A^T)^-1

If r ≥ c and rank(A) = c  (vertical matrix), then:
A^+ = (A^T * A)^-1 * A^T

You can see the blog entry about the Moore-Penrose inverse here:  http://edspi31415.blogspot.com/2014/08/moore-penrose-inverse-of-matrix.html.

On the LSQ (Least Squares Solution Command) function, featured on both the HP Prime and HP 50G, you can find A^+.

Syntax to find A^+ using the LSQ Command

HP Prime:

LSQ(matrix, IDENMAT(r))

Where IDENMAT is the identity matrix command. 

Path to IDENMAT:  Toolbox, Math, 7. Matrix, 4. Create, 2. Identity
Path to LSQ:  Toolbox, Math, 7. Matrix, 7. Factorize, 2. LSQ

HP 50g:

2:  identity matrix of dimension r x r  (R IDN)
1:  matrix
LSQ

Build the identity matrix by entering r, then [Left Shift], [5] (MATRICES), [F1] (CREATE), [F4] (IDN)
Path to LSQ:  [Left Shift], [5] (MATRICES), [F2] (OPER), [NXT], [F2] (LSQ)

Examples (answers are rounded to five decimal places):

Example 1

M =
[[2, 5],
[3, 3],
[4, 5]]  
(3 x 2 matrix, r = 3)

HP Prime:  LSQ(M, IDENMAT(3))
HP 50g:   2:  (3 IDN), 1: M

Result:

M^+ »
[[-0.45026, 0.31579, 0.21579],
[0.35263, -0.15789, -0.05789]]

Example 2

M =
[[4, 0.3, -0.8, 4.8, 2.5],
[-1.8, 3.6, 4.2, 4.4, -7]] 
(2 x 5, r = 2)

HP Prime:  LSQ(M, IDENMAT(2))
HP 50g:   2:  (2 IDN), 1: M

Result:

M^+ »
[[0.0853, -0.01272],
[0.01109, 0.03586],
[-0.01224, 0.04039],
[0.11059, 0.04939],
[0.04594, -0.06586]]

  This blog is property of Edward Shore. 2014. 


Symmetric Quartic Equation - Python

Symmetric Quartic Equation - Python Note: The code is programmed with a TI-84 Evo. No modules are used, so the scripts should be adopt...