So, the problem is: A line is determined by the equation y = a * x, where you are given a = 0.9.
Find approximate values of x and y for which the line intersects the curve determined by the equation y = x*x - 4x + 3.7
Your program does this and gives (x,y) = (0.93, 0.84) with an error in x of something less than 0.01.
I think the assignment might be to let the value of a go from 0.9 through 2.0 in steps of 0.1 and find an approximate intersection point for each such line with the given curve.
I'll rearrange your original program slightly in anticipation of letting the line be defined by
a*x (where
a is a variable) rather than being hardcoded to
0.9*x:
public class Z {
public static void main(String [] args) {
double x;
double stepsize;
double a;
a = 0.9;
x = 0.0;
stepsize = 1.0;
while (stepsize >= 0.01) {
while ((x * x - 4.0 * x + 3.7) > (a * x)) {
x = x + stepsize;
}
x = x - stepsize;
stepsize = stepsize / 10;
}
System.out.printf("The line %.1fX and the curve intersect at approximately (%.2f,%.2f)\n",
a, x, a*x);
} // end of main method
} // end of the class
I left out the counter stuff since it makes absolutely no sense to me.
Anyhow, the output is
The line 0.9X and the curve intersect at approximately (0.93,0.84)
Now, just make a loop that lets a go from 0.9 through 2.0 and put the calculations (starting with x = 0) and printout inside that loop
As for the requirement to count the number of steps, that still makes no sense to me, so maybe you can ask your instructor.
Bottom line:
Sometimes it helps to visualize what the program is actually doing. I have attached a plot of the curve and the lines for values of
a that I think you need. Each line intersects the parabola at two points. For a given value of
a, the program finds an approximate value of x for the intersection closest to the origin.
Final comment: If that is not what your assignment is, well, maybe you can articulate it in a way that we can understand. I gave it a shot.
Cheers!
Z