Help required to solve a linear equation through excel

S

souravroy1

Hi all,
I am trying to write a program wich accepts to two arrays one of
coefficients and other of constants and solve it through Gauss
eliminatiion method, the function directly writes the solution in a
colum starting from the given cell
I am not able to trace the problem with the code. though I am new to
excel programming
Can someone help me

The code is as follows:-
Option Base 1
Sub solution(coefficient As Range, constants As Range, output As Range)
p = coefficient.Rows.Count
n = coefficient.coloums.Count
MsgBox ("HI")
Dim a(), R(), sol()
For v = 1 To p
For j = 1 To n
a(v, j) = coefficient(v, j).Value
Next j
R(v) = constants(v).Value
Next v
For i = 1 To n
check = 0
For j = 1 To n
If Not (a(i, j) = 0) Then check = 1
Next j
If check = 0 Then Count = Count + 1
If Not (R(i) = 0) Then
For d = 1 To n
e = a(i, d)
a(i, d) = a((p - Count), d)
a((p - Count), d) = e
Next d
End If
For k = 1 To i
For l = 1 To (i - 1)
coef = a(i, l)
For m = 1 To n
a(i, m) = a(i, m) - (coef * a(l, m))
Next m
R(i) = R(i) - (coef * R(l))
Next l
Next k
Next i
If ((p - Count) < n) Then
output.Value = " infinite solution"
Else
For c = n To 1
sol(c) = R(c)
For b = n To (c - 1)
sol(c) = sol(c) - (a(b, b) * sol(b))
Next b
Next c
End If
For ab = 1 To n
output.Cells(ab, 0) = sol(i)
Next ab
End Sub

thanks
Sourav
 
D

Dana DeLouis

Hi. Here's some initial thoughts.
n = coefficient.coloums.Count
n = coefficient.Columns.Count
Dim a(), R(), sol()
Maybe something like this:

Dim a(), R(), Sol()

p = coefficient.Rows.Count
n = coefficient.Columns.Count

ReDim a(1 To p, 1 To n)
ReDim R(1 To p)
ReDim Sol(1 To p)

Now something like this should work...
a(v, j) = coefficient(v, j).Value

If you are trying to solve linear equations, perhaps something like this
small example.

Sub Demo()
Dim M As Variant 'Coefficient Array
Dim RHS As Variant ' RHS Array

M = [A1:C3]
RHS = [E1:E3]
With WorksheetFunction
[G1:G3] = .MMult(.MInverse(M), RHS)
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top