https://mooseframework.inl.gov
Loading...
Searching...
No Matches
PolynomialFit.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#include "PolynomialFit.h"
11
12#include "MooseError.h"
13#include "MathUtils.h"
14
15// C++ includes
16#include <fstream>
17#include <algorithm>
18
20
21PolynomialFit::PolynomialFit(const std::vector<Real> & x,
22 const std::vector<Real> & y,
23 unsigned int order,
24 bool truncate_order)
25 : LeastSquaresFitBase(x, y), _order(order), _truncate_order(truncate_order)
26{
27 if (_x.size() == 0)
28 mooseError("PolynomialFit does not allow empty input vectors");
30 {
31 if (_x.size() <= _order)
32 _order = _x.size() - 1;
33 }
34 else if (_x.size() <= order)
35 mooseError("PolynomialFit requires an order less than the size of the input vector");
36
37 _num_coeff = _order + 1;
38}
39
40void
42{
43 unsigned int num_rows = _x.size();
44 unsigned int num_cols = _order + 1;
45 _matrix.resize(num_rows * num_cols);
46
47 for (unsigned int col = 0; col < num_cols; ++col)
48 for (unsigned int row = 0; row < num_rows; ++row)
49 _matrix[(col * num_rows) + row] = MathUtils::pow(_x[row], col);
50}
51
52Real
54{
55 unsigned int size = _coeffs.size();
56 Real value = 0;
57
58 Real curr_x = 1;
59 for (unsigned int i = 0; i < size; ++i)
60 {
61 value += _coeffs[i] * curr_x;
62 curr_x *= x;
63 }
64 return value;
65}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Base class for linear least squares fit method.
std::vector< Real > _x
Independent variable.
std::vector< Real > _coeffs
Vector of coefficients of the least squares fit.
unsigned int _num_coeff
The number of coefficients.
std::vector< Real > _matrix
Basis functions evaluated at each independent variable (note: actually a vector)
bool _truncate_order
Flag to implement a truncated polynomial.
PolynomialFit(const std::vector< Real > &x, const std::vector< Real > &y, unsigned int order, bool truncate_order=false)
static int _file_number
File number.
unsigned int _order
Order of the polynomial.
virtual Real sample(Real x) override
This function will take an independent variable input and will return the dependent variable based on...
virtual void fillMatrix() override
Helper function that creates the matrix necessary for the least squares algorithm.
T pow(T x, int e)
Definition MathUtils.h:91