```
/* Assignment 2 - Question 2
Ed Macey - PT EEE - Student No. 2802204
Mob. 07811 185 749 Email edmacey@gmail.com
Determine the voltage across the resistor at given time intervals by using the below
equation. You enter the end time and the number of time steps required; the program will
determine the voltage at each of the time steps and stored in a text file, which can be
loaded into MATLAB and plotted.
Where Resistor R = 1e3 Ohms , Capacitor C = 1e-06 F, Applied voltage E = 10 volts. */
#include <iostream>
#include <stdio.h>
#include <math.h> // allows us to use more complex mathematical expressions in our programme
#include <fstream>
using namespace std;
//define all our variables and allocate values if known
double inputtime, timeintervals, nointervals, result, param, voltage;
double n = 0;
double R = 1000;
double C = 0.000001;
double E = 10;
int main()
{
//this writes the header text to data file
ofstream myfile;
myfile.open ("data.txt");
myfile << "Time interval,Voltage" << std::endl;
myfile.close();
//get input data
cout << "Voltage over time calc for a Resistive Capacitor" << std::endl;
cout << "___________________________________________________" << std::endl;
cout << "Please type in the total length of time in seconds," << std::endl;
cout << "ie 10 milliseconds would be 0.1 > ";
cin >> inputtime;
cout << "Please type in the number of time steps" << std::endl;
cout << "you would like > ";
cin >> nointervals;
timeintervals = inputtime / nointervals;
cout << nointervals;
cout << std::endl;
cout << std::endl;
ofstream myfile;
myfile.open ("data.txt");
//using do while we calculate the voltage at each interval step until there
//are no more interval steps left
for (n = 0; n < inputtime; n += timeintervals)
{
// this is the calculation
result =(exp (-(n/(R*C))));
voltage = result*E;
cout << "the voltage at " << n << " seconds is " << voltage << " volts" << std::endl;
// this prints the text to data file
myfile << n << "," << voltage << std::endl; // we include the comma so that matlab can
// pick up the data as a comma delimeted text file
myfile.close();
}
cout << std::endl;
cout << std::endl;
cout << "Please load data.txt into Matlab to plot this data" << std::endl;
return 0;
}
```