Hi, if I run the following code and press L on the screen, I should be getting a leapfrog.txt textfile. But I only see my previous euler.txt. What is wrong with the code.
Thanks in advance for any help.
#include <iostream>
#include <fstream>
#include <cmath>
#include "H:\Visual Studio 2008\Projects\section2\threevector.h"
using namespace std;
void errors(double, threevector, ofstream&); // Function that calculates the errors
// and outputs the results
int main()
{
char determinant;
cout << "Press E to use Euler's method or L to use leapfrog method." << endl;
cin >> determinant;
if (determinant = 'E') // Euler method
{
ofstream outfile("euler.txt");
outfile << "Time \t Vx \t Vy \t Vz \t Vxerror \t Vyerror \t Vzerror \t Vmagerror" << endl;
threevector vn(0.0, 0.0, 2.0); // Initial vector velocity
threevector b(1.0, 0.0, 0.0); // Magnetic field
double delta_t = 0.01;
for (int i = 0; i < 2001; i++) // Calculates the velocity from t = 0 to t = 20
{ // in steps of delta_t = 0.01
double t = delta_t*i; // Time
if (t == 0.0)
{
errors(t, vn, outfile);
}
else
{
vn += ((vn^b)*delta_t); // New velocity
errors(t, vn, outfile);
}
}
}
else if (determinant = 'L') // Leapfrog method
{
ofstream outfile("leapfrog.txt");
outfile << "Time \t Vx \t Vy \t Vz \t Vxerror \t Vyerror \t Vzerror \t Vmagerror" << endl;
threevector vnminus1; // where vnminus1 will be stored
threevector vreserve; // where vn will be stored
threevector vn(0.0, 0.0, 2.0); // Initial vector velocity
threevector b(1.0, 0.0, 0.0); // Magnetic Field
double delta_t = 0.01;
for (int i = 0; i < 2; i++) // Euler used to calculate vnminus1 and vn
{
double t = delta_t*i; // Time
if (t == 0.0)
{
vnminus1 = vn; // The first velocity: vnminus1
errors(t, vn, outfile);
}
else
{
vn += ((vn^b)*0.01); // The second velocity: vn
errors(t, vn, outfile);
}
}
for (int i = 2; i < 2001; i++) // Leapfrog used for all subsequent iterations
{
double t = delta_t*i; // Time
vreserve = vn; // vn stored for subsequent calculation
vn = vnminus1 + ((vn^b)*(2.0*delta_t));
vnminus1 = vreserve; // vnminus1 stored for subsequent calculation
errors(t, vn, outfile);
}
}
else
{}
return 0;
}
void errors(double t, threevector vn, ofstream& outfile)
{
threevector vtrue (0.0, 2.0*sin(t), 2.0*cos(t)); // True velocity
threevector verror = vn - vtrue; // Error in velocity components
double vmagerror = vn.mag3() - 2.0; // Error in veocity magnitude
outfile << t << '\t'
<< vn.getx() << '\t' << vn.gety() << '\t' << vn.getz() << '\t'
<< verror.getx() << '\t' << verror.gety() << '\t' << verror.getz() << '\t'
<< vmagerror
<< endl;
}