The book has two different MATLAB® versions of Newton\'s method. One is a progra
ID: 3794428 • Letter: T
Question
The book has two different MATLAB® versions of Newton's method. One is a program (on page 162) and the other is a function (page 170). The comments on page 162 indicate the algorithm does not work on zero roots (i.e., it does not work when the true root is equal to zero). This is the case because of the form of "myrel".
Create a variation on the function m-file (page 170) in which the zero-root restriction is removed by changing the definition of "myrel." Specifically, do not divide by x; but leave the rest the same.
Then test your modified code on the MATLAB® pre-defined "sin" function (and remember your differential calculus: the derivative of sine is simply cosine).
Set the initial guess to pi/4. [You can still name the function "newtfun_LastName.m"].
[x, conv] Uses Newton's method to solve f (x) 0 fh is handle to f(x), dfh is handle to f (x) Initial guess is xo Returns final value of x, f(x), and conv (1 = convergence, 0 = divergence) newtfun (fh, xo) function % NEWTON f, dfh, % iteration counter steps = 0; x = x0 ; re = 1e-8 ; myre1 = 1 ; % required relative error while myre1 > re (stepsExplanation / Answer
package com.java2novice.algos;
public class MyBinarySearch {
public int binarySearch(int[] inputArr, int key) {
int start = 0;
int end = inputArr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == inputArr[mid]) {
return mid;
}
if (key < inputArr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
MyBinarySearch mbs = new MyBinarySearch();
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16};
System.out.println("Key 14's position: "+mbs.binarySearch(arr, 14));
int[] arr1 = {6,34,78,123,432,900};
System.out.println("Key 432's position: "+mbs.binarySearch(arr1, 432));
}
}
-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.