Mantid
Loading...
Searching...
No Matches
ExpDecayOsc.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12#include <cmath>
13
15
16using namespace CurveFitting;
17
18using namespace Kernel;
19
20using namespace API;
21
22DECLARE_FUNCTION(ExpDecayOsc)
23
24void ExpDecayOsc::init() {
25 declareParameter("A", 0.2, "Amplitude at time 0");
26 declareParameter("Lambda", 0.2, "Decay rate");
27 declareParameter("Frequency", 0.1, "Frequency of oscillation");
28 declareParameter("Phi", 0.0, "Phase of oscillation at 0 (in Radians)");
29}
30
31void ExpDecayOsc::function1D(double *out, const double *xValues, const size_t nData) const {
32 const double gA0 = getParameter("A");
33 const double gs = getParameter("Lambda");
34 const double gf = getParameter("Frequency");
35 const double gphi = getParameter("Phi");
36
37 for (size_t i = 0; i < nData; i++) {
38 double x = xValues[i];
39 out[i] = gA0 * exp(-gs * x) * cos(2 * M_PI * gf * x + gphi);
40 }
41}
42
43void ExpDecayOsc::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
44 const double gA0 = getParameter("A");
45 const double gs = getParameter("Lambda");
46 const double gf = getParameter("Frequency");
47 const double gphi = getParameter("Phi");
48
49 for (size_t i = 0; i < nData; i++) {
50 double x = xValues[i];
51 double e = exp(-gs * x);
52 double c = cos(2 * M_PI * gf * x + gphi);
53 double s = sin(2 * M_PI * gf * x + gphi);
54 out->set(i, 0, e * c); // derivative w.r.t. A (gA0)
55 out->set(i, 1, -gA0 * x * e * c); // derivative w.r.t Lambda (gs)
56 out->set(i, 2,
57 -gA0 * e * 2 * M_PI * x * s); // derivate w.r.t. Frequency (gf)
58 out->set(i, 3, -gA0 * e * s); // detivative w.r.t Phi (gphi)
59 }
60}
61
62void ExpDecayOsc::setActiveParameter(size_t i, double value) {
63 size_t j = i;
64
65 if (parameterName(j) == "Phi") {
66 // Put angle in range of (-180 to 180] degrees
67 double a = fmod(value, 2 * M_PI);
68 if (a <= -M_PI)
69 a += 2 * M_PI;
70 if (a > M_PI)
71 a -= 2 * M_PI;
72 setParameter(j, a, false);
73 } else
74 setParameter(j, value, false);
75}
76
77} // namespace Mantid::CurveFitting::Functions
double value
The value of the point.
Definition: FitMW.cpp:51
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
Represents the Jacobian in IFitFunction::functionDeriv.
Definition: Jacobian.h:22
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
void setParameter(size_t, const double &value, bool explicitlySet=true) override
Set i-th parameter.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
double getParameter(size_t i) const override
Get i-th parameter.
Provide oscillating exponential decay function: h*exp(-lambda.x)*(cos(2pi*f*x+phi))
Definition: ExpDecayOsc.h:27
void setActiveParameter(size_t i, double value) override
Set new value of i-th active parameter.
Definition: ExpDecayOsc.cpp:62
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.
Definition: ExpDecayOsc.cpp:31
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
Definition: ExpDecayOsc.cpp:43