# Thread: Help! Plotting a function

1. Member
Join Date
Feb 2009
Posts
8
Rep Power
0

## 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.

2. Member
Join Date
Nov 2008
Posts
4
Rep Power
0
What is your question?

3. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
Simply put, I do not know how to write the code for it.

4. 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.

5. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
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.

6. then if you are truly lost, you may be beyond our help. Your best bet is to speak to your teacher.

7. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
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.

8. 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.

9. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
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?

10. 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.

11. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
StdDraw is Standard Drawing. I thought this could be done using a normal text editor.

12. Hm, I've not heard of it, and can't help you much with it. Perhaps others here may know more about this, but I have my doubts. If you have any other information about this and you still need help, please post it here.

13. Member
Join Date
Feb 2009
Posts
8
Rep Power
0
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.

14. I haven't been paying much attetion to this post, but shouldn't the values of x, v t and/or g change ?
Java 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

15. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
6
"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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•