1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <math.h>
#include <iostream>
using namespace std;
void ccspline(int n, long double *x, long double *a, long double FPO, long double FPN)
{
long double *b, *c, *d, *h, *e, *z, *u, *l;
b= new long double[n+1];
c= new long double[n+1];
d= new long double[n+1];
h= new long double[n+1];
e= new long double[n+1];
z= new long double[n+1];
u= new long double[n+1];
l= new long double[n+1];
int i;
for (i=0;i<n;i++)
h[i]=x[i+1]-x[i];
e[0]=3*(a[1]-a[0])/h[0]-3*FPO;
e[n]=3*FPN-3*(a[n]-a[n-1])/h[n-1];
for (i=1;i<n;i++)
e[i]=3/h[i]*(a[i+1]-a[i])-3/h[i-1]*(a[i]-a[i-1]);
l[0]=2*h[0];
u[0]=0.5;
z[0]=e[0]/l[0];
for(i=1;i<n;i++)
{
l[i]=2*(x[i+1]-x[i-1]) - h[i-1]*u[i-1];
u[i] = h[i]/l[i];
z[i] = ( e[i]-h[i-1]*z[i-1])/l[i];
};
l[n]=h[n-1]*(2-u[n-1]);
z[n]=(e[n]-h[n-1]*z[n-1])/l[n];
c[n]=z[n];
for(i=n-1;i>=0;i--)
{
c[i]=z[i]-u[i]*c[i+1];
b[i]=(a[i+1]-a[i])/h[i]-h[i]*(c[i+1]+2*c[i])/3;
d[i]=(c[i+1]-c[i])/(3*h[i]);
};
for(i=0; i<n;i++)
cout << a[i] << ", " << b[i]<<", "<<c[i]<<", "<<d[i]<<endl;
};
int main()
{
int n,i;
long double *a,*x,FPO,FPN;
cout << " How many intervals?" ;
cin >> n;
a=new long double[n+1];
x=new long double[n+1];
cout << endl << " Input the x coords: " << endl;
for (i=0;i<n+1;i++)
cin >> x[i];
cout << endl << " thanks, now the f(x)'s " << endl;
for (i=0;i<n+1;i++)
cin >> a[i];
cout << endl<< " Enter f'(x0) and f'(xn) please " << endl;
cin >> FPO;
cin >> FPN;
cout << " The spline follows " << endl;
ccspline(n,x,a,FPO,FPN);
};
|