-
polynomials
I'm wanting to create a program that graphs polynomials...One thing I don't understand, is how to create the points from the polynomial given.
Say I am given this:
5x^5 + 4x^4 + 3x^3 + 2x^2 + 1x + 1
how would I create coordinates out of this? All the user will be able to do, is give the coeficents in the polynomial..
So, just this:
5x^5 --- how could you get x,y coordinates out of that?
-
Re: polynomials
I'm assuming you have some sort of set resolution you are working with here...
For example, you know that the window you are graphing to will be K pixels wide.
If this is that case, just go across from the minimum x value to the maxium one, at certain steps, such that you have > K steps from minX to maxX. (looks best with some integer multiple of K).
i.e. say you want to draw y = x^2 + 2x - 1 from x = -10 to x = 10, and you know that you have 200 pixels to draw into, you can do this:
1) a loop of 10000 (= 50 x 200) is fast enough (presumably)
2) split the -10 -> 10 interval into 10000 steps
3) calculate the X value, and then the Y value for that spot
4) Convert this (X, Y) into screen coordinates on the grid you are drawing to
5) Plot the point
Code:
for(int i = 0; i < 10000; i++){
double Xval = -10 + (10 - (-10)) * (i / 9999);
double Yval = f( Xval ); // whatever function
Plot(Xval, Yval); // some function that takes coordinates and draws a spot
}
hopefully, the multiplier is large enough to make the line look continuous, but small enough for it to draw quickly. If it looks broken, use more points. If it takes ages to draw, use less.
-
Re: polynomials
Thank you sql_lall, that helped me A LOT! By the way, you are extremely smart!
-
Re: polynomials
As a compromise between speed and accuracy you could choose to draw lines as poposed to points.
That way you could get a ways with less point calculations while keeping the line looking continuous.
Instead of polting the pixel, just log it's value then when you have the next one draw a line joining them (then log that new pixel).
-
Re: polynomials
That's what Im going to do, it's just I had to know a way of getting the points first from the polynomial.
-
Re: polynomials
I hate to bring this up again, but I don't think I have the right equations...It's suppose to give the user the option of having up to a 5th order polynomial, by way of JSliders...
It looks a lot like this sites, but differently coded, and I want up to the 5th order:
http://tecfa.unige.ch/staf/staf-e/pa...4/welcome.html
here is the equation:
Code:
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(150,0,150,300);
g.drawLine(0,150,300,150);
g.setColor(Color.black);
g.drawRect(0,0,300,300);
if (coefs != null) {
g.setColor(Color.white);
calcpoints();
g.drawPolyline(x_points, y_points, x_points.length);
}
}
public void calcpoints() {
Dimension size = getSize();
for (int i=0; i<x_points.length; i++)
{
double x = i * (x_max - x_min) / x_points.length + x_min;
double y = 0.0;
for (int k=0; k<coefs.length; k++)
{
y = y*x + coefs[k];
}
x_points[i] = (int) (size.width * (x-x_min) / (x_max-x_min));
y_points[i] = (int) (size.height * (y-y_max) / (y_min-y_max));
}
}
Please look at this, because it's not working...If needed, I can post the whole program.
-
Re: polynomials
xmax,ymax = 10;
xmin,ymin = -10;