Jump to content



Photo
- - - - -

Calculate A Matrix Inversion Step By Step


  • Please log in to reply
10 replies to this topic

#1 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 25 June 2007 - 10:08 PM

I want to calculate the Inverse of a Matrix by using the Gau? algorithm(via an Identity matrix) on my CFX 9850 GC+

And that should be done step by step.

Means the following:

( 1 3 -4 I 1 0 0 )
( 0 -1 2 I 0 1 0 )
( 0 0 2 I 0 0 1 )
.
.
.
.
( 1 0 0 I 1 3 -1 )
( 0 1 0 I 0 -1 1 ) --> Solved
( 0 0 1 I 0 0 0,5 )

Basically I think, it's possible to use a "stepbystep" Gau? alogrithmus to solve the left matrix. The steps of the solving of the left matrix should also be processed to a Identity matrix saved in Mat B.

If found a Gau? algoritmus program, but it's only working for a Nx(n+1) matrix.

So the first question is how to make it working for a NxN matrix.

Second would be: How can I assign to do the same steps(solving the left matrix) for the identity matrix(the right one)

I spent nearly 5 hours, but with no result.

thx for your help.

Lbl 0
Dim Mat A
List Ans[1->A
List Ans[2->B
B<>A+1=>"MATRIX IS NOT IN
AUGMENTED FORM
N*(N+1)"
B<>A+1=>Goto 0
{1,A}->Dim Mat B
"INITIAL MATRIX"_
Mat A->Mat C_
For 1->K To A-1
For K->J To A
Mat C[J,K]<>0=>Break
Next
A=1=>1->J
Mat C[J,K]=0=>Goto 9
A=1=>Goto 8
If J<>K
Then ClrText
Locate 1,2,"SWAPPING ROWS"
Locate 1,3,K
Locate 1,4,"AND"
Locate 1,5,J_
Swap C,J,K
Mat C_
IfEnd
For K+1->J To A
-Mat C[J,K]%Mat C[K,K->H
If H<>0
Then ClrText
Locate 1,2,"ADDING"
Locate 1,3,H
Locate 1,4,"TIMES ROW"
Locate 1,5,K
Locate 1,6,"TO ROW"
Locate 1,7,J_
*Row+ H,C,K,J
Mat C_
IfEnd
Next
Next
Lbl 8
ClrText
"ELIMINATION COMPLETE"_
Mat C_
Mat C[A,A]->M
M=0=>Goto 9
Mat C[A,A+1]%M->Mat B[1,A
A=1=>Goto 7
For A-1->B To 1 Step -1
0->D
For B+1->C To A
Mat B[1,C]Mat C[B,C]+D->D
Next
(Mat C[B,A+1]-D)%Mat C[B,B->Mat B[1,B
Next
Lbl 7
"THE SOLN IS"_
Mat B_
Goto 0
Lbl 9
"NO UNIQUE SOLN"
Goto 0


#2 caspro

caspro

    Casio Freak

  • Members
  • PipPipPipPip
  • 216 posts

Posted 26 June 2007 - 06:44 AM

Hello e^x,
I will rewrite the gaussian elimination program to calculate the inverse and display step-by-step.

I will post the new program later on today or tomorrow.

You may also be interested in this page:
http://www.math.tu-d...-nm-10-2001.pdf

#3 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 26 June 2007 - 07:04 AM

I wrote a similar program for Lua a long time ago, but not publc it:

Warning: this program doesnt test if the matrix is inversable (this word is right?), you should test if det of the matrix is not equa to 0 before use (afx already has a function for this) or try to calculate it by:

det(A)=a_1_1 * det(M_1_1) - a_1_2 * det(M_1_2) + ... + (-1)^(1+n) * a_1_n * det(M_1_n)

with M_i_j is matrix A which removed row i and column j.

Here is the program:
--Input the matrix size n*n:

print("Input the matrix size...")

n=tonumber(input("n="))


print("Input the matrix elements...")

matrix={}

for i=1,n do
 matrix[i]={}
  for j=1,n do
   matrix[i][j]=tonumber(input("["..i.."]["..j.."]="))
   matrix[i][j+n]=0
  end --for j
 matrix[i][i+n]=1
end --for i

--Start solving
print("Solving...")

--<!--you must test if det(matrix) is not equa to 0 here
--[...]
--end test-->

for r=1,n-1 do
 if matrix[r][r]==0 then
  for r2=r+1,n do
   if matrix[r2][r]~=0 then
	--swap row r and r2
	print("Swap row "..r.." and "..r2.."...")
	for c=1,2*n do
	 local tmp=matrix[r][c]
	 matrix[r][c]=matrix[r2][c]
	 matrix[r2][c]=tmp
	end
	break
   end --if
  end --for r2
 else--if
  for r2=r+1,n do
   local factor=-matrix[r2][r]/matrix[r][r]
   --add Rr2 with k times Rr
   print("R"..r2.."="..factor.."*".."R"..r.."+R"..r2.."...")
   for c=r,2*n do
	matrix[r2][c]=matrix[r2][c]+factor*matrix[r][c]
   end --for c
  end --for r2
 end --else
end --for r

if matrix[n][n]~=0 then
 --Rr=Rr/k
 local factor=matrix[n][n]
 print("R"..n.."=R"..n.."/"..factor)
 for c=n,2*n do
  matrix[n][c]=matrix[n][c]/factor
 end --for c
end--if

for r=n,2,-1 do
 for r2=r-1,1,-1 do
  local factor=-matrix[r2][r]
  print("R"..r2.."="..factor.."*".."R"..r.."+R"..r2.."...")
   for c=r,2*n do
	matrix[r2][c]=matrix[r2][c]+factor*matrix[r][c]
   end --for c
 end --for r2
end --for r


--Display the result
print("\nResult:")
for i=1,n do
 for j=n,1,-1 do
  matrix[i][j]=matrix[i][j+n]
  matrix[i][j+n]=nil
 end
 print(unpack(matrix[i]))
end


#4 caspro

caspro

    Casio Freak

  • Members
  • PipPipPipPip
  • 216 posts

Posted 26 June 2007 - 12:36 PM

@ e^x - Ok, the Matrix Inverse by Gaussian Elimination is now at:
http://www.spiderpix...gramsindex.html
in txt or cat.
Let me know if you find any bugs.

@vanhoa - the word is invertible, but anyway what is the point of posting a LUA program in a thread about CFX-9850
and in a subforum about CFX/AFX/FX, isn't LUA a language for the classpad ?

#5 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 26 June 2007 - 08:23 PM

big thanks to you, CasPro! :thumbsup: thx for you time!

I will run some tests with your program now.

BTW: What's your profession?

#6 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 26 June 2007 - 09:03 PM

I figured out a problem:

When calculating the inverse of
( -3 2 ) (seems to be the problem of all 2x2 matrices
( -4 3 )

then the last step is missing

-3 2 1 0
-4 3 0 1

-3 2 1 0
0 1 -4 3

-3 0 9 -6
0 1 -4 3 --> here it's saying: Inverse complete, showing the right one.

1 0 -3 2
0 1 -4 3


Test result:
-3 2 * -3 2 = 1 0
-4 3 -4 3 0 1

Also tested with:

3 2
1 1

Maybe also implement as a test:

if Mat A * Mat B = Identity Matrix(saved in Mat D)
Then "Solvin true"
Else "Solvin false"
IfEnd



thx

#7 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 26 June 2007 - 09:25 PM

and perhaps it be perfect if you could remove the nxn again... :rolleyes:

If the following is possible:

1. Input a nx(n+1) matrix in Mat A
2. The program cuts off the last column, and save this to another Mat
3. The program calculates the Inverse of the nXn matrix
4. When it's solved it multiplied with the part we cut off

#8 caspro

caspro

    Casio Freak

  • Members
  • PipPipPipPip
  • 216 posts

Posted 26 June 2007 - 09:37 PM

Ok, I've found the bug.

The line that says
For A->C To 2 Step -1
should be
For A->C To 1 Step -1

and perhaps it be perfect if you could remove the nxn again...


I'll see what I can do. :rolleyes:

#9 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 26 June 2007 - 10:45 PM

ah, thx!

#10 caspro

caspro

    Casio Freak

  • Members
  • PipPipPipPip
  • 216 posts

Posted 26 June 2007 - 10:47 PM

I've uploaded a new version
- this time you can enter NxN or NxN+1
- if you enter NxN+1 then it will also display the solution

#11 e^x

e^x

    Newbie

  • Members
  • Pip
  • 13 posts

  • Calculators:
    Fx-991MS
    Fx-991ES
    CFX-9850GC +
    AFX 2.0 +

Posted 27 June 2007 - 12:22 PM

thx again!


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users