@sthomason1/# Newton's Method

Files

- Main.java
- jdt.ls-java-project

Main.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

```
/*
* Shyam Kurup
* MCS 140
* Complete the Newton's Method visualizing program
* by creating the static methods below labeled TODO
* and completing the static method newtonsMethod() below
*/
public class Newtons {
/*
* Create a static method called f() to compute: f(x) = (e^x) * sin(x) - 5x^3 + 2
*
* The method should take a double value as an argument (the x-value)
* The method should return a double value (the f(x)-value)
*/
//TODO: create the method here
public static double f(double x) {
return Math.exp(x)*Math.sin(x)-5*Math.pow(x,3)+2;
}
/*
* Create a static method called drawGraph() to draw a graph by connecting the points
* GIVEN in two (parallel) arrays representing the x-values and the y-values
* of a set of ordered pairs on the graph
*
* The method should take two arrays of double values as arguments
* (an array of x-values and an array of y-values)
* The method should return nothing, instead it should use StdDraw to
* "connect the dots" with line segments
* Note: the arrays will already have the x and y values, you just need to draw
* the line segments connecting the points
*/
//TODO: create the method here
public static void drawGraph(int[] xvals, int[] yvals) {
for(int i = 1; i < 100; i++) {
StdDraw.Line();
}
}
/*
* Complete the static method below, newtonsMethod()
* 1) Compute the approximations given by the method using Netwon's Method.
* Display each successive approximation: x1, x2, x3, etc.
* Complete the method when two successive approximations are within 0.0001
* of each other, i.e., their absolute difference is <= 0.0001
* Return the final approximation.
*
* 2) As the approximations are computed,
* draw (in red) a line connecting the point
* on the function at the current approximation to the point
* on the x-axis at the next approximation (this is a tangent line) and
* another line connecting the point on the x-axis at the next approximation
* to the point on the function at the next approximation.
*
* So if the current approximation is x1 and the next approximation is x2
* then draw a line connecting (x1,f(x1)) to (x2, 0) and another line
* connecting (x2,0) to (x2, f(x2)).
*
* When drawn this should illustrate the series of tangent lines that converges
* on the root.
*
* Note: you can use the method f() you created to compute the f(xn) values
* and the completed method below, fprime(), that approximates the derivative,
* f'(xn).
*/
public static double newtonsMethod(double guess) {
//TODO: complete the method
}
//*******DO NOT MODIFY THE METHODS BELOW**************
/*
* the main method is where the code begins executing
*/
public static void main(String args[]) {
//set up the drawing window
double xMin = -8;
double xMax = 8;
double yMin = -1200;
double yMax = 2000;
StdDraw.setXscale(xMin,xMax);
StdDraw.setYscale(yMin, yMax);
//draw the x & y axes
StdDraw.line(xMin,0, xMax,0);
StdDraw.line(0,yMin, 0, yMax);
//draw the function
int N = 1000; //number of points to compute
double deltaX = (xMax - xMin) / N; //step size between points
double xVals[] = new double[N+1]; //array of x-values
double yVals[] = new double[N+1]; //array of y-values
computePoints(xVals, yVals, xMin, deltaX);
drawGraph(xVals, yVals);
//begin Newton's Method
System.out.println("Root of f(x) is approximately: " + newtonsMethod(xMin));
}
/*
* this method approximates f'(x) using the limit definition of the derivative
*/
public static double fprime(double x) {
return (f(x+h)-f(x))/1e-6;
}
/*
* this method fills the given arrays with x-values and y-values
* corresponding to points on the graph of f(x)
* x-values start at xMin and increase by the given step size
* until the array is full.
*/
public static void computePoints(double x[], double y[], double xStart, double step) {
//first point
x[0] = xStart;
y[0] = f(xStart);
//remaining points
for (int i=1; i<=x.length-1; i++) {
x[i] = x[i-1] + step;
y[i] = f(x[i]);
}
}
} //end NewtonsMethodGraph
```

Fetching token