(完整)三次样条插值的C程序(很全啊)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
三次样条插值 C/C++程序(自己整理的) 具体推导看书<<数值分析〉〉 code:
#include
(完整)三次样条插值的 C 程序(很全啊)
const int MAXN = 100;
int n; double x[MAXN], y[MAXN]; //下标从 0。。n double alph[MAXN], beta[MAXN], a[MAXN], b[MAXN]; double h[MAXN]; double m[MAXN]; //各点的一阶导数;
inline double sqr(double pa) { return pa * pa;
}
double sunc(double p, int i) {
[i]
return (1 + 2 * (p — x[i]) / (x[i + 1] - x[i])) * sqr((p - x[i + 1]) / (x[i + 1] — x[i])) * y
+ (1 + 2 * (p — x[i + 1]) / (x[i] - x[i + 1])) * sqr((p - x[i]) / (x[i + 1] — x [i])) * y[i + 1]
+ (p - x[i]) * sqr((p — x[i + 1]) / (x[i] - x[i + 1])) * m[i] + (p — x[i + 1]) * sqr((p — x[i]) / (x[i + 1] — x[i])) * m[i + 1]; }
int main() { int i, j;
double xx;
(完整)三次样条插值的 C 程序(很全啊)
freopen("threeInsert.in", "r", stdin);
scanf(”%d", &n);
for (i = 0; i <= n; i++) scanf(”%lf%lf”, &x[i], &y[i]);
// scanf("%lf%lf”, &m[0], &m[n]);
for (i = 0; i <= n - 1; i++) h[i] = x[i + 1] - x[i];
//第一种边界条件
//alph[0] = 0; alph[n] = 1; beta[0] = 2 * m[0]; beta[n] = 2 * m[n];
//第二种边界条件
alph[0] = 1; alph[n] = 0; beta[0] = 3 * (y[1] — y[0]) / h[0]; beta[n] = 3 * (y[n] - y[n — 1] / h [n — 1]);
for (i = 1; i 〈= n - 1; i++) { alph[i] = h[i — 1] / (h[i - 1] + h[i]); beta[i] = 3 * ((1 - alph[i]) * (y[i] — y[i — 1]) / h[i - 1] + alph[i] * (y[i + 1] — y
[i]) / h[i]);
} a[0] = — alph[0] / 2; b[0] = beta[0] / 2;
for (i = 1; i <= n; i++) { a[i] = — alph[i] / (2 + (1 — alph[i]) * a[i — 1]); b[i] = (beta[i] - (1 — alph[i]) * b[i - 1]) / (2 + (1 - alph[i]) * a[i — 1]);
} m[n + 1] = 0;
for (i = n; i 〉= 0; i--) { m[i] = a[i] * m[i + 1] + b[i];
} scanf("%lf”, &xx);
for (i = 0; i < n; i++) { if (xx 〉= x[i] && xx <= x[i + 1]) break;
} printf(”%lf\n", sunc(xx, i));
return 0; }
(完整)三次样条插值的 C 程序(很全啊)
#include
float f(int x1, int x2, int x3)/*****************求差分函数(含三个参数)**** ************************/ {
float a = (y[x3] — y[x2]) / (x[x3] — x[x2]); float b = (y[x2] — y[x1]) / (x[x2] - x[x1]); return (a - b)/(x[x3] — x[x1]); }
void cal_m(int n)/***********************用追赶法求解出弯矩向量 M…… ***************************/ {
float B[MAX]; B[0] = c[0] / 2;
for(int i = 1; i 〈 n; i++)
(完整)三次样条插值的 C 程序(很全啊)
B[i] = c[i] / (2 - a[i]*B[i-1]);
//fxym[0] = fxym[0] / 2; for(i = 1; i 〈= n; i++)
fxym[i] = (fxym[i] - a[i]*fxym[i-1]) / (2 - a[i]*B[i-1]); for(i = n—1; i >= 0; i--)
fxym[i] = fxym[i] — B[i]*fxym[i+1]; } void printout(int n);
int main()
{
int n,i; char ch;
do
{
cout〈〈"请输入已知断点个数:”;
cin〉>n;
for(i = 0; i 〈= n; i++)
{
cout<<"Please put in X"〈〈i〈〈’:’;
cin〉>x[i];
//cout<
cin>>y[i]; //cout<〈endl;
}