Question

Newton invented the Newton-Raphson method for solving an equation. We are going to ask you to write some code to solve equations. To solve an equation of the form x2-3x + 2-0 we start from an initial guess at the solution: say x,-4.5 Each time we have the ih guess x, we update it as For our equation,f(x) = x2-3x + 2 andf,(x) = 2x-3. Thus, our update equation is x2 - 3x, 2 2x, - 3 We stop whenever f(x)l 10-8:i.e, we are very close to a root of the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A)

object HelloWorld {

    def fx(x: Double) : Double = {
        return x * x - 3*x + 2
    }

    def fxDash(x: Double) : Double = {
        return 2* x - 3
    }

    def calculateRoot(x0: Double) : Double = {
        var x = x0
        while(scala.math.abs(fx(x)) > scala.math.pow(10, -8)) {
            x = x - fx(x)/fxDash(x)
        }
        return x
    }

   def main(args: Array[String]) {
      println(calculateRoot(4.5))
   }
}
Execute l> Share HelloWorld.scala STDIN 1 object Hellokorld [ .lI Result def fx(x: Double) Double -{ return x* x 3*x 2 $scalac *.scala $scala HelloWorld 2.000000000444432 4 def fxDash(x: Double) Double return 2* x3 10 def calculateRootRec(xe: Double) : Double 12 13 14 15 16 17 18 19 20 def main(args: Array[String]) [ 21 if(scala.math.abs(fx(xe)) > scala.math.pow(10. -8)) return calculateRootRec(xe fx(xe)/fxDash(x0)) return ΧΘ println(calculateRootRec(4.5)) 23

B)
object HelloWorld {

    def fx(x: Double) : Double = {
        return x * x - 3*x + 2
    }

    def fxDash(x: Double) : Double = {
        return 2* x - 3
    }

    def calculateRootRec(x0: Double) : Double = {

        if(scala.math.abs(fx(x0)) > scala.math.pow(10, -8)) {
            return calculateRootRec(x0 - fx(x0)/fxDash(x0))
        }

        return x0
    }

   def main(args: Array[String]) {
      println(calculateRootRec(4.5))
   }
}

C)

object HelloWorld {

    def fx(x: Double) : Double = {
        return x * x - 3*x + 2
    }

    def fxDash(x: Double) : Double = {
        return 2* x - 3
    }

    def solveEquationNewtonRaphson(f: Double => Double, fDash: Double => Double, x0: Double) : Double = {

        if(scala.math.abs(f(x0)) > scala.math.pow(10, -8)) {
            return solveEquationNewtonRaphson(f, fDash, x0 - f(x0)/fDash(x0))
        }

        return x0
    }

   def main(args: Array[String]) {
      println(solveEquationNewtonRaphson(fx, fxDash, 4.5))
   }
}

Hi. please find the answer above.. In case of any doubts, please ask in comments. If the answer helps you, please upvote. I am in great need of upvotes. Thanks!

Add a comment
Know the answer?
Add Answer to:
Newton invented the Newton-Raphson method for solving an equation. We are going to ask you to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT