Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- vector<float> l;
- vector<float> x;
- vector<float> c;
- vector<float> b;
- vector<float> d;
- vector<float> lambda_;
- vector<float> sigma_;
- float y(float t) {
- return t * exp(t);
- }
- void interpolate(float left, float right, int step) {
- float h = (right - left) / (float)step;
- float xx = left;
- while (xx < right + h) {
- x.push_back(xx);
- xx += h;
- }
- for (int i = 1; i < x.size(); i++) {
- l.push_back((y(x[i]) - y(x[i - 1])) / h);
- }
- l.push_back(right);
- c.resize(step);
- b.resize(step);
- d.resize(step);
- lambda_.push_back(1.5 *((l[2] - l[1]) / h));
- sigma_.push_back(- 1 / 4);
- //прогонка
- for (int i = 2; i < x.size(); i++) {
- sigma_.push_back((-h) / (2 * h + 2 * h + h * sigma_[i - 2]));
- lambda_.push_back((3 * l[i] - 3 * l[i - 1] - h * lambda_[i - 2]) / (2 * h + 2 * h + h * sigma_[i - 2]));
- }
- //обратная прогонка
- for (int i = step - 1; i > 0; i--) {
- c[i - 1] = sigma_[i - 1] * c[i] + lambda_[i - 1];
- }
- for (int i = 1; i < step; i++) {
- b[i] = (l[i] + (2 * c[i] * h + h * c[i - 1]) / 3);
- d[i] = ((c[i] - c[i - 1]) / (3 * h));
- }
- }
- float getV(float v) {
- for (int i = 1; i < x.size(); i++) {
- if (v == x[0]) {
- return y(x[0]);
- } else if (i == x.size() - 1) {
- return y(x.back());
- } else if (x[i - 1] <= v && v <= x[i]) {
- float u = (v - x[i]);
- return y(x[i]) + b[i] * u + c[i] * u * u + d[i] * u * u * u;
- }
- }
- }
- int main() {
- interpolate(0, 1, 10);
- vector<float> t;
- float k = 0;
- cout << "x: ";
- while (k < 1.0) {
- cout << k << " ";
- k += 0.05;
- }
- cout << endl;
- k = 0;
- cout << "y: ";
- while (k < 1.0) {
- cout << y(k) << " ";
- k += 0.05;
- }
- cout << endl;
- k = 0;
- cout << "y*: ";
- while (k < 1.0) {
- cout << getV(k) << " ";
- k += 0.05;
- }
- cout << endl;
- k = 0;
- cout << "delta: ";
- while (k < 1.0) {
- cout << y(k) - getV(k) << " ";
- k += 0.05;
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement