cubic spline

i want to test the spline with any three points in x[0]<x<x[n] using function Test(). but the output is confusing me. i dont know how to fix it. please someone help me.
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
#include <iostream>
#include <fstream>
#define n 9

using namespace std;

typedef struct
{
	double x,y;
	
}POINT;

class cs
{
public:
	cs()	{}
	~cs()	{}
	void ReadData(POINT [n+1]);
	void CSpline();
	void Display();
	void Test();

};



void cs::ReadData(POINT pt[n+1])
{

	int i;
	ifstream ifp("Q7.in");
	for (i=0; i<=n; i++)
		ifp>>pt[i].x>>pt[i].y;

	ifp.close();

		//for (i=0; i<=n; i++)
			//cout<<pt[i].x<<"\t"<<pt[i].y<<endl;
}



void cs::CSpline()
{
	POINT pt[n+1];
	double a[n+1], b[n+1], c[n+1], d[n+1];
	double alpha[n+1], h[n+1], mu[n+1], el[n+1], z[n+1];
	
	a[n]=pt[n].y=2;
	int i;
	for (i=0; i<=n-1; i++)
	{
		h[i]=pt[i+1].x-pt[i].x;
		a[i]=pt[i].y;
	}

	for (i=1; i<=n-1; i++)
	{
		alpha[i]=(3/h[i])*(a[i+1]-a[i])-(3/h[i-1])*(a[i]-a[i-1]);
	}

	el[0]=1; mu[0]=0; z[0]=0;
	for (i=1; i<=n-1; i++)
	{
		el[i]=2*(pt[i+1].x-pt[i-1].x)-h[i-1]*mu[i-1];
		mu[i]=h[i]/el[i];
		z[i]=(alpha[i]-h[i-1]*z[i-1])/el[i];

	}

	el[n]=1; z[n]=0; c[n]=0;
	int j;
	for (j=n-1; j>=0; j--)
	{
		c[j]=z[j]-mu[j]*c[j+1];
		b[j]=(a[j+1]-a[j])/h[j]-h[j]*(c[j+1]+2*c[j])/3;
		d[j]=(c[j+1]-c[j])/(3*h[j]);
	}
	
	
}

void cs::Display( )
{
      //display results
}

void cs::Test()
{
	cs CS;
	int i,j;
	double x;
	double S[n+1], a[n+1], b[n+1], c[n+1], d[n+1];
	POINT pt[n+1];
	CS.CSpline();
	
	for (i=0; i<=n; i++)
		for (j=0; j<=n-1; j++)
		{
			x=1.2;
			S[j]=a[j]+b[j]*(x-pt[j].x)+c[j]*pow((x-pt[j].x),2)+d[j]*pow((x-pt[j].x),3);
	
			cout<<S[j]<<endl;
		}

}

void main()
{
	cs CS;
	double S[n+1];
	POINT pt[n+1];
	CS.ReadData(pt);
	CS.CSpline();
	CS.Test();
	cin.get();
}
Hi lyn18,

So what compiler output do you have?

Some things I noticed:

Your class doesn't have any member variables - only functions.

All your functions are void, most of them don't take arguments, there are no references or pointers, so none of the variables in separate functions are related.

I don't see where the variable n is defined anywhere.

The main function should always return an int:

1
2
3
4
5
6
int main() {

//your code here

return 0; //if all is well, some thing else otherwise
}


I think you need to do some reading about classes:

http://www.cplusplus.com/doc/tutorial/


Hop all goes well.
Topic archived. No new replies allowed.