2012-09-13 4 views
2

파이썬을 사용하여 6 개의 비선형 방정식 시스템을 풀려고합니다. 9 개의 변수가 있으며 그 중 3 개가 미리 결정됩니다 (6 개의 미지수가있는 6 개의 방정식 시스템이 남음). 문제는 그것이 3 일 수 있다는 것입니다. 나는 미리 알 방법이 없습니다.미리 정의 된 매개 변수를 scipy.optimize.fsolve로 넘김

다음은 방정식입니다 (관심있는 경우).

C11 * C12 + C21 * C22 + C31 * C32 = 0

C11 * C13 + C21 * C23 + C31 * C33 = 0

C12 * C13 + C22 * C23 + C32 * C33 = 0

C11 * C21 + C12 * C22 + C13 * C23 = 0

C11 * C31 + C12 * C32 + C13 * C33 = 0

C21 * C31 + C22 * C32 + C23 * C33 = 0


참고 : 이것은 가장 빠르고 쉽게 해결할 수있는 방법입니다. 또 다른 가능한 식입니다 :

|c11 c21 c31| 
A = |c12 c22 c32| 
    |c13 c23 c33| 

    |c11 c12 c13| 
B = |c21 c22 c23| 
    |c31 c32 c33| 

     |1 0 0| 
A*B = |0 1 0| 
     |0 0 1| 

내 질문은 : 고정 이들의 3을 설정하고 scipy.optimize.fsolve이 (? 또는 더 적합한 모듈) 나머지에 대한 해결하기 위해 어쨌든이 매개 변수?

답변

0

그래서 유효한 해결책을 찾았습니다. 최상의 솔루션인지는 확실하지 않지만 기능적입니다.

내 질문에 대답하기 위해 scipy.optimize.fsolve는 인수 args = (여기에 추가 인수)를 취합니다. 여기에 미리 결정된 매개 변수를 넣었습니다. 함수가 호출되면 args가 먼저 구문 분석되고 3 개의 미리 정해진 값이 적절한 지점에 배치됩니다.

나머지 6 개의 변수가 목록에 전달되고 나머지 간격을 채우기 위해 반복됩니다. 인수가 변경되지 않으므로 각 변수는 항상 행렬의 동일한 지점에 배치됩니다.

이 방법을 사용하면 3 개의 행렬 요소를 미리 결정할 수 있으며 fsolve는 나머지를 결정하려고 시도합니다.

paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False) 

knwonVals 소정 매개 변수의 목록이며, 내가 시작 추측 (6 개없는 매개 변수가 같은 시작 추측을 얻을)입니다 : fsolve에 대한

호출하는 문은 다음과 같습니다. full_output은 선택적 출력을 반환하도록 허용하고 warning = False는 솔루션을 찾을 수 없을 때 나타나는 경고 메시지를 끕니다. 자세한 내용은 다음을 참조하십시오. http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html

관심있는 사람들은 아래의 코드를 참조하십시오.

import scipy 
from scipy.optimize import fsolve 

def func(params, *args): 
    c = propMatrix(createMatrix(args), params) 

    ans =(scipy.dot(c[:, 0],c[:, 1]), scipy.dot(c[:, 1],c[:, 2]), scipy.dot(c[:, 0],c[:, 2]),scipy.dot(c[:, 0],c[:, 0])-1,scipy.dot(c[:, 1],c[:, 1])-1,scipy.dot(c[:, 2],c[:, 2])-1) 
    return ans 

def createMatrix(knownVals): 

    c = [['____', '____', '____'],['____', '____', '____'], ['____', '____', '____']] 

    for element in knownVals: 
     x, y, val = element 
     c[y][x] = float(val) 
    return c 

def propMatrix(c, params): 
    for p in params: 
     assign = True 
     for x in range(3): 
      for y in range(3): 
       if c[x][y]=='____' and assign: 
        c[x][y] = float(p) 
        assign = False 

    return scipy.array(c) 

def test(c): 
    v1 = c[:, 0] 
    v2 = c[:, 1] 
    v3 = c[:, 2] 
    h1 = c[0, :] 
    h2 = c[1, :] 
    h3 = c[2, :] 
    ans = (scipy.dot(v1,v1)-1, scipy.dot(v1,v2), scipy.dot(v1, v3), scipy.dot(v2, v2)-1, scipy.dot(v2, v3), scipy.dot(v3,v3)-1, scipy.dot(h1,h1)-1, scipy.dot(h1,h2), scipy.dot(h1, h3), scipy.dot(h2, h2)-1, scipy.dot(h2, h3), scipy.dot(h3,h3)-1) 
    return ans 

def getInput(): 
    knownVals = [] 
    print """\n\nThis module analytically solves for the rotation matrix\n 
    First, enter 3 known values of the matrix:\n 
       x 
      1 2 3 
     1 | c11 c12 c13 | 
    y 2 | c21 c22 c23 | 
     3 | c31 c32 c33 |\n\n""" 

    for i in range(3): 
     invalid = True 
     print "Point Number %i:"%(i) 
     while invalid: 
      x = int(raw_input("\tx-coordinate:"))-1 
      if x>2 or x<0: 
       print "\tInvalid x-coordinate." 
      else: 
       invalid = False 
     invalid = True 
     while invalid: 
      y = int(raw_input("\ty-coordinate:"))-1 
      if y>2 or y<0: 
       print "\tInvalid y-coordinate." 
      else: 
       invalid = False 
     invalid = True 
     while invalid: 
      val = float(raw_input("\tValue:")) 
      if val>1 or val<-1: 
       print "\tInvalid value. Must be -1 <= value <= 1" 
      else: 
       invalid = False 
     knownVals.append((x, y, val)) 
    c = createMatrix(knownVals) 
    print "Input Matrix:\n\n", scipy.array(c) 
    choice = raw_input("\nIs this correct (y/n)? ") 
    if choice == "y": 
     return knownVals 
    elif choice == "n": 
     return getInput() 

def Main(): 
    solution = False 
    knownVals = getInput() 
    for i in (-1,-.5,0,.5,1): 
     paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False) 
     if ier == 1: 
      print "\nInitial value: %r"%(i) 
      print propMatrix(createMatrix(knownVals),paramSolve1) 
      solution = True 
    if not solution: 
     print "Could not find a valid solution" 

scipy.set_printoptions(precision = 4, suppress = True) 
Main() 
관련 문제