Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Not sure what kind of loop I need to use for an intersection program

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Not sure what kind of loop I need to use for an intersection program

    We were given a code that we need to expand from. The code finds and prints the x and y coordinates of the intersection point of a line and it's curve. We have to modify and add to the program so that it finds all the coordinates where x varies from 0.9 to 2.0 with 0.1 increments. It also has to calculate the number of steps taken to find the solution.

    // Homework Program Intersection inter.java
     
    public class inter
     
    {
    	public static void main(String[] args)
    	{
    		double x;
    		double stepsize;
    		int counter;
     
    		{
    		x = 0.0;
    		stepsize = 1.0;
    		counter = 0;
     
    		{
    		while (stepsize >= 0.01)
    		{
    			while ((x*x - 4.0*x + 3.7) > (0.9*x))
    			{
    				x = x + stepsize;
    			}
    			x = x-stepsize;
    			stepsize = stepsize / 10;
     
    			if (stepsize <=2)
    				{
    				++counter;
    				}
     
    		}
    		System.out.printf("%s%6.2f\n","The line 0.9X and the curve meet at x = ",x);
    		System.out.printf("%s%6.2f\n", " and y =", x*0.9);
    	}
    }
    } // end of main method
    } // end of the class

    I tried to add in
    for ( double count = 0.9; count <= 2; count+=.1)
    but it just repeated the initial thing over and over again. I'm not sure if I should use an IF loop to make this work. The counter isn't adding correctly either, but that's not surprising since it's not doing the other part right either. We aren't on arrays yet.

    I really am not grasping java programming. This is due tomorrow night. I was in a car accident on Tuesday that has thrown me way off track this week. If anybody is willing to help give me an idea of where to go with this, I'd really appreciate it. I'm not looking to have it done for me, just give me an idea of what kind of loop will make this work? I think I can probably get it from there.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Not sure what kind of loop I need to use for an intersection program

    Quote Originally Posted by sessypeanut View Post
    We were given a code that we need to expand from. The code finds and prints the x and y coordinates of the intersection point of a line and it's curve. We have to modify and add to the program so that it finds all the coordinates where x varies from 0.9 to 2.0 with 0.1 increments.
    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
    Attached Files Attached Files

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what kind of loop I need to use for an intersection program

    How do you find the second intersection?









    Quote Originally Posted by Zaphod_b View Post
    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

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Not sure what kind of loop I need to use for an intersection program

    Quote Originally Posted by lucky13 View Post
    How do you find the second intersection?
    Here's a recap of how this method found the the first intersection: (Refer to the graphs in my previous post.)

    For a given value of a we know the the point on the curve at x = 0 is above the the point on the line at that value of x. (That is, evaluating the equation of curve at x = 0 gives a number that is greater than the value obtained by evaluating the equation for the line at x = 0.)

    So: Find an approximation of the value of x where the two intersect by incrementing x until we find a value of x for which the point on the curve is below the point on the line.

    Then, we know that the intersection is between the last two points. This can be proved since function for the curve and the function for the line are both continuous. (Refer to the Intermediate Value Theorem in your favorite elementary mathematical analysis text.)

    Now, to find the second intersection, start at the value of x for which the point on the curve is below the point on the line. That is, start with a point just to the right of the approximate value of x of the first intersection. The point is below the line, right? Then increment x until we find a value of x for which the point on the curve is above the point on the line. Then we know that there is an intersection between the last two values of x.

    In other words, add another set of nested loops after the loops in the previous program. The second set of loops looks pretty much like the first, but the termination condition of the inner loop has '<' rather than '>' in the expression.

    For a given value of a, the result from the enhanced program could look something like

    The line 0.9X and the curve intersect at approximately (0.93,0.84)
    The line 0.9X and the curve intersect at approximately (3.96,3.56)


    Also...

    I now realize what was meant in the assignment by counting the number of steps taken. I'm not sure why I didn't see it before:

    Define an int variable named counter.

    Before the outer loop, set counter equal to zero.
    Increment the counter value every time it goes through the inner loop. (That is, increment the variable each time you get a new value of x.)
    Print the value of counter after the outer loop terminates.

    Bottom line: If you can find two values of x where the line is above the curve for one value and the line is below the curve for the second value, you know they intersect somewhere between those two values of x. For any given functions, looking at their graphs might give some insight into developing a search strategy.

    The assignment defines a strategy that can work for the given functions, starting at x = 0 and with an initial step size of 1. These values might not work for other functions.


    Cheers!

    Z

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what kind of loop I need to use for an intersection program

    Thank you very much for the help.

    I now understand.

    Best Regards





    Quote Originally Posted by Zaphod_b View Post
    Here's a recap of how this method found the the first intersection: (Refer to the graphs in my previous post.)

    For a given value of a we know the the point on the curve at x = 0 is above the the point on the line at that value of x. (That is, evaluating the equation of curve at x = 0 gives a number that is greater than the value obtained by evaluating the equation for the line at x = 0.)

    So: Find an approximation of the value of x where the two intersect by incrementing x until we find a value of x for which the point on the curve is below the point on the line.

    Then, we know that the intersection is between the last two points. This can be proved since function for the curve and the function for the line are both continuous. (Refer to the Intermediate Value Theorem in your favorite elementary mathematical analysis text.)

    Now, to find the second intersection, start at the value of x for which the point on the curve is below the point on the line. That is, start with a point just to the right of the approximate value of x of the first intersection. The point is below the line, right? Then increment x until we find a value of x for which the point on the curve is above the point on the line. Then we know that there is an intersection between the last two values of x.

    In other words, add another set of nested loops after the loops in the previous program. The second set of loops looks pretty much like the first, but the termination condition of the inner loop has '<' rather than '>' in the expression.

    For a given value of a, the result from the enhanced program could look something like

    The line 0.9X and the curve intersect at approximately (0.93,0.84)
    The line 0.9X and the curve intersect at approximately (3.96,3.56)


    Also...

    I now realize what was meant in the assignment by counting the number of steps taken. I'm not sure why I didn't see it before:

    Define an int variable named counter.

    Before the outer loop, set counter equal to zero.
    Increment the counter value every time it goes through the inner loop. (That is, increment the variable each time you get a new value of x.)
    Print the value of counter after the outer loop terminates.

    Bottom line: If you can find two values of x where the line is above the curve for one value and the line is below the curve for the second value, you know they intersect somewhere between those two values of x. For any given functions, looking at their graphs might give some insight into developing a search strategy.

    The assignment defines a strategy that can work for the given functions, starting at x = 0 and with an initial step size of 1. These values might not work for other functions.


    Cheers!

    Z

Similar Threads

  1. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  2. [SOLVED] What kind of object instantiation is this?
    By Lord Voldemort in forum Object Oriented Programming
    Replies: 3
    Last Post: July 7th, 2011, 07:00 AM
  3. Need a program using only for loop its urgent
    By pokuri in forum Object Oriented Programming
    Replies: 2
    Last Post: January 14th, 2011, 05:13 AM
  4. sync two diffrent kind of threads?
    By adamruss in forum Threads
    Replies: 1
    Last Post: January 10th, 2010, 08:59 PM
  5. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM