{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Pre-Calc Exam Notes 135

# Pre-Calc Exam Notes 135 - × 10 − 50 That is our Fnal...

This preview shows page 1. Sign up to view the full content.

Numerical Methods in Trigonometry Section 6.2 135 Listing 6.1 Program listing for secant.java 1 import java.math. * ; 2 public class secant { 3 public static void main (String[] args) { 4 double x0 = Double.parseDouble(args[0]); 5 double x1 = Double.parseDouble(args[1]); 6 double x = 0; 7 double error = 1.0E-50; 8 for (int i=2; i <= 10; i++) { 9 if (Double.compare(Math.abs(x0 - x1),error) > 0) { 10 x = x1 - (x1 - x0) * f(x1)/(f(x1) - f(x0)); 11 x0 = x1; 12 x1 = x; 13 System.out.println("x" + i + " = " + x); 14 } else { 15 break; 16 } 17 } 18 MathContext mc = new MathContext(50); 19 BigDecimal answer = new BigDecimal(x,mc); 20 System.out.println("x = " + answer); 21 } 22 //Define the function f(x) 23 public static double f (double x) { 24 return Math.cos(x) - x; 25 } 26 } Lines 4-5 read in x 0 and x 1 as input parameters to the program. Line 6 initializes the variable that will eventually hold the solution. Line 7 sets the maximum error ǫ error to be 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: × 10 − 50 . That is, our Fnal answer will be within that (tiny!) amount of the real solution. Line 8 starts a loop of 9 iterations of the algorithm, i.e. it will create the successive ap-proximations x 2 , x 3 , ..., x 10 to the real solution, though in Line 9 we check to see if the two previous approximations differ by less than the maximum error. If they do, we stop (since this means we have an acceptable solution), otherwise we continue. Line 10 is the main step in the algorithm, creating x n from x n − 1 and x n − 2 . Lines 11-12 set the new values of x n − 2 and x n − 1 , respectively. Lines 18-20 set the number of decimal places to show in the Fnal answer to 50 (the default is 16) and then print the answer. Lines 23-24 give the deFnition of the function f ( x ) = cos x − x ....
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online