Hello everyone!
I'm a Java newbie and I'm trying to create an applet that will draw a graph to find the roots of a cubic equation y = ax^3+bx^2+cx+d.
The user has to input the range and the values of a, b, c and d.
I've done the layout but now I don't know to implement the maths.

Can anyone help please?

I've done the layout but now I don't know to implement the maths.

The quick approach which comes to my mind is store a,b,c and d in an array say "coefficients" of size 4 and let the index at which the corresponding value is stored indicate the power of "x".
Ex:-

coefficients[0]=d;
coefficients[1]=c;
coefficients[2]=b;
.....

Now to calculate value of "y" corresponding to the given value of "x" would be as simple as summing up the value of :-

y = coefficients[index] * Math.pow(x,index);

for every element in the coefficients array.

The second approach to calculate the value of "y" which the programmer in me despises is the more mechanical approach as show below:-
Assumptions:- Current value of "x" is in variable "x" and same applies for a,b,c and d.

y = a * x *x *x + b * x * x + c * x + d;

Notes:-
If you wish to try the first approach you may read about arrays here and here.
Here and here are example how to parse through arrays using the "for" loop and "for each" loop respectively.
You can also see that javadocs of the Math class for the pow() method

Comments
Nice

Finding the roots of a cubic equation isn't trivial. See this wikipedia link and scroll down for a discussion and links to finding roots.

http://en.wikipedia.org/wiki/Cubic_equation

I personally think that using Newton's Method's and estimating the roots to a very small tolerance would be a better/easier solution than using the formulas which would calculate an EXACT value. The math involved in Newton's Method is much simpler.

http://en.wikipedia.org/wiki/Newton%27s_method

Comments
Nice

@Vernon
Apparently I too thought he was more interested in finding the roots of his cubic equation, but reading his posts I guessed he was more interested in just plotting the graph.

Thank you stephen84s and VernonDozier.

Yes, I need to find the approximate values from the graph.
The maths way is to differentiate the equation and find the minimum and maximum points of the curve (I guess its the Newton Method)
My problem is to implement the maths and plot the graph. I don't know much about arrays and math class but i'll try the tutos.

Again thanks a lot to both!! :)

The maths way is to differentiate the equation and find the minimum and maximum points of the curve (I guess its the Newton Method)

Which "curve" are you speaking of? If you are planning on taking the derivative of the cubic equation (resulting in a quadratic equation) and solving for when that is 0, you would not use Newton's Method, you would use the quadratic formula, and that would result in the vertices of the cubic equation, not the roots of the cubic equation. The minimum and maximum values of the cubic are going to be infinity and negative infinity. The minimum and maximum values of the cubic's derivative are also not going to give you the roots of the cubic.

The computer I am on doesn't currently have Flash, Power Point, Java, or Adobe Reader on it, so I can't access a lot of the good Newton's Method tutorials to link one, but there are many on the web and a few links at the bottom of the Wikipedia article I linked earlier. It is an iterative process. You WILL need to find the derivative of the cubic, but you won't be finding maximums and minimums.

This article has been dead for over six months. Start a new discussion instead.