Reference documentation for deal.II version GIT relicensing-437-g81ec864850 2024-04-19 07:30:02+00:00
Searching...
No Matches
fe_series.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
4// Copyright (C) 2016 - 2021 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15
16
17#include <deal.II/base/config.h>
18
20
21#include <iostream>
22
23
25
26namespace FESeries
27{
28 std::pair<double, double>
29 linear_regression(const std::vector<double> &x, const std::vector<double> &y)
30 {
31 FullMatrix<double> K(2, 2), invK(2, 2);
32 Vector<double> X(2), B(2);
33
34 Assert(x.size() == y.size(),
35 ExcMessage("x and y are expected to have the same size"));
36
37 Assert(x.size() >= 2,
39 "at least two points are required for linear regression fit"));
40
41 double sum_1 = 0.0, sum_x = 0.0, sum_x2 = 0.0, sum_y = 0.0, sum_xy = 0.0;
42
43 for (unsigned int i = 0; i < x.size(); ++i)
44 {
45 sum_1 += 1.0;
46 sum_x += x[i];
47 sum_x2 += x[i] * x[i];
48 sum_y += y[i];
49 sum_xy += x[i] * y[i];
50 }
51
52 K(0, 0) = sum_1;
53 K(0, 1) = sum_x;
54 K(1, 0) = sum_x;
55 K(1, 1) = sum_x2;
56
57 B(0) = sum_y;
58 B(1) = sum_xy;
59
60 invK.invert(K);
61 invK.vmult(X, B, false);
62
63 return std::make_pair(X(1), X(0));
64 }
65} // namespace FESeries
66
void vmult(Vector< number2 > &w, const Vector< number2 > &v, const bool adding=false) const
void invert(const FullMatrix< number2 > &M)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::pair< double, double > linear_regression(const std::vector< double > &x, const std::vector< double > &y)
Definition fe_series.cc:29