linear interpolation method

Nov 27, 2015 at 3:03pm
i have a text file having two columns heading "z","p","v","w","av","aw" containing numeric values.So i want a program to store these values in two different
arrays on which i can operate.
here is my text file:
z p v w av aw
-0.67001 6.54438 -0.01078 0.00001 -0.00987 0.00000
-0.65043 6.35235 -0.01081 -0.00014 -0.00981 0.00329
-0.63086 6.16032 -0.01083 -0.00028 -0.00976 0.00657
-0.61129 5.96829 -0.01086 -0.00034 -0.00969 0.00967
-0.59172 5.77623 -0.01089 -0.00038 -0.00961 0.01154
-0.57215 5.58411 -0.01096 -0.00035 -0.00945 0.01071
-0.55258 5.39199 -0.01103 -0.00034 -0.00929 0.00992
-0.53301 5.19987 -0.01109 -0.00033 -0.00911 0.00915
-0.51344 5.00767 -0.01119 -0.00038 -0.00887 0.01036
-0.49387 4.81547 -0.01131 -0.00042 -0.00862 0.01172
-0.47431 4.62327 -0.01141 -0.00044 -0.00832 0.01302
-0.45473 4.431 -0.01154 -0.00047 -0.00799 0.01445
-0.43516 4.23871 -0.01169 -0.00051 -0.00762 0.01589
-0.41559 4.04644 -0.01183 -0.00051 -0.00721 0.01725
-0.39602 3.85407 -0.01201 -0.00053 -0.00675 0.01879
-0.37645 3.66171 -0.01221 -0.00053 -0.00625 0.02029

Next i want to give a Z value as input and want my program to check if the given value of is present in my "z" list or not...if yes then produce
the corresponding "v" and "w"value respectively.
If the given Z value is not in the "z" list then find the linear interpolation between the values nearest(nearest above and below)
the given z value from the "z" list,to produce u and w value.
Thanks in advance ...i need it urgently.




#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

using namespace std;
double interpolation (double x1, double x2, double y1, double y2, double xInt)
{

double yInt = y1 + (y2-y1)/(x2-x1)*(xInt-x1);

return yInt;
}
int main()
{
ifstream theFile("velocity.txt");
double depth;
double z;
cin>>z;
int n=0;
double y[]={0};
double v[]={0};
double w[]={0};
double ax[]={0};
double ay[]={0};
double velx;
double vely;
double pressure;
double velocityx;
double velocityy;
double acclx;
double accly;
while (theFile>>depth>>pressure>>velocityx>>velocityy>>acclx>>accly)
{
cout<<depth<<" "<<pressure<<" "<<velocityx<<" "<<velocityy<<" "<<acclx<<" "<<accly<<endl;
y[n]=depth;
v[n]=velocityx;
w[n]=velocityy;
ax[n]=acclx;
ay[n]=accly;
n=n+1;

}
cout<<n;
for(int j=0;j<n;j++)
{
cout<<y[j]<<" "<<v[j]<<" "<<w[j]<<" "<<ax[j]<<" "<<ay[j]<<endl;
}
for(int i=0;i<n;i++)
{ int f=0;
if(y[i]==z)
{
velx=v[i];
vely=w[i];
}
else if(y[i]<z)
{
f=f+1;
}
else
{
velx=interpolation (y[f-1], y[f], v[f-1], v[f], z);
vely=interpolation (y[f-1], y[f], w[f-1], w[f], z);
break;
}
}
cout<<velx<<endl<<vely<<endl;


}

i dont know why it is not working.....pls help me guys ...i need it urgently
Nov 27, 2015 at 3:04pm
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Nov 28, 2015 at 4:08am
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/178149/
Nov 28, 2015 at 4:10am
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/files/
Nov 28, 2015 at 5:14am
closed account (48T7M4Gy)
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
#include <fstream>
#include <string>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;

double interpolation(double, double, double, double, double);

int main()
{
	const int limit = 100;
	string header = "";
	double depth;
	double z;

	int n = 0;
	double y[limit] = { 0 };
	double v[limit] = { 0 };
	double w[limit] = { 0 };
	double ax[limit] = { 0 };
	double ay[limit] = { 0 };
	double velx;
	double vely;
	double pressure;
	double velocityx;
	double velocityy;
	double acclx;
	double accly;

	cout << "Enter a value for z: "; // ?
	cin >> z;

	ifstream theFile("velocity.txt");

	if (theFile.is_open())
	{
		getline( theFile, header);
		cout << header << endl;
		while (theFile >> depth >> pressure >> velocityx >> velocityy >> acclx >> accly)
		{
			cout
				<< depth << " " << pressure << " "
				<< velocityx << " " << velocityy << " "
				<< acclx << " " << accly << endl;

			y[n] = depth;
			v[n] = velocityx;
			w[n] = velocityy;
			ax[n] = acclx;
			ay[n] = accly;
			n = n + 1;
		}

		theFile.close();
	}

	cout << n;
	for (int j = 0;j < n;j++)
	{
		cout
			<< y[j] << " " << v[j] << " "
			<< w[j] << " " << ax[j] << " "
			<< ay[j] << endl;
	}

	for (int i = 0;i < n;i++)
	{
		int f = 0;
		if (y[i] == z)
		{
			velx = v[i];
			vely = w[i];
		}
		else if (y[i] < z)
		{
			f = f + 1;
		}
		else
		{
			velx = interpolation(y[f - 1], y[f], v[f - 1], v[f], z);
			vely = interpolation(y[f - 1], y[f], w[f - 1], w[f], z);
			break;
		}
	}

	cout << velx << endl << vely << endl;
	return 0;
}

double interpolation(double x1, double x2, double y1, double y2, double xInt)
{
	return y1 + (y2 - y1) / (x2 - x1)*(xInt - x1);
}
Nov 28, 2015 at 5:55am
1
2
3
4
5
double y[]={0};
double v[]={0};
double w[]={0};
double ax[]={0};
double ay[]={0};


These define arrays of 1 element each and initialize that element to 0. The size of these arrays do not change within the program so accessing any element other than the first (that at index 0) is to access an element that doesn't exist.


The only possible value for f in:
1
2
			velx = interpolation(y[f - 1], y[f], v[f - 1], v[f], z);
			vely = interpolation(y[f - 1], y[f], w[f - 1], w[f], z);


is 0. So, even if you had arrays larger than one element in size, you would be accessing outside the bounds of the arrays.



Nov 29, 2015 at 2:38pm
closed account (48T7M4Gy)
Multiple posts are bad.

http://www.cplusplus.com/forum/beginner/179868/
Topic archived. No new replies allowed.