Difference between revisions of "DPL94 derivatives"

From relax wiki
Jump to navigation Jump to search
(Created page with "== Jacobian == <source lang="python"> from sympy import * # In contrast to other Computer Algebra Systems, in SymPy you have to declare symbolic variables explicitly: R1 = S...")
 
Line 1: Line 1:
 
== Jacobian ==
 
== Jacobian ==
 +
=== sympy ===
 
<source lang="python">
 
<source lang="python">
 
from sympy import *
 
from sympy import *
Line 26: Line 27:
 
print("""Form the Jacobian matrix by:
 
print("""Form the Jacobian matrix by:
 
------------------------------------------------------------------------------
 
------------------------------------------------------------------------------
from numpy import array, transpose
+
from numpy import array, cos, sin, pi, transpose
+
 
 +
R1 = 1.1
 +
theta = pi / 4
 +
R1rho_p = 10.
 +
phi_ex = 1100.
 +
kex = 2200.
 +
we = 3300.
 +
 
 
d_f_d_R1 = %s
 
d_f_d_R1 = %s
 
d_f_d_theta = %s
 
d_f_d_theta = %s
Line 35: Line 43:
 
d_f_d_we = %s
 
d_f_d_we = %s
 
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )
 
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )
 +
 +
print jacobian_matrix
 
------------------------------------------------------------------------------
 
------------------------------------------------------------------------------
 
""" % (d_f_d_R1, d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we) )
 
""" % (d_f_d_R1, d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we) )
 
</source>
 
</source>
 
+
=== output ===
 
output
 
output
 
<source lang="python">
 
<source lang="python">
from numpy import array, transpose
+
from numpy import array, cos, sin, pi, transpose
+
 
 +
R1 = 1.1
 +
theta = pi / 4
 +
R1rho_p = 10.
 +
phi_ex = 1100.
 +
kex = 2200.
 +
we = 3300.
 +
 
 
d_f_d_R1 = cos(theta)**2
 
d_f_d_R1 = cos(theta)**2
 
d_f_d_theta = -2*R1*sin(theta)*cos(theta) + 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta)
 
d_f_d_theta = -2*R1*sin(theta)*cos(theta) + 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta)
Line 50: Line 67:
 
d_f_d_we = -2*kex*phi_ex*we*sin(theta)**2/(kex**2 + we**2)**2
 
d_f_d_we = -2*kex*phi_ex*we*sin(theta)**2/(kex**2 + we**2)**2
 
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )
 
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )
 +
 +
print jacobian_matrix
 
</source>
 
</source>

Revision as of 09:06, 1 September 2014

Jacobian

sympy

from sympy import *
 
# In contrast to other Computer Algebra Systems, in SymPy you have to declare symbolic variables explicitly:
R1 = Symbol('R1')
theta = Symbol('theta')
R1rho_p = Symbol('R1rho_p')
phi_ex = Symbol('phi_ex')
kex = Symbol('kex')
we = Symbol('we')

# Define function
f = R1 * cos(theta)**2 + (R1rho_p + ( (phi_ex * kex) / (kex**2 + we**2) ) ) * sin(theta)**2

print("Now calculate the Jacobian. The partial derivative matrix.\n")
print("Jacobian is m rows with function derivatives and n columns of parameters.")

d_f_d_R1 = diff(f, R1)
d_f_d_theta = diff(f, theta)
d_f_d_R1rho_p = diff(f, R1rho_p)
d_f_d_phi_ex = diff(f, phi_ex)
d_f_d_kex = diff(f, kex)
d_f_d_we = diff(f, we)

print("""Form the Jacobian matrix by:
------------------------------------------------------------------------------
from numpy import array, cos, sin, pi, transpose

R1 = 1.1
theta = pi / 4
R1rho_p = 10.
phi_ex = 1100.
kex = 2200.
we = 3300.

d_f_d_R1 = %s
d_f_d_theta = %s
d_f_d_R1rho_p = %s
d_f_d_phi_ex = %s
d_f_d_kex = %s
d_f_d_we = %s
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )

print jacobian_matrix
------------------------------------------------------------------------------
""" % (d_f_d_R1, d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we) )

output

output

from numpy import array, cos, sin, pi, transpose

R1 = 1.1
theta = pi / 4
R1rho_p = 10.
phi_ex = 1100.
kex = 2200.
we = 3300.

d_f_d_R1 = cos(theta)**2
d_f_d_theta = -2*R1*sin(theta)*cos(theta) + 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta)
d_f_d_R1rho_p = sin(theta)**2
d_f_d_phi_ex = kex*sin(theta)**2/(kex**2 + we**2)
d_f_d_kex = (-2*kex**2*phi_ex/(kex**2 + we**2)**2 + phi_ex/(kex**2 + we**2))*sin(theta)**2
d_f_d_we = -2*kex*phi_ex*we*sin(theta)**2/(kex**2 + we**2)**2
jacobian_matrix = transpose(array( [d_f_d_R1 , d_f_d_theta, d_f_d_R1rho_p, d_f_d_phi_ex, d_f_d_kex, d_f_d_we] ) )

print jacobian_matrix