mailr25275 - in /trunk/test_suite/shared_data/curve_fitting/numeric_gradient: ./ integrate.log integrate.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on August 26, 2014 - 12:06:
Author: bugman
Date: Tue Aug 26 12:06:18 2014
New Revision: 25275

URL: http://svn.gna.org/viewcvs/relax?rev=25275&view=rev
Log:
Added a script and log file for calculating the numerical gradient for an 
exponential curve.

This uses the data at 
http://thread.gmane.org/gmane.science.nmr.relax.devel/6807/focus=6840 and
calculates the gradient using the scipy.misc.derivative() function both at 
the minimum and at a
point away from the minimum.  The values will be used to construct a unit 
test to check the C module
implementation.


Added:
    trunk/test_suite/shared_data/curve_fitting/numeric_gradient/
    trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log
    trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py

Added: 
trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log
URL: 
http://svn.gna.org/viewcvs/relax/trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log?rev=25275&view=auto
==============================================================================
--- trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log 
  (added)
+++ trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.log 
  Tue Aug 26 12:06:18 2014
@@ -0,0 +1,4 @@
+The gradient at [1, 1000] is:
+    [-1.0995282792650802e-09, 2.1826111665238544e-12]
+The gradient at [2, 500] is:
+    [722.67864120737488, -11.564651301654292]

Added: 
trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py?rev=25275&view=auto
==============================================================================
--- trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py  
  (added)
+++ trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py  
  Tue Aug 26 12:06:18 2014
@@ -0,0 +1,70 @@
+# Script for numerically calculating the exponential curve gradient.
+
+# Python module imports.
+from math import exp
+from numpy import array, float64
+from scipy.misc import derivative
+
+
+def func_R(R):
+    """Calculate the chi-squared value."""
+
+    global times, I, I0, errors
+
+    # The intensities.
+    back_calc = []
+    for i in range(len(times)):
+        back_calc.append(I0 * exp(-R*times[i]))
+
+    # The chi2.
+    chi2 = 0.0
+    for i in range(len(times)):
+        chi2 += (I[i] - back_calc[i])**2 / errors[i]**2
+
+    # Return the value.
+    return chi2
+
+
+def func_I(I0):
+    """Calculate the chi-squared value."""
+
+    global times, I, R, errors
+
+    # The intensities.
+    back_calc = []
+    for i in range(len(times)):
+        back_calc.append(I0 * exp(-R*times[i]))
+
+    # The chi2.
+    chi2 = 0.0
+    for i in range(len(times)):
+        chi2 += (I[i] - back_calc[i])**2 / errors[i]**2
+
+    # Return the value.
+    return chi2
+
+
+# The real parameters.
+R = 1
+I0 = 1000
+
+# The time points.
+times = [0, 1, 2, 3, 4]
+
+# The intensities for the above I0 and R.
+I = [1000.0, 367.879441171, 135.335283237, 49.7870683679, 18.3156388887]
+
+# The intensity errors.
+errors = [10, 10, 10, 10, 10]
+
+# The numeric gradient at the minimum.
+grad_R = derivative(func_R, R, dx=1e-5, order=11)
+grad_I = derivative(func_I, I0, dx=1e-5, order=11)
+print("The gradient at %s is:\n    %s" % ([R, I0], [grad_R, grad_I]))
+
+# The numeric gradient off the minimum.
+R_off = 2
+I0_off = 500
+grad_R = derivative(func_R, R_off, dx=1e-5, order=11)
+grad_I = derivative(func_I, I0_off, dx=1e-5, order=11)
+print("The gradient at %s is:\n    %s" % ([R_off, I0_off], [grad_R, grad_I]))




Related Messages


Powered by MHonArc, Updated Tue Aug 26 12:20:02 2014