Math Problem

I need some help! I admit, I'm not a java guru...far from it, just a college student with a book that doesn't have many examples for what I'm looking for. Any help will be greatly appreciated!!!
My problem: My java program is to find a root using the Newton-Rapson method. The input is a function, i.e. f(x) = x^2 + x + 2. The program is to prompt the user for the beginning numbers, i.e. 225 and -43, and then find the answer to the nearest hundredth.
I can do this on paper - no problem, my problem is I don't know enough about Java to program it to do the math.
Unfortunately, if there are any predefined classes or methods that will do this problem for me, I'm NOT allowed to use them. Every step has to be hard coded. :(
I'm not sure how to get Java to recognize the + sign or
the ^ sign or any other non-numeric signs. I know there is something called substring, but I can't seem to find examples where it uses an integer. All the examples I have found are chars (like reading the string "hello"). Not helpful when you are trying to mix integers with symbols.
Any help offered would be greatly appreciated, and I'm sure would in-turn help me the the bisection algorithm and all the future algorithm's I have to put into a program.
Thanks for your time.

[mode=Pedantic] Its Newton-Raphson [mode]
Anyway use this as a starting point, its from a guy by the name of Neil Broadbent (see: http://www.neiljohan.com/java/ for lots of java samples)
This will calculate the square root of a number using Newton-Raphson factoring, you can use this as a starting point.
//Program to calculate the square root of a number using the Newton-Raphson.
//It stops when the difference between consecutive approximations is less than 0.00005
//by Neil Broadbent
import java.io.IOException;
import NeilClass.*;
public class NewtonRaphsonClass
     private static boolean debugging = true;
     /** @param tFactorial the value from which to find the square root
      *  precondition tFactorial>0
      *  @return x1 so that x1>0 */
    public static double iSquareRoot (double tFactorial) throws IOException
            double x1=0;
            double x0=tFactorial/2;
            double a=tFactorial;
            boolean finished=false;
     /* check pre-condition */
     if (debugging && (tFactorial<=0)){
          System.out.println("Pre-condition violoated");
          finished=true;
            while (finished==false)
                x1=(x0+(a/x0))/2;
                if (x1>x0){
                    if ((x1-x0)<0.00005){
                        finished=true;
                else if (x0>x1){
                    if ((x0-x1)<0.00005){
                        finished=true;
                x0=x1;
          /* check post-condition */
          if (debugging && (x1<=0)){
               System.out.println("Post-condition violoated");
            return x1;
}

Similar Messages

  • Looking for an Alarm Clock app where you do math problems to turn it off

    Someone told me of an application for the iPhone that is an alarm clock in which you can use a mode that, when the alarm sounds, you need to solve a math problem or other visual test in order to cease the alarm. That way the user is more awake by the time the alarm is stopped. Sounds annoying, yes, but would be just what I need.
    Anyone know which alarm app this is? Or if it even exists?

    There's another app that got a lot of press recently called *Proactive Sleep*.
    It works similarly to the math app mentioned above but you do a 30 second visual game when you wake up. It also records your score as well as your sleep patterns and can give you recommendations and other feedback to improve your sleeping habits.

  • Getting the top left corner of a grid cell (math problem)

    I'm making a map editor for a tile based game. The editor makes a grid:
    private function drawGrid():void {
                var grid:Sprite = new Sprite();
                var tileSize:uint= 50;
                for(var i:int=0;i<row;i++)
                    grid.graphics.lineStyle(1,0x000000,0.3);
                    grid.graphics.moveTo(0, i*tileSize);
                    grid.graphics.lineTo(700, i*tileSize);
                for(var j:int=0;j<column;j++)
                    grid.graphics.lineStyle(1,0x000000,0.3);
                    grid.graphics.moveTo(j*tileSize, 0);
                    grid.graphics.lineTo(j*tileSize, 400);
                stage.addChild(grid);
    And when the user clicks anywhere in the screen a movieclip is added to stage. The movieclip (exported for actionscript as Tile) should be placed in the top left corner of the grid cell.
    private function placeTile(mouseX:int, mouseY:int, frame:uint):void {
                var tile:Tile = new Tile();
                tile.gotoAndStop(frame);
                tile.x = PROBLEM HERE
                tile.y = PROBLEM HERE
                stage.addChild(tile);
    How can I do that? I came up with an idea but it didn't work because I have a math problem.  Let's pretend that the user mouse is at position mouseX = 456:
    I could remove the hundreds and use the quotient 56 and check if it is above 50. If it is above 50 I would place it at x = 500 if not I would place it at x = 450. How can I remove the hundreds? Do you have a better idea?
    Message was edited by: wilsonsilva7 because unconsciously I pressed ctrl+s (to save) and the message was posted

    Thanks. I asked a friend and he told me about the %but I still had a problem. Your expression solved my problem
            private function placeTile(mouseX:int, mouseY:int, frame:uint):void {
                var tile:Tile = new Tile();
                tile.gotoAndStop(frame);
                tile.x = Math.floor(mouseX / tileSize) * tileSize;
                tile.y = Math.floor(mouseY / tileSize) * tileSize;
                stage.addChild(tile);
    That way every tile is centered every 50 pixels

  • Strange math-problem with LabVIEW 7.1

    Hello!
    I am using LabVIEW 7.1 and I came to this strange math-problem. Could it be that it is a problem of LabVIEW 7.1?
    Ok try this with LabVIEW 7.1:
    open a new VI
    place a subtract-VI (pallet numerical) on the block diagram and try to subtract 0.2 from 1.6
    place a less-VI (pallet compare) on the block diagram and wire a numerical constant 1.4 to input x
    wire the result of the subtraction to input y of the comparison
    place an LED to the output of the comparison
    and start this.
    What does it say?
    The LED should turn on and this means here that 1.4 would be less than 1.4 .
    But I would have expected that 1.4 is not less than 1.4 .
    This works also if you add or subtract upto 0.2 to/from the values 1.4 and 1.6 (or equivalent with 0.2) .
    If you change the inputs of the comparison then the result is as expected and 1.4 is reported not to be less than 1.4 .
    If you compare two constants with the same value 1.4 you will not get the same strange result.
    Does anybody have an explanation for this?
    Espelkamper

    Hello,
    For floating point math: Rounding to multiples of 0.1 is problematic as
    those numbers are infinite fractions in base2 representation...
    So maybe you should change your numbers to integers using fixed point math. If all your input values are in the range of 1.4 to 1.8 you can multiply by 10000 and round to next integer. Then do your comparison on that integer (there's also an 'InRange' function in the comparison palette).
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Maths Problem in Action Script 2.0

    Hi surfers,
    We have a problem in Action Script 2.0 to solve the maths equation given below:
    write a code in Action Script 2.0 to solve these two equation.
    Aim: To know the value of "a" in both different equations.
    1. 2a + 5 = 3a + 1
    2. 4a + 3 = 11
    Thanks in Advance.
    Pls give me ASAP, i'm struggle here.

    Hello, you are at the wrong forum. for AS 2.o questions, hop on there: http://forums.adobe.com/community/flash/flash_actionscript
    BTY, people answer when they can, not when you want...

  • Logic for this math problem

    Before I get stinker replies for cross posting , let me tell that , i have been recommended to post this in this forum.
    My problem is such:
    I need to find a logic to implement the following :
    In the floating point number , say , 1.0.. the Integral part will remain constant(1) but the decimal part has to get incremented by 1
    1.1 +0.1
    1.2+0.1
    1.3+0.1
    1.4+0.1
    1.5 +0.1
    1.6+0.1
    1.7+0.1
    1.8+0.1
    1.9+0.1
    1.10+0.1
    1.2... But what I need here is 1.11 ,1.12
    I need my programming logic to detect there needs to be a change in the increment value from 0.1 to 0.01 when I reach 1.1
    and from 0.01 to 0.001 when i reach 1.111..so on,
    please suggest..

    you can't do it with a double,
    since double d = 1.10d; will be treated as 1.1d
    what you can do is treat the double as a String
    public void test(){
    String s1 = "1.9";
    String s2 = "1.10";
    String s3 = "1";
    String s4 = "1.1";
    System.out.println(increment(s1));
    System.out.println(increment(s2));
    System.out.println(increment(s3));
    System.out.println(increment(s4));
    public String increment(String d){
    int pos = d.indexOf(".");
    if (pos != -1){
    if (d.charAt(d.length()-1) == 0)
    return d.substring(0, d.length()-2) +
    ength()-2) + "1";
    String temp = d.substring(pos+1);
    int num = Integer.parseInt(temp);
    num++;
    return d.substring(0, pos+1) + num;
    else
    return d + ".1";
    }gives
    1.10
    1.11
    1.1
    1.2
    I leave it to you to understand the logic...it's to
    get you started..it does not check for all conditions
    of the inputI think it is a Math with algorithm problem. Your solution is good, but you are using String functions so much. There is other solution, handling with numbers only.

  • How can I use superscript in math problems presented on Firefox

    Working in KahnAcademy.org, there are math videos to explain an issue and then trial problems to test learning. In the Pre-Algebra section there are some videos regarding logarithms, To write a logarithm one needs to write a number with its exponent. A 2 for squared, 3 for cubed etc. To write the exponent it should be in superscript.
    I need to be able to write the exponent wherever I write something when within Firefox.

    Sorry, Firefox for Android is not yet compatible with Adobe Flash Player. We've started work to add Flash support, and you can follow our progress on this page: https://wiki.mozilla.org/Fennec/Features/Plugins

  • Creating a form with a field that contains a simple math problem.

    I am creating a form in Acrobat 9 Standard that contains a field that requires a simple math function (divide).
    if (QuantityRow1 > 0)
    ExtensionRow1 / QuantityRow1
    Take the value input in ExtensionRow1 and divide it by the value in QuantityRow1. I have included an IF statement to prevent an error that occur if QuantityRow1 were equal to zero.
    After inputting the data into the two fields I tab past the field where I expect to see my result and the field remains blank. I don't receive any error messages.
    Any ideas? Thanks - jb

    This is a duplicate of your question on Acrobatusers.com (
    http://answers.acrobatusers.com/Need-simple-math-operation-function-Acrobat-9-Standard-cre ating-form-q142800.aspx
    Did the information provided there not help you with your problem?
    Karl Heinz Kremer
    PDF Acrobatics Without a Net
    PDF Software Development, Training and More...
    [email protected]
    http://www.khkonsulting.com

  • AS3 core maths problem

    Hi. If you run the code at the bottom in AS3 you get this:
    0.1
    0.2
    0.30000000000000004
    0.4
    0.5
    0.6
    0.7
    0.7999999999999999
    0.8999999999999999
    0.9999999999999999
    if you run it in AS2 you get
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    1
    the as2 version is what you would expect. Anybody know why
    this is?

    hutn.jimbo,
    > Thanks for the reply Rothrock, but that does
    > seem strange. If you add 0.1 to something, it
    > should go up by that.
    In a theoretical sense, of course, you're right. The problem
    isn't with
    your arithmetic ability, but rather with the basic problem of
    how computers
    represent decimal values. I wish I could explain it better
    than that (it
    definitely surprised me when I first saw it), but you do get
    used to it.
    The same output occurs in JavaScript:
    <script>
    var counter = 0;
    for (var i=0; i<10; i++) {
    document.write((counter += 0.1) + "<br />");
    </script>
    > I don't really know how to get the output I'm after,
    > as sometimes you would need to round down and
    > other up.
    I can think of two ways in this scenario. Either multiply
    your value by
    10, round, then divide it by 10 (or whatever decimal place
    makes sense) ...
    var myNum:Number = 0;
    for (var i:Number = 0; i < 10; i++) {
    myNum *= 10;
    myNum += 1;
    myNum /= 10;
    trace(myNum);
    ... which is your only choice (I think) with ActionScript
    2.0. In
    JavaScript, you could use the Number.toFixed() method:
    <script>
    var counter = 0;
    for (var i=0; i<10; i++) {
    document.write((counter += 0.1).toFixed(1) + "<br
    />");
    </script>
    David Stiller
    Co-author, Foundation Flash CS4 for Designers
    http://tinyurl.com/5j55cv
    "Luck is the residue of good design."

  • Does labVIEW have a function to solve the following math problem?

    In Labview what is the easiest way to solve the following problem?
    |----             ---|  -1       |--     ---|                |--       --|
    |  1   1   0       |            |   a     |                |    x     |
    |  1   0   1       |            |   b     |     =         |    y     |
    |  0   1   1       |            |   c     |                |    z     |
    |__             __|            |_       _|                |_       _|
    from the equations:
         a = x + y
         b = x + z
         c = y + z         where a, b, c are know values.    The problem I actually have is a system of 9 equations.
    Thank you.

    You have a known matrix, a known vector, and a vector you need to solve for given the equation Ax = b.  This is exactly the problem this VI is designed to solve:
    With the values I made up for a, b, and c, the solution is [2, 3, 4].
    Chris M

  • A stupid MATH problem

    Hello and thanks for entering this topic,
    I'm sure you can help me in my problem. I'm writing a soccer game,and I have just started to edit the flight of the ball,but I have a shameful problem. I want to calculate the null points of a parabola,but I don't know how JAVA calculates the extraction of a root. Is there any operator for that?
    If yes,please let me know
    BYE:AWEXcg

    If you want it to handle anything like a real football, you'll need to bring in some fluid dynamics. At the very least you'll want a basic Bernoulli effect implementation. There's a great article on football dynamics at http://physicsweb.org/article/world/11/6/8

  • Easy math problem...I suppose....

    I am trying to make these calculations in a Java program, but they will not simply work.
    I want to power a variable r(the radius of a sphere) with 2 and then 3.
    v(volume), a(area) and r(radius) are all Integer.
    What am I doing wrong?
    v = (4*(Math.PI)*rE3)/3;
    a = 4*Math.PI*rE2;
    Many thanks
    /Martin

    rE2 - looks like you're assuming that means 'raise r to the 2nd power'. It doesn't (it just looks for a variable named 'rE2').
    Use Math.pow(r, 2)

  • Help with a math problem.

    Hi all,
    I'm trying to get acclimated to Java and I have an arithmetic problem that I'm not too sure where to begin with.
    One way to measure the amount of energy that is expended during exercise is to use metabolic equivalents (MET). Here are some METS for various activities:
    Running 6 MPH: 10 METS
    Basketball: 8 METS
    Sleeping: 1 MET
    The number of calories burned per minute may be estimated using the formula:
    Calories/Minute = 0.0175 x MET x Weight in kilograms
    Write a program that calculates and outputs the total number of calories burned for a 150-pound person who runs 6 MPH for 30 minutes, plays basketball for 30 minutes, and then sleeps for 6 hours. One kilogram is equal to 2.2 pounds.
    I'm assuming I need to declare variables, so I did that. But I'm not sure where to go after doing that. Can I please get some direction here? Any help would be much appreciated I am DESPARATE here. Here's what I've got so far:
    +public class mets {+
    +public static void main(String[] args) {+
    +// Variable declarations+
    int numRunning = 10;
    int numBasketball = 8;
    int numSleeping = 1;
    +}+
    +}+
    Thanks,
    Stephanie

    You were given a formula
    Calories/Minute = 0.0175 x MET x Weight in kilograms
    and you need to calculate calories. Values for all others in the formula are given. Simply rearrange the formula so calories is on one side by itself and calculate.

  • A little math problem

    I have built a slider that for arguments sake is 100px long
    and outputs a number from 0 to 100 depending on where you drag the
    handle to. If you imagine a graph plotting the output against the
    position of the slider handle it would be a straight line going
    steadily upwards on a diagonal.
    My question is this:
    Can I set it up so that the graph would go up very slowly
    until near the end and then suddenly rise sharply (I'm sure there
    is a mathematical term for this type of curve)?
    In other words if you dragged the handle from the 0 end over
    the first 50px (50% of the total) the output would only go from say
    0 to 10 then in the next 25px it would rise from 10 to 50 then in
    the last 25px from 50 to 100.
    I have played around with hard coding values and using
    weighted arrays but this seems very untidy. I have also dabbled
    with Math.log(), Math.exp() etc but my maths is pretty shoddy (as
    you may have gathered from my rambling explanation of the issue :)
    If anyone can shed any light on the issue it would be greatly
    appreciated. I am fascinated by maths and its relation to patterns
    and lines but it is all a bit above my head (at the moment)
    Cheers all

    y = (x^a)/(b^a)*b is the same as y = (x^a)/(b^[a-1]) , for
    one thing. Not a biggie, but it might simplify things some
    internally.
    You might also try using what's called a piecewise function,
    which is defined as different equations, depending on its input.
    So, in your example, you'd want to define it as, say, x/10 for x
    between 0 and 50; (x-10)/0.8 +10 for the second part, and
    (x-50)*2+50 for the third part. It's not really elegant, but it's
    pretty intuitive, which is important for learning new ideas.
    If precise accuracy's not important and you just want to get
    a curve going, an Excel spreadsheet is a quick and dirty way to get
    an equation. Type in your known x coordinates in one column, and
    the associated y coordinates in another column. Graph them, with
    the XY (Scatter) type. Then, right-click on a data point, and click
    Add Trendline. Play around with that, and be sure to select 'show
    equation' and 'show R-squared value' under options. The closer
    r-squared is to 1.0, the more perfect a match the displayed
    equation will be.
    Finally, I've found sosmath.com to be a good introductory
    resource, and
    http://mathforum.org/library/
    looks fairly promising, as well.
    Sorry if that's a bit of a brain dump, but I hope it helps
    -Bob

  • Hi i need  help with a maths problem

    I have to find the inital value of a loan
    The information given is the payment per month, rate of interest, months of loan
    Premium(double payment, double rate, int months);
    for (int i=1; i<= months; i++)
    double newRate = rate;
    newRate /= (100 * 12);
    premium += payment;
    premium -= (premium * newRate)
    This does not work please help
    example if
    for Interest = 12.0
    payment = 101.0
    m = 1
    Expected inital loal = 100.0
    thanks

    sorry for lack of info
    public void computePremium(double payment,double
    rate, int months);
    for (int i=1; i<= months; i++)
    double newRate = rate; // yearly rate
    changed to months
    newRate /= (100 * 12);
    premium += payment;
    premium -= (premium * newRate)
    premium being the inital loanThat doesn't even compile.
    KajI think premium is a class variable of some sort. But
    it doesn't get reinitialized which can't be good.I was rather refering to the fact that the method declaration had an ; at the end, and that there were no "body" of the method declared. The code was just "hanging" in the air.

Maybe you are looking for

  • How to make XSLT files in ORACLE :-(

    Hi I am very new to the world of XML. I can create XML from the SQL queries but need to transform them in to a specific format. I think I need XSLT for transformation, but I could not find a way to make one using oracle. I want to ask, if there is an

  • Decide the path at run time

    II have a property file in my application. And I have to specify the path in code for that property file. But in code if I use "String curDir = System.getProperty("user.dir");" to find the path , in that case the path available is the path where code

  • CC - Parameter ID for Supplementary Rule.

    HI All, I was trying to create a supplementary rule for the users using transction F-53 and want to restrict the user by the amount in the bank data field in f-53. For the same I had two questions: 1. In doing the same I am stuck up with the Paramate

  • After effect CC  - impossible de travailler avec Mocha AE

    impossible de travailler avec mocha, une fois ouvert Mocha réponse fichier non trouvé. J'ai essayé avec des fichiers TIF TGA ou MOV aucun n'est reconnu par mocha AE CC

  • Why is the "missing favicon" icon gone on new tabs?

    When I open a new tab, or I am on a page with no favicon, the little dotted line square that used to be present is now gone. Is this intentional? And if it is, can I bring it back, preferably with the classic theme restorer?