# Kwadratic formula

Printable View

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-26-2012, 11:19 AM
BURNY26
Kwadratic formula
Hi Im quite new to java and programming in general and I was hoping on getting some help with this piece of code.

class kwadratic {
public static void main(String []args){
for (double xxx=-1000; xxx<1000;xxx++){
double equation = xxx*xxx*3-8*xxx+4;
}
if (equation ==0){
System.out.println(xxx);
}
}
}

Im using Netbeans ...if it matters.:P:
• 03-26-2012, 11:24 AM
JosAH
Re: Kwadratic formula
That way you'll only find one root (2.0) if you're lucky; the other root (2/3) can not be found that way. Why don't you apply the close form formula (-b+-sqrt(b*b-4*a*c))/(2*a).

kind regards,

Jos
• 03-26-2012, 11:38 AM
BURNY26
Re: Kwadratic formula
Well I wanted to find the solution without the formula to make it as basic as possible.
• 03-26-2012, 11:41 AM
BURNY26
Re: Kwadratic formula
Is there any way of letting the xxx increment by ..lets say 0.01 instead of 1 ?
• 03-26-2012, 11:55 AM
JosAH
Re: Kwadratic formula
Sure, add 0.01 to variable xxx each time through the loop:

Code:

```for (double xxx= -1000; x < 1000; x+= 0.01)     // ...```
... but you still won't find the other root (2/3) that way.

kind regards,

Jos
• 03-26-2012, 11:59 AM
BURNY26
Re: Kwadratic formula
Sweet ,but i still get an error :
Attachment 3342
• 03-26-2012, 12:25 PM
noobplus
Re: Kwadratic formula
hi,
a variable cannot be accessed outside a block
also, every instance of the variable "equation" should be checked, but u're checking it only once after the end of the loop

regards
dhilip
• 03-26-2012, 12:32 PM
BURNY26
Re: Kwadratic formula
Uhm , in nooblanguage please. Or most preferably in code form ???:(blush):
• 03-26-2012, 12:34 PM
JosAH
Re: Kwadratic formula
Quote:

Originally Posted by BURNY26
Uhm , in nooblanguage please. Or most preferably in code form ???:(blush):

Put lines #6 and #7 before line #5.

kind regards,

Jos
• 03-26-2012, 12:39 PM
BURNY26
Re: Kwadratic formula
cheers ,case dismissedAttachment 3343:P:
• 03-26-2012, 12:51 PM
JosAH
Re: Kwadratic formula
So you're happy with this? Your program only finds solutions to your quadratic formula if the root is -1000+i*0.01. An ordinary quadratic formula isn't too difficult to implement ... it's not like a Jenkins-Traub method that works on arbitrary polynomials ...

kind regards,

Jos
• 03-26-2012, 01:27 PM
noobplus
Re: Kwadratic formula
Quote:

Originally Posted by JosAH
Sure, add 0.01 to variable xxx each time through the loop:

Code:

```for (double xxx= -1000; x < 1000; x+= 0.01)     // ...```

Hi,

adding "0.01" to "double xxx=0" gives us approximate double values, not exact increment
for example,
Code:

```for(int xxx=0;xxx<1;xxx+=0.01)   {     System.out.println(xxx);   }```
gives output
(first 15 output values..)
Code:

```0.0 0.01 0.02 0.03 0.04 0.05 0.060000000000000005 //this is non-exact 0.07 0.08 0.09 0.09999999999999999//non-exact 0.10999999999999999//non-exact 0.11999999999999998//non-exact 0.12999999999999998//non-exact 0.13999999999999999//non-exact```
this is because of rounding errors
hence even "2.0" may not be displayed
I learnt it at Java theory and practice: Where's your point?

regards
dhilip
• 03-26-2012, 01:32 PM
JosAH
Re: Kwadratic formula
Of course you haven't read my original reply:

Quote:

Originally Posted by JosAH
That way you'll only find one root (2.0) if you're lucky; the other root (2/3) can not be found that way.

Bold faced font added for clarity.

Jos
• 03-26-2012, 01:37 PM
noobplus
Re: Kwadratic formula
Quote:

Originally Posted by JosAH
Of course you haven't read my original reply:

Bold faced font added for clarity.

Jos

ok boss
• 03-26-2012, 02:32 PM
noobplus
Re: Kwadratic formula
Quote:

Originally Posted by JosAH
Of course you haven't read my original reply:

Bold faced font added for clarity.

Jos

Code:

```/** *@author:dhilipkumar(noobplus)  */ public class kwadratic {         static double equation;         static double incrementedvalue;         static double repeatchecker=-1;                 public static void main(String []args){                                                                                 for(int x=0;x<11;x++)                 {                                                         for(int i=1;i<11;i++)                 {                         for(int j=0;j<10;j++)                                         {                                         if(j<i)                                         {                         incrementedvalue=(x+(double)j/(double)i);                                                 equation = (incrementedvalue*incrementedvalue*3)-(8*incrementedvalue)+4;                                                 if ((equation==0) && (incrementedvalue!=repeatchecker))                         {                                                             System.out.println(incrementedvalue);                                 repeatchecker=incrementedvalue;                         }                                         }                                         }                                         }                                 }                                                                 } }```
output:
Code:

```0.6666666666666666 2.0```
we don't need to use "xxx+=0.01" :)-:

regards
dhilip
• 03-26-2012, 02:33 PM
JosAH
Re: Kwadratic formula
Quote:

Originally Posted by noobplus
we don't need to use "xxx+=0.01" :)-:

We don't need any iterations at all.

Jos
• 03-26-2012, 02:36 PM
noobplus
Re: Kwadratic formula
Quote:

Originally Posted by JosAH
We don't need any iterations at all.

Jos

and how?

dhilip
• 03-26-2012, 03:11 PM
JosAH
Re: Kwadratic formula
Quote:

Originally Posted by noobplus
and how?

dhilip

So you still haven't read this thread ...

Jos
• 03-26-2012, 03:17 PM
KevinWorkman
Re: Kwadratic formula
"Kwadratic" is my new favorite word.
• 03-26-2012, 03:20 PM
noobplus
Re: Kwadratic formula
Quote:

Originally Posted by JosAH
So you still haven't read this thread ...

Jos

yep..basic quadratic formula .. yep..
Quote:

Originally Posted by BURNY26
Well I wanted to find the solution without the formula to make it as basic as possible.

maybe he wanted to become a loop-master :P:

regards
dhilip
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last