Open main menu

Changes

DPL94 derivatives

5,633 bytes added, 08:16, 13 September 2014
Added an introductory sentence and see also section. This is to move this page out of the dead-end page list (http://wiki.nmr-relax.com/Special:DeadendPages).
These are the first and second partial derivatives of the equations of the [[DPL94]] [[:Category:Relaxation dispersion analysis|relaxation dispersion]] model.
 
== Equation ==
<math>
\mathrm{R}_{1\rho}= \mathrm{R}_1\cos^2\theta + \left( \mathrm{R}_{1\rho}{´} + \frac{\Phi_\textrm{ex} \textrm{k}_\textrm{ex}}{\textrm{k}_\textrm{ex}^2 + \omega_\textrm{e}^2} \right) \sin^2\theta
</math>
 
== Jacobian ==
=== sympy ===
<source lang="python">
asfrom 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 functionf = 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.1theta = pi / 4R1rho_p = 10.phi_ex = 1100.kex = 2200.we = 3300. d_f_d_R1 = %sd_f_d_theta = %sd_f_d_R1rho_p = %sd_f_d_phi_ex = %sd_f_d_kex = %sd_f_d_we = %sjacobian_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) ) #### Method 2# http://docs.sympy.org/0.7.2/modules/matrices/matrices.html # The vectorial function.X = Matrix([R1 * cos(theta)**2 + (R1rho_p + ( (phi_ex * kex) / (kex**2 + we**2) ) ) * sin(theta)**2])# What to derive for.Y = Matrix([R1, theta, R1rho_p, phi_ex, kex, we]) # Make the JacobianJacobian = X.jacobian(Y) jac_string = str(Jacobian)jac_string_arr = jac_string.replace("Matrix", "array") print("""Form the Jacobian matrix by:------------------------------------------------------------------------------from numpy import array, cos, sin, pi, transpose R1 = 1.1theta = pi / 4R1rho_p = 10.phi_ex = 1100.kex = 2200.we = 3300. jacobian_matrix_2 = %s print jacobian_matrix_2------------------------------------------------------------------------------""" % (jac_string_arr) )
</source>
=== output ===
outputis
<source lang="python">
from numpy import array, cos, sin, pi, transpose
print jacobian_matrix
 
R1 = 1.1
theta = pi / 4
R1rho_p = 10.
phi_ex = 1100.
kex = 2200.
we = 3300.
 
jacobian_matrix_2 = array([[cos(theta)**2, -2*R1*sin(theta)*cos(theta) + 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta),
sin(theta)**2, kex*sin(theta)**2/(kex**2 + we**2), (-2*kex**2*phi_ex/(kex**2 + we**2)**2 + phi_ex/(kex**2 + we**2))*sin(theta)**2, -2*kex*phi_ex*we*sin(theta)**2/(kex**2 + we**2)**2]])
 
print jacobian_matrix_2
</source>
 
== Hessian ==
=== sympy ===
<source lang="python">
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 Hessian.\n")
 
# Define symbols to derive for.
syms = [R1, theta, R1rho_p, phi_ex, kex, we]
 
# Do the hessian.
hess = hessian(f, syms)
 
hess_string = str(hess)
hess_string_arr = hess_string.replace("Matrix", "array")
 
print("""Form the Hessian matrix by:
------------------------------------------------------------------------------
from numpy import array, cos, sin, pi
 
R1 = 1.1
theta = pi / 4
R1rho_p = 10.
phi_ex = 1100.
kex = 2200.
we = 3300.
 
hessian_matrix = %s
 
print hessian_matrix
------------------------------------------------------------------------------
""" % (hess_string_arr) )
</source>
 
=== output ===
output is
<source lang="python">
from numpy import array, cos, sin, pi
 
R1 = 1.1
theta = pi / 4
R1rho_p = 10.
phi_ex = 1100.
kex = 2200.
we = 3300.
 
hessian_matrix = array([[0, -2*sin(theta)*cos(theta), 0, 0, 0, 0],
[-2*sin(theta)*cos(theta), 2*R1*sin(theta)**2 - 2*R1*cos(theta)**2 - 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*sin(theta)**2 + 2*(R1rho_p + kex*phi_ex/(kex**2 + we**2))*cos(theta)**2, 2*sin(theta)*cos(theta),
2*kex*sin(theta)*cos(theta)/(kex**2 + we**2), 2*(-2*kex**2*phi_ex/(kex**2 + we**2)**2 + phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta), -4*kex*phi_ex*we*sin(theta)*cos(theta)/(kex**2 + we**2)**2],
[0, 2*sin(theta)*cos(theta), 0, 0, 0, 0], [0, 2*kex*sin(theta)*cos(theta)/(kex**2 + we**2), 0, 0, -2*kex**2*sin(theta)**2/(kex**2 + we**2)**2 + sin(theta)**2/(kex**2 + we**2), -2*kex*we*sin(theta)**2/(kex**2 + we**2)**2],
[0, 2*(-2*kex**2*phi_ex/(kex**2 + we**2)**2 + phi_ex/(kex**2 + we**2))*sin(theta)*cos(theta), 0, -2*kex**2*sin(theta)**2/(kex**2 + we**2)**2 + sin(theta)**2/(kex**2 + we**2),
(8*kex**3*phi_ex/(kex**2 + we**2)**3 - 6*kex*phi_ex/(kex**2 + we**2)**2)*sin(theta)**2, (8*kex**2*phi_ex*we/(kex**2 + we**2)**3 - 2*phi_ex*we/(kex**2 + we**2)**2)*sin(theta)**2],
[0, -4*kex*phi_ex*we*sin(theta)*cos(theta)/(kex**2 + we**2)**2, 0, -2*kex*we*sin(theta)**2/(kex**2 + we**2)**2,
(8*kex**2*phi_ex*we/(kex**2 + we**2)**3 - 2*phi_ex*we/(kex**2 + we**2)**2)*sin(theta)**2, 8*kex*phi_ex*we**2*sin(theta)**2/(kex**2 + we**2)**3 - 2*kex*phi_ex*sin(theta)**2/(kex**2 + we**2)**2]])
 
print hessian_matrix
 
</source>
 
== See also ==
[[Category:Relaxation_dispersion analysis]]
Trusted, Bureaucrats
4,223

edits