?? 第一型樣條插值.cpp
字號:
/*
第一型樣條插值,邊界條件為兩端的一階導數值已知
*/
#include <iostream>
#include <vector>
#include <fstream>
#include "zhuigang.h"
#include "conio.h"//_getch()
using namespace std;
void main()
{
vector<double> x, y;
int n;//數值的個數
fstream filex("Spline1x.txt");//數據文件
fstream filey("Spline1y.txt");//最后兩數為端點的導數值
double temp;
while (filex >> temp)
x.push_back(temp);
while (filey >> temp)
y.push_back(temp);
n = x.end() - x.begin();
double df0, dfn;
df0 = y[n];//端點的導數值
dfn = y[n + 1];
vector<double> h, mui, b, lmel, M, d;
for(int i = 0; i < n - 1; i++)//數據間隔
{
h.push_back(x[i + 1] - x[i]);
}
for(i = 0; i < n - 2; i++)//μ值
{
mui.push_back(h[i] / (h[i] + h[i + 1]));
}
mui.push_back(1);
lmel.push_back(1);
for(i = 0; i < n - 2; i++)//λ值
{
lmel.push_back(1 - mui[i]);
}
temp = (y[1] - y[0] - df0 * h[0]) / h[0] / h[0];
d.push_back(6 * temp);//d值
for(i = 0; i < n - 2; i++)
{
temp = y[i] / h[i] / (h[i] + h[i + 1]) - y[i + 1] / h[i] / h[i + 1]
+ y[i + 2] / (h[i] + h[i + 1]) / h[i + 1];
d.push_back(6 * temp);
}
temp = (dfn * h[n - 1] - y[n - 1] + y[n - 2]) / h[n - 1] / h[n - 1];
d.push_back(6 * temp);
for(i = 0; i < n; i++)
{
b.push_back(2);
}
M = zhuigang(mui, b, lmel, d);//追趕法求M值
for (i = 0; i < n; i++)
{
cout << M[i] << "\t";
}
do//檢查程序輸出
{
cout << "\ninput x:";
cin >> temp;
cout << "S(" << temp << ") is: ";
int j = 0;//找出x所在的區間
for(i = 0; i < n, j == 0; i++)
{
if(x[i] > temp)
j = i + 1;
}
j -= 2;
if(j < 0 || j > n - 1)
{
cout << "the input is out of range";
goto another;
}
temp =y[j] + ((y[j + 1] - y[j]) / h[j] - (M[j] / 3 + M[j + 1] / 6) * h[j])
* (temp - x[j]) + M[j] / 2 * (temp - x[j]) * (temp - x[j])
+ (M[j + 1] - M[j]) / 6 / h[j]
* (temp - x[j]) * (temp - x[j]) * (temp - x[j]);
cout << temp << endl;
another:cout << "press any key to see another S(x) except x(exit)";
}while(_getch() != 'x');
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -