0

I've tried using matrices, and it has failed. I've looked at external modules and external programs, but none of it has worked. If someone could share some tips or code that would be helpful, thanks.

JShoe
  • 3,186
  • 9
  • 38
  • 61
  • 1
    Could you share the equations in question? (It is possible they are inconsistent - ie have no solution - or not fully constrained - more than one solution). – Hugh Bothwell Jun 01 '12 at 02:06
  • They're arbitrary. Mainly I'm solving for the coefficients of an equation like so: ax^4 + bx^3 + cx^2 + dx + c so for example: a + b + c + d + e = 1 16a + 8b + 4c + 2d + e = 2 81a + 27b + 9c + 3d + e = 3 256a + 64b + 16c + 4d + e = 4 625a + 125b + 25c + 5d + e = 8 They are consistant and fully constrained – JShoe Jun 01 '12 at 02:07

3 Answers3

5

I'm not sure what you mean when you say the matrix methods don't work. That's the standard way of solving these types of problems.

From a linear algebra standpoint, solving 5 linear equations is trivial. It can be solved using any number of methods. You can use Gaussian elimination, finding the inverse, Cramer's rule, etc.

If you're lazy, you can always resort to libraries. Sympy and Numpy can both solve linear equations with ease.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • The answer just didn't come out right. I don't know why. I know that matrices should normally work, and that it's standard, I'm just having difficulty implementing it. – JShoe Jun 01 '12 at 02:11
  • @JShoe: That sounds like the perfect candidate for a question on SO. You should solve the problem if possible, not work around it! – tskuzzy Jun 01 '12 at 02:12
5
import numpy
import scipy.linalg

m = numpy.matrix([
    [1, 1, 1, 1, 1],
    [16, 8, 4, 2, 1],
    [81, 27, 9, 3, 1],
    [256, 64, 16, 4, 1],
    [625, 125, 25, 5, 1]
])

res = numpy.matrix([[1],[2],[3],[4],[8]])

print scipy.linalg.solve(m, res)

returns

[[ 0.125]
 [-1.25 ]
 [ 4.375]
 [-5.25 ]
 [ 3.   ]]

(your solution coefficients for a,b,c,d,e)

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

Perhaps you're using matrices in a wrong way.

Matrices are just like lists within lists.

[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1,1]]

The aforementioned code would make a list that you can access like mylist[y][x] as the axes are swapped.

Oskar
  • 1,321
  • 9
  • 19
Name McChange
  • 2,750
  • 5
  • 27
  • 46
  • I definitely know how to make a matrix. Thanks though. – JShoe Jun 01 '12 at 02:16
  • @JShoe Why the downvote? I thought maybe you were using it wrong, as the y and x are flipped. Also, I'm not really sure why you are using Numpy or Scipy, as it seems kind of overkill for what you are trying to do. – Name McChange Jun 01 '12 at 02:32