Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree3Likes

Thread: Kwadratic formula

  1. #1
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Kwadratic formula

    Well I wanted to find the solution without the formula to make it as basic as possible.

  4. #4
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Kwadratic formula

    Is there any way of letting the xxx increment by ..lets say 0.01 instead of 1 ?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Kwadratic formula

    Sure, add 0.01 to variable xxx each time through the loop:

    Java 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
    BURNY26 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Kwadratic formula

    Sweet ,but i still get an error :
    Kwadratic formula-equationu.png

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default 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

  8. #8
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Kwadratic formula

    Uhm , in nooblanguage please. Or most preferably in code form ???

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Kwadratic formula

    Quote Originally Posted by BURNY26 View Post
    Uhm , in nooblanguage please. Or most preferably in code form ???
    Put lines #6 and #7 before line #5.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    BURNY26 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Kwadratic formula

    cheers ,case dismissedKwadratic formula-case_dismissed_gavel-779357.jpg

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Lightbulb Re: Kwadratic formula

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

    Java 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,
    Java Code:
    for(int xxx=0;xxx<1;xxx+=0.01)
      {
        System.out.println(xxx);
      }
    gives output
    (first 15 output values..)
    Java 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
    Last edited by noobplus; 03-26-2012 at 05:13 PM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Kwadratic formula

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



    Bold faced font added for clarity.

    Jos
    ok boss

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Lightbulb Re: Kwadratic formula

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



    Bold faced font added for clarity.

    Jos
    Java 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:
    Java Code:
    0.6666666666666666
    2.0
    we don't need to use "xxx+=0.01"


    regards
    dhilip
    Last edited by noobplus; 03-26-2012 at 02:40 PM.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Kwadratic formula

    Quote Originally Posted by noobplus View Post
    we don't need to use "xxx+=0.01"
    We don't need any iterations at all.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Kwadratic formula

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

    Jos
    and how?

    dhilip

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Kwadratic formula

    Quote Originally Posted by noobplus View Post
    and how?

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Kwadratic formula

    "Kwadratic" is my new favorite word.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Kwadratic formula

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

    Jos
    yep..basic quadratic formula .. yep..
    Quote Originally Posted by BURNY26 View Post
    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

    regards
    dhilip
    Last edited by noobplus; 03-26-2012 at 03:26 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Formula Logic Help
    By sehudson in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 04:17 AM
  2. Formula class
    By imorio in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 09:38 PM
  3. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 01:20 PM
  4. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 06:57 PM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •