You compute the y coordinates corresponding to the x values of a given range.
You transform arbitrary ranges (input and output) to sketch coordinates (width, height).
You draw lines from a previous (x, y) point to the next one.
OK, here is a simple quick example to get you started:
Code:void setup()
{
size(640, 480);
background(255);
stroke(#000055);
noLoop(); // Just draw once and stop
}
void draw()
{
int prevX = -1, prevY = -1;
for (int posX = 0; posX < width; posX++)
{
// The x coordinate in the math world
float x = map(posX, 0, width, -5.0, +5.0);
// The corresponding y coordinate, using the function of your choice
float y = sin(x);
// Map back to sketch world
int posY = (int) map(y, -1.0, +1.0, 0, height);
if (prevX >= 0)
{
line(prevX, prevY, posX, posY);
}
prevX = posX;
prevY = posY;
}
}
If that's for homework, try and understand it (ask if you have doubts) instead of regurgitating it as is.
Exercise: put margins around the graph, add axes, etc.