Demo entry 1487104

Part 1 c++

Submitted by anonymous on Apr 17, 2015 at 16:42
Language: C++. Code size: 3.1 kB.

/*
* Michael Van Wickle - GE1111 - sec23
*
* Final Project Part 1 C++
*
*/

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

double epotential(double, double); // function prototype for epotential

int main(){
double  q(8*pow(10, -9)), k(9*pow(10, 9)); // Initialize q and k

ofstream of1, of2, of3; // create out streams

of1.open("x_vs_Vp1.dat"); // open files for each part
of2.open("x_vs_Vp2.dat");
of3.open("x_vs_Vp3.dat");

of1 << endl;

// Part 1(a)
// Calculate epotential for each point per range
for (double x = -0.5; x <= -0.1; x += 0.01){
of1 << x << " " << epotential(q, x) << endl;
}
for (double x = 0.1; x <= 0.5; x += 0.01){
of1 << x << " " <<  epotential(q, x) << endl;
}
of1 << endl;
of1.close(); // close first file
of2 << endl;
// Part 1(b)
double q1(8*pow(10, -9)), q2(12*pow(10, -9)), x1(0.5), x2(-0.5); // new charges, distances
double r1(0), r2(0), ep(0); // initialize radii and epotential

// Calculate epotential for each point per range
for (double x = -0.8; x <= -0.6; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r1); // calculate each epotential
of2 << x << " " << ep << endl; // output to file
}
of2 << endl;
for (double x = -0.4; x <= 0.4; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r2); // calculate each epotential
of2 << x << " " << ep << endl; // output to file
}
of2 << endl;
for (double x = 0.6; x <= 0.8; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r2); // calculate each epotential
of2 << x << " " << ep << endl; // output to file
}
of2 << endl;
of2.close(); // close second file
of3 << endl;
// Part 1(c)
q2 = -12*pow(10, -9); // new second charge

// Calculate epotential for each point per range
for (double x = -0.8; x <= -0.6; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r1); // calculate each epotential
of3 << x << " " << ep << endl; // output to file
}
of3 << endl;
for (double x = -0.4; x <= 0.4; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r2); // calculate each epotential
of3 << x << " " << ep << endl; // output to file
}
of3 << endl;
for (double x = 0.6; x <= 0.8; x += 0.01){
r1 = abs(x1 - x); // find distance from point to charge
r2 = abs(x2 - x);
ep = epotential(q1, r1) + epotential(q2, r2); // calculate each epotential
of3 << x << " " << ep << endl; // output to file
}
of3 << endl;
of3.close(); // close third file
return 0;
}

double epotential(double q, double r){
double k(9*pow(10, 9)); // k constant
return (k*q) / r; // return the epotential
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.