[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 15 KB, 276x183, index.jpg [View same] [iqdb] [saucenao] [google]
6098737 No.6098737 [Reply] [Original]

Alright /sci/ so I've been stuck on a computer homework assignment for 2 days now, it's basic stuff I know, but I still do not understand why my program will not work correctly, can anyone give some advice? The program is supposed to find the quadratic formula


Your program should receive 4 parameters through Command Line Arguments: Tolerance, a, b, and c. Exit the
program if there are less than 4 parameters. Use a while loop to make sure that the user has entered appropriate
values (Tolerance <= 0.01, the value of a should be different from 0, and b^2-4ac>=0)
In the file QuadraticEq.java, a few variables have been already declared and initialized for you:
double x_min=-10, x_max = 10, x, step = 0.01;
double x1, x2, y;
int max_iter = 100000;
Now we are going to be looking for a solution. You can do this either with a for loop starting at x_min and
ending in x_max and with a step size of 0.01; or with a while starting a x_min, and steps of size 0.01. In any
case, you start at x_min, so set x to x_min, and y = ax2 + bx + c. Check if |y|<Tolerance. If it isn't, move x to
x+=step, and calculate y again. Check again if |y|<Tolerance. If it isn't, move x again. Continue this procedure
until |y|<=Tolerance. This means that ax^2+bx+c is very close to 0, and we have found a solution.

Will post my code below

>> No.6098740

This is the code I have right now, and it does not work. I probably messed up while writing the loop, but am unsure any help would be greatly appreciated
/*Using x_min, set an initial value for y, using the equation for the quadratic formula (i.e. y = ax^2+bx+c). if |y|>Tolerance, then avance x to x+step, and calculate a new y. Check again if |y|>Tolerance. Continue doing this until |y|<=Tolerance. */
x=-10;
double y = a*(Math.pow(x,2))+b*x+c;
for(x = -10; Tolerance >= Math.abs(y);x+=step) {


x1 = x+=step;
System.out.println("Equation " +a+ "x^2 " +b+ " x" + c+ " has a solution at x1 = " +x1);

}

>> No.6098742

/*Once |y|<=Tolerance, save in the variable x1 the x that made this possible. In other words, x1 will be such that |ax1*x1+b*x1+c| <= Tolerance. */
Then I need to do this then display results, again any help would be much appreciated as I am having trouble understanding what to do

>> No.6099879

I don't know java, but I would do this:

for(double x=x_min; x<=x_max; x+=step)
{
y= a*(Math.pow(x,2))+b*x+c;

if ((Math.abs(y))<=Tolerance)

{
x1=x;
break;
}
}

System.out.println("Equation " +a+ "x^2 " +b+ " x" + c+ " has a solution at x1 = " +x1);

>> No.6100220
File: 210 KB, 1024x768, Jshit.jpg [View same] [iqdb] [saucenao] [google]
6100220

>>6098737
>java
>math

Kill yourself

>> No.6100296

>>6100220
>>>/g/
...

"homework assignment" : maybe he doesn't chose the language...