# Help! Plotting a function

• 02-19-2009, 09:29 PM
cvubando
Help! Plotting a function
Can somebody help me plot the function x0 + v0t – gt^2/2, which is the displacement in meters after t seconds when an object is thrown straight up from initial position x0 at velocity v0 meters per second and where g is the constant 9.78033 (gravity).

Using StdDraw, I need to set the x scale to 20 and the y scale to 500.

Any help would be much appreciated. Thanks!

The name of my program is called GravityPlot.
• 02-20-2009, 12:05 AM
prometheuzz
• 02-20-2009, 12:29 AM
cvubando
Simply put, I do not know how to write the code for it.
• 02-20-2009, 03:19 AM
Fubarable
Then break down the problem into small steps and solve each small step one at a time. You should this way be able to figure out most of it. Then come on back if you run into trouble with one of the small steps. This way if you come back, you'll be asking a question that can reasonably be answered on the forum. Best of luck.
• 02-20-2009, 03:28 AM
cvubando
I am very new to java and I don't even know how to get the code started.
I don't know how to set the scale of a graph.
• 02-20-2009, 03:45 AM
Fubarable
then if you are truly lost, you may be beyond our help. Your best bet is to speak to your teacher.
• 02-20-2009, 03:56 AM
cvubando
One reason why I'm asking here is that I can't understand my teacher's explanations so I was thinking of asking someone else to help me understand.
• 02-20-2009, 04:02 AM
Fubarable
but you've yet to ask a specific question. All you've done is to post the assignment and state that you don't understand it. Please understand that specific questions will bring your specific answers. General unfocused questions will bring you, well, this thread.
• 02-20-2009, 04:10 AM
cvubando
Can someone show me the code on how to plot the function x0 + v0t – gt^2/2 using StdDraw? Also on how to set the x scale to 20 and the y scale to 500?
• 02-20-2009, 04:16 AM
Fubarable
to change the scale of a set of numbers you simply multiply the numbers by the a scale factor, here it would be 20 for the x axis and 500 for the y axis. There's nothing fancy about that.

To plot a graph an equation is more complex, and as noted above, you have to break the problem down into small problems and solve each small problem.

Steps would include obtaining the data, scaling the data (as noted above), figuring out a range an domain (max and min x and y values).

then if using Swing and Graphics2D, you'd want to create a graphing JPanel where the graphing would take place. If you are unsure how to draw on a JPanel I'd recommend the Sun graphics tutorials at the Sun tutorial site. You'd have to work a bit to learn how to do this, I'm afraid but it's time well spent.
I'm not clear what you mean by "StdDraw" though, and you may need to describe this further as there's nothing with this name in the standard Java library.
• 02-20-2009, 04:22 AM
cvubando
StdDraw is Standard Drawing. I thought this could be done using a normal text editor.
• 02-20-2009, 04:29 AM
Fubarable
• 02-20-2009, 05:46 AM
cvubando
Now I'm just having trouble plotting the data.

double x = Double.parseDouble(args[0]);
double v = Double.parseDouble(args[1]);
double t = Double.parseDouble(args[2]);
double g = 9.78033;

int N = 50;
double[] a = new double[N+1];
for (int i = 0; i <= N; i++)
a[i] = x + v*t - g*(t*t)/2;
StdDraw.setXscale(0, N);
StdDraw.setYscale(-10, 500);
for (int i = 1; i <= N; i++)
StdDraw.line(i-1, a[i-1], i, a[i]);

I'm not even sure if this is the right code to plot the data onto the graph.
• 02-20-2009, 06:11 AM
CJSLMAN
I haven't been paying much attetion to this post, but shouldn't the values of x, v t and/or g change ?
Code:

`a[i] = x + v*t - g*(t*t)/2;`
because if not, you'll be filling the array up with the same value.

Luck,
CJSL
• 02-20-2009, 08:34 AM
emceenugget
"t" should be changing. x0, v0 and g should be constants.

also why are you using this value for gravity? are you using a fictional gravity? i haven't had physics in close to 4 years, but i'm pretty sure that the gravity of earth is 9.81 m/s^2