Mantid
Loading...
Searching...
No Matches
SmoothTransition.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2021 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12
13#include <cmath>
14
16
17using namespace CurveFitting;
18
19using namespace Kernel;
20
21using namespace API;
22
23DECLARE_FUNCTION(SmoothTransition)
24
25void SmoothTransition::init() {
26 declareParameter("A1", 0.0, "the limit of the function as x tends to zero");
27 declareParameter("A2", 0.1, "the limit of the function as x tends to infinity");
28 declareParameter("Midpoint", 100.0, "Sigmoid Midpoint");
29 declareParameter("GrowthRate", 1.0, "Growth rate");
30}
31
32void SmoothTransition::function1D(double *out, const double *xValues, const size_t nData) const {
33 const double a1 = getParameter("A1");
34 const double a2 = getParameter("A2");
35 const double midpoint = getParameter("Midpoint");
36 const double gr = getParameter("GrowthRate");
37
38 for (size_t i = 0; i < nData; i++) {
39 out[i] = a2 + (a1 - a2) / (exp((xValues[i] - midpoint) / gr) + 1);
40 }
41}
42
43void SmoothTransition::functionDeriv1D(Jacobian *out, const double *xValues, const size_t nData) {
44 const double a1 = getParameter("A1");
45 const double a2 = getParameter("A2");
46 const double midpoint = getParameter("Midpoint");
47 const double gr = getParameter("GrowthRate");
48
49 for (size_t i = 0; i < nData; i++) {
50 double expFunc = exp((xValues[i] - midpoint) / gr);
51 double denominatorSq = pow((expFunc + 1), 2);
52
53 double diffa1 = 1 / (expFunc + 1);
54 double diffa2 = 1 - diffa1;
55 double diffmidpoint = ((a1 - a2) * expFunc) / (gr * denominatorSq);
56 double diffgr = ((a1 - a2) * (xValues[i] - midpoint) * expFunc) / (pow(gr, 2) * denominatorSq);
57
58 out->set(i, 0, diffa1);
59 out->set(i, 1, diffa2);
60 out->set(i, 2, diffmidpoint);
61 out->set(i, 3, diffgr);
62 }
63}
64
65} // namespace Mantid::CurveFitting::Functions
#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.
double getParameter(size_t i) const override
Get i-th parameter.
Provide Smooth Transition function interface to IFunction.
void functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) override
Derivatives of function with respect to active parameters.
void function1D(double *out, const double *xValues, const size_t nData) const override
Function you want to fit to.