Problem C: Piecewise Linear Interpolation Sometimes time-dependent data that is
ID: 3914668 • Letter: P
Question
Problem C: Piecewise Linear Interpolation
Sometimes time-dependent data that is gathered at irregular times needs to be estimated at more regular times. For example, a remote sensor’s readings might occur only at cer- tain times t = 0.0, .12, .25, .37, .50, .71, 1.0 when what you want is readings at times 0.0, 0.1, 0.2, 0.3, . . . , 1.0. One way to estimate what the intermediate readings would have been is to use piecewise linear interpolation.3 For example, suppose we want to esti- mate what the reading would have been at time t = 0.2. Let f(.12) be the reading at timet=.12,andf(.25)bethereadingattimet=.25. The timet= 0.2 is 8/13 of the way from .12 to .25, so we can estimate the reading at t = 0.2 by the weighted average
f(0.2) = ( 1 - 8/13)(.12) + (8/13)f(.25)
In general, to estimate the reading at time t, let a be the largest time less than t at which a reading was taken, and b be the smallest time after tˆ at which a reading was taken. (For example, if tˆ = .36, then in the example we would have a = .25 and b = .37.) Then the estimate for the reading at tˆ is given by
f(t) = ((b-t)/(b-a))f(a) + ((t-a)/(b-a))f(b)
Write a C++ program that does the following:
First, it should prompt the user for the input file name and open that file, exiting with an error message if the file can’t be opened.
Then it should prompt the user for an output file name and open that file for writing, exiting with an error message if the file can’t be opened.
3. Third, for each time tˆ = 0.0, 0.1, 0.2, ..., (until the end value in the input file) it should ˆˆˆ estimate f(t) and write a line consisting of t and f(t) to the output file. Both values should be written with two digits to the right of the decimal point, and they should be separated by a single space. (See the example output file below.)
4. When all the values have been written to the output file, both the input and output file should be closed.
Piecewise linear interpolation is not the only way to estimate intermediate values. In general, whether it is a reasonable approach to use depends on the specific problem.
Note that, other than the two filenames, the program has no input: all the other input is from the input file. Similarly, the program does not output anything to the console window: all the output is written to the output file.
The input file has the following format: it consist of a number of lines, each of which has two values: the time, followed by whitespace, followed by the reading at that time. You will need to determine when you are at the end of the file by using the eof() function. To simplify the program, assume the first time in the file is always 0.0, and the last is always a value with a 0 fractional part (such as 6.0 or 8.00).
Here is an example input file:
Note that part of the challenge of this problem is that the time spacings in the input file are irregular. For example, note the following for the regular times 0.0, 0.1, . . . , 2.0: First, 0.0 is the same as the first time in the file, so the reading f(0.0) can just be copied, it does not need to be computed. Then, 0.1 is between the file times 0.0 and .12; 0.2 is between .12 and .25; 0.3 is between .25 and .37, and 0.4 is between .37 and .50. The time 0.5 is in the input file, so the reading f(0.5) can be copied. Then the times 0.6 and 0.7 are between .50 and .71. Then the times 0.8 and 0.9 are between .71 and 1.00. Then the time 1.0 is in the input file. Then 1.1, 1.2, 1.3 and 1.4 are between 1.0 and 1.42. Then 1.5, 1.6, 1.7, 1.8, and 1.9 are between 1.45 and 2.00. (Moreover, note that there are two consecutive times, 1.42 and 1.45, in the input file that don’t have any of the regular times between them.) Finally, the last value 2.00 is in the input file.
For that example input, the resulting output file should be
6
Some important comments:
This should not be a long program. However, how to coordinate the regular times 0.0, 0.1, 0.2., . . . with the irregular input times is challenging. A related, challenging part is ensuring that you generate output lines all the way to, but not past, the last desired time value. Both of these challenges will require some thought and some care.
You may assume the input data file contains at least two lines of data.
Remember you should also assume the first time in the file is 0.00, and the last time is a whole number of seconds (i.e., the fractional part is 0).
Be careful about how you compute the regular times. An approach where, for example, you start with 0.0 and add 0.1 successively is likely to lead to frustrating round-off errors.
Your program should work not only on the example input file above, but on any input file of that type.
The solution to this problem does not require arrays. You may use arrays if you wish, but the simplest solution does not use arrays (we have not yet covered arrays in class and it is not expected that you understand how to use them yet).
Explanation / Answer
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
float tval1 = 0; // Value a
float tval2 = 0;// Value b
float ftval1 = 0;//Value f(a)
float ftval2 = 0;//Value f(b)
float iter = 0;//Goes from 0.0, 0.1, ... till the end
float ans = 0;
ifstream in("text.txt");
ofstream out("ans.txt");
out << std::fixed;
out << std::setprecision(2);//To print only 2 decimal places
in>>tval1>>ftval1;//Input first case
while(in>>tval2>>ftval2){//Keep inputing till end of file
while(iter >= tval1 && iter<tval2+0.1){//As ling as it is calculatable within the given range(somewhy <= wasn't working so I used +0.1)
if(iter == tval1){//No need to calculate easy answer
ans = ftval1;
}else if(iter == tval2){//Same as above
ans = ftval2;
}else{//Calculate the value
ans = ((tval2-iter )/(tval2-tval1))*ftval1 + ((iter-tval1)/(tval2-tval1))*ftval2;
}
out<<iter<<" "<<ans<<endl;//Output the value
iter += 0.1;//Go to next value
}
//Reading next line new values will be taken
//So a and f(a) need to update first
tval1 = tval2;
ftval1 = ftval2;
}
in.close();
out.close();
return 0;
}
----
Input File:
0.0 4.00
.12 3.94
.25 3.88
.37 3.92
.50 4.12
.71 4.34
1.0 4.50
1.42 5.12
1.45 5.13
2.0 5.57
-----
Output File:
0.00 4.00
0.10 3.95
0.20 3.90
0.30 3.86
0.40 3.93
0.50 4.12
0.60 4.22
0.70 4.33
0.80 4.43
0.90 4.44
1.00 4.50
1.10 4.65
1.20 4.80
1.30 4.94
1.40 5.09
1.50 5.24
1.60 5.25
1.70 5.33
1.80 5.41
1.90 5.49
2.00 5.57
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.