An advantage of programming is the ability to perform millions of calculations and dump it to a file for plotting. This can be extremely useful for engineers if they are simulating or characterizing a system. Below you will calculate data, write it to a file, and plot the contents in excel.
a)
Create a csv file for writing. This can be done by creating a file with the open method as we
have done in class but with a *.csv extension in the file name . For example
outFile.open(“equation.csv”);
b)
Declare a double variable t and initialize its value to 0.01
c)
Write a loop that increments t value by .01 from 0 to 9 and calculate the following for each t
y = e^(−t/1.5)∗cos(t)
d)
To calculate e^(−t/1.5) and cos (t), you can use the exp() function and cos() function in the cmatch library. For example: include<cmath.h> double x = exp(1.1);//the value of x will be
e^1.1
double m=cos(2.2);//
the value of m will be cos(2.2);
e)
Now write your data to the csv file. The goal is to plot the information in excel, which mean that in the first column we shall have our x values, and the second column the calculated y
values. The format of a cvs file states that every comma denotes the end of the contents in a given column. A newline denotes a new row.
Thus
12,3,8
4,6,9
will be saved in excel as:
12, 3, 8
4, 6, 9
Therefore, print the t value followed by a comma and the corresponding y value on a line for each calculation to a your csv file. It should look something like the following.
0,1
0.01,0.993306
0.02,0.986558
0.03,0.979758
0.04,0.972907
f)
Finish the rest of the steps for your file IO.
g)
Find the file, and open it with excel. Good news, excel 2010 an up inherently knows how to
parse the file. Otherwise, you may have to select comma delimited.
h)
Take your two columns of data and plot them using a scatter plot
i)
Convert the file from a csv file to an excel workbook file, xlsx, through the save as dialog box.
j)
Submit your program and corresponding excel workbook.
#include<bits/stdc++.h>
using namespace std;
// b, function
double calculate(double t){
double val= exp(-t/1.5) * cos(t);
return val;
}
int main()
{
ofstream outFile;
//a. Creating File With Open
outFile.open("equation.csv");
double t;
//c. Loop For Incrementation from 0 to 9
for(t=0;t<=9;t=t+0.01){
// d. calculating from within the loop by the function call each time
double val= calculate(t);
// e. Writing the data to csv file
outFile<<t<<","<<val<<"\n";
}
outFile.close();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.