-1

Attached below is my code

import sympy as sym

k = [3,10,0.65,0.3]
x = [999, 1, 0] 
SU, IN, IM= x[0], x[1], x[2]

def equation():   
    dsdt = '(IM/k0) - ((IN/(IN+SU))*k1*k2*SU)'
    dindt = '((IN/(IN+SU))*k1*k2*SU) - (IN/k3)'
    dimdt = '(IN/k3) - (IM/k0)'
    return [dsdt, dindt, dimdt]

def Jacobian(v_str, f_list):
    vars = sym.symbols(v_str)
    f = sym.sympify(f_list)
    J = sym.zeros(len(f),len(vars))
    for i, fi in enumerate(f):
        for j, s in enumerate(vars):
            J[i,j] = sym.diff(fi, s)
    return J

Jacobian('k0 k1 k2 k3', equation())

I need the return value to be in numpy.array for further processing... now im just hard coding it. Using jupyter notebook, python version 3.7.6

Sample output:

sample output

JohanC
  • 71,591
  • 8
  • 33
  • 66
zhikai
  • 1
  • 1

1 Answers1

0

If you want a numpy array then the following will work: flatten the matrix and pass it to array:

from numpy import array
from sympy import flatten, Matrix
print(array(flatten(Matrix(equation()).jacobian(var('k0 k1 k2 k3')))))
smichr
  • 16,948
  • 2
  • 27
  • 34