I’m a physics undergrad and am basically stuck teaching myself root. Half my classes want me to use root but I’ve only gotten the bare minimum of instruction and am left to my own devices to learn what I need.
The background is that I am taking computational physics and the part one of the assignment was to calculate the distance/time/etc for a baseball with drag. No probs. Part two is to determine the angle at which the baseball travels the farthest with the drag. Again, no probs. The code is pretty easy. But she wants us to display the graph for each angle as a proof. She said just nest the code in a loop, but it isn’t that easy. The code executes perfectly but I am only getting one line on the graph.
I’ve been over the site for the past half hour and I think I need to be using a multigraph, but I’m not clear as to how that works. I’ve included the code bits so you can see where I’m coming from. All the variable names are fairly self-explanatory. Obviously, the parts I’m currently working on are in the Draw Graph Canvas and Draw Data Points for Numerical Solution.
I’m a bit stuck so any help you can provide would be greatly appreciated.
[code]
for (int angleloop = 0; angleloop <= 90; angleloop+=5)
{
// Baseline Calculations - Convert Data Entry into Usable Values
angleRad = (angleDeg + angleloop) * 2 * pi / 360; // Convert Degrees to Radians
v_horiz[0] = initialV * cos(angleRad); // Calculate Horizontal Velocity
v_vert[0] = initialV * sin(angleRad); // Calculate Initial Vertical Velocity
// Find Exact Solution
exactTime = v_vert[0] / gravity;
exactHoriz = v_horiz[0] * 2 * exactTime;
exactVert = .5 * (-gravity) * exactTime * exactTime + v_vert[0] * exactTime;
if (exactTime < maxsize * step)
{
// Find Numerical Euler Solution
loop = 0;
do
{
t[loop + 1] = t[loop] + step;
totalV = sqrt(v_horiz[loop] * v_horiz[loop] + v_vert[loop] * v_vert[loop]); // Calculate Composite V
drag = .0039 + .0058 / (1 + exp((totalV - 35.0) / 5)); // Given Equation 35.0 = Vd, 5 = Delta
//drag = .00814013975;
v_horiz[loop + 1] = v_horiz[loop] - drag * totalV * v_horiz[loop] * step;
v_vert[loop + 1] = v_vert[loop] - gravity * step - drag * totalV * v_vert[loop] * step;
d_horiz[loop + 1] = d_horiz[loop] + v_horiz[loop] * step;
d_vert[loop + 1] = d_vert[loop] + v_vert[loop] * step;
if (d_vert[loop+1] > d_vert[loop]) maxHeight = d_vert[loop+1];
loop++;
}
while (d_vert[loop] > 0)
cout << endl;
cout << "Angle = " << angleDeg + angleloop << ", Horizontal Distance = " << d_horiz[loop] << endl;
// Draw Graph Canvas
if (angleloop == 0)
{
TCanvas *c1 = new TCanvas("c1", "Baseball Best Angle", 200, 10, 700, 500);
c1->SetFillColor(42);
c1->SetGrid();
TMultiGraph *mg = new TMultiGraph();
}
// Draw Data Points for Numerical Solution
gr[angleloop] = new TGraph(loop, d_horiz, d_vert);
gr[angleloop]->SetLineColor(2 + angleloop / 5);
gr[angleloop]->SetLineWidth(4);
gr[angleloop]->SetMarkerColor(5);
gr[angleloop]->SetMarkerStyle(20);
gr[angleloop]->SetTitle("Baseball Flight Path");
gr[angleloop]->GetXaxis()->SetTitle("Horizontal Distance (m)");
gr[angleloop]->GetXaxis()->SetLimits(0, exactHoriz);
gr[angleloop]->GetXaxis()->CenterTitle();
gr[angleloop]->GetYaxis()->SetTitle("Vertical Height (m)");
gr[angleloop]->GetYaxis()->SetRangeUser(0, exactVert * 1.05);
gr[angleloop]->GetYaxis()->CenterTitle();
gr[angleloop]->Draw("C");
// Draw Legend
leg = new TLegend(0.45, 0.3, 0.75, 0.5);
leg->SetFillColor(42);
leg->AddEntry(gr[angleloop], (angle + "`f[#circ]").c_str(), "pl");
leg->Draw("same");
}
else cout << "Error.";
}[/code]