User:Subanark/Sandbox

Section 2.1

10. Possible roots are -2,-1,0,1,2. We will use bisection until only one root is in the inteveral.

10a.

f(-1.5) is negative

f(2.5) is positive

f(0.5) is positive

f(-0.5) is negative

Thus this will converge to 0

10b.

f(-0.5) is negative

f(2.4) is positive

f(1.95) is negative

This will then converge to 2.

10c.

f(-0.5) is negative

f(3) is positive

f(1.25) is negative

This will then converge to 2.

10d.

f(-3) is positive

f(-0.5) is negative

f(-1.75) is negative

Thus this will converge to -2

15.

$$ -log_2(10^{-4}) < 14 $$

Section 2.2

1.a

$$		g_1(x) = \left({3 + x - 2x^2}\right)^{1/4} $$

$$		x = \left({3 + x - 2x^2}\right)^{1/4} $$

$$		x^4 = 3 + x - 2x^2 $$

$$		x^4 + 2x^2 - x - 3 = 0 $$

1.b

$$		g_2(x) = \left(\frac{x + 3 - x^4}{2}\right)^{1/2} $$

$$		x = \left(\frac{x + 3 - x^4}{2}\right)^{1/2} $$

$$		x^2 = \frac{x + 3 - x^4}{2} $$

$$		2x^2 = x + 3 - x^4 $$

$$		x^4 + 2x^2 - x - 3 = 0 $$

1.c

$$	g_3(x) = \left(\frac{x + 3}{x^2 + 2}\right)^{1/2} $$

$$	x = \left(\frac{x + 3}{x^2 + 2}\right)^{1/2} $$

$$	x^2 = \frac{x + 3}{x^2 + 2} $$

$$	x^4 + 2x^2 = x +3 $$

$$	x^4 + 2x^2 - x - 3 = 0 $$

1.d

$$	g_4(x) = \frac{3x^4+2x^2+3}{4x^3+4x-1} $$

$$	x = \frac{3x^4+2x^2+3}{4x^3+4x-1} $$

$$	4x^4 + 4x^2 - x = 3x^4 + 2x^2 + 3 $$

$$	x^4 + 2x^2 - x - 3 = 0 $$

8.

$$ g(x) = 2^{-x} $$

g is a monotone function

$$g(x)$$ is in $$\left[\frac{1}{3},1\right]$$ since:

$$ g(1) = \frac{1}{2} $$ $$ g(\frac{1}{3}) \approx 0.7937005260 $$

thus g has a fixed point

$$ g'(x) = -2^{-x}ln(2) $$

$$g'$$ is a monotone function

$$ g'(\frac{1}{3}) \approx -0.5501512820 $$

$$ g'(1) \approx -0.3465735903 $$

thus $$k \leq 0.5501512820$$

We use $$\frac{2}{3}$$ as an approximation for the fixed point.

After n iterations we will be within $$\frac{1}{3}k^n$$ accarcacy.

To get within $$10^{-4}$$ accuacry:

$$ \frac{1}{3}0.5501512820^n \leq 10^{-4} $$

$$ n = log_{0.5501512820}(3 \cdot 10^{-4}) = 13.57470580 $$

So, 14 iterations is sufficient to get that accuarcy.

Actual was 9 iterations (see computer program output).

Section 2.3

18.

As seen in the computer output, the secant method spirals out of control, while the bisection method gets pretty close. The reason for this is that the function is quite variable in the range that is speficied. The slope of the line between the two points is nowhere close to the actual slope of the line for most of that range.

Section 2.1
2.a Guess: f(-2.0) = -22.5 Guess: f(1.5) = 3.75 Guess: f(-0.25) = 2.109375 Guess: f(-1.125) = -1.294921875 Solution: -0.6875 2.b Guess: f(-1.25) = -2.953125 Guess: f(2.5) = 31.5 Guess: f(0.625) = -0.228515625 Guess: f(1.5625) = 4.594482421875 Solution: 1.09375 6. a. guess: f(1.0) = 0.2817181715409549 guess: f(2.0) = -1.3890560989306495 guess: f(1.5) = 0.018310929661935482 guess: f(1.75) = -0.5046026760057298 guess: f(1.625) = -0.2034190371800806 guess: f(1.5625) = -0.08323318196760265 guess: f(1.53125) = -0.030203152782080522 guess: f(1.515625) = -0.005390403793883003 guess: f(1.5078125) = 0.006598106634198686 guess: f(1.51171875) = 6.384470894760241E-4 guess: f(1.513671875) = -0.002367312534214072 guess: f(1.5126953125) = -8.622683830337152E-4 guess: f(1.51220703125) = -1.1136982611414936E-4 guess: f(1.511962890625) = 2.636738038397368E-4 guess: f(1.5120849609375) = 7.618578602830439E-5 guess: f(1.51214599609375) = -1.7583570236290313E-5 guess: f(1.512115478515625) = 2.9303220283161124E-5 guess: f(1.5121307373046875) = 5.86035312810651E-6 guess: f(1.5121383666992188) = -5.861476526369813E-6 answer: 1.5121345520019531 b. guess: f(0.0) = 2.0 guess: f(1.0) = -0.0973749108546258 guess: f(0.5) = 1.48402641497099 guess: f(0.75) = 0.8280665900087878 guess: f(0.875) = 0.3991152805228779 guess: f(0.9375) = 0.15932576721450564 guess: f(0.96875) = 0.033091500364295534 guess: f(0.984375) = -0.03161237042456033 guess: f(0.9765625) = 8.718568434926688E-4 guess: f(0.98046875) = -0.015337178739274648 guess: f(0.978515625) = -0.007224392079361319 guess: f(0.9775390625) = -0.003174200480549061 guess: f(0.97705078125) = -0.0011506550441016117 guess: f(0.976806640625) = -1.392699079345583E-4 guess: f(0.9766845703125) = 3.66325765717157E-4 guess: f(0.97674560546875) = 1.1353600339480963E-4 guess: f(0.976776123046875) = -1.2864933641498766E-5 guess: f(0.9767608642578125) = 5.0336039533860344E-5 guess: f(0.9767684936523438) = 1.8735679110370995E-5 answer: 0.9767723083496094 c. guess: f(0.0) = 2.0 guess: f(1.0) = -0.0973749108546258 guess: f(0.5) = 1.48402641497099 guess: f(0.75) = 0.8280665900087878 guess: f(0.875) = 0.3991152805228779 guess: f(0.9375) = 0.15932576721450564 guess: f(0.96875) = 0.033091500364295534 guess: f(0.984375) = -0.03161237042456033 guess: f(0.9765625) = 8.718568434926688E-4 guess: f(0.98046875) = -0.015337178739274648 guess: f(0.978515625) = -0.007224392079361319 guess: f(0.9775390625) = -0.003174200480549061 guess: f(0.97705078125) = -0.0011506550441016117 guess: f(0.976806640625) = -1.392699079345583E-4 guess: f(0.9766845703125) = 3.66325765717157E-4 guess: f(0.97674560546875) = 1.1353600339480963E-4 guess: f(0.976776123046875) = -1.2864933641498766E-5 guess: f(0.9767608642578125) = 5.0336039533860344E-5 guess: f(0.9767684936523438) = 1.8735679110370995E-5 answer: 0.9767723083496094 d. guess: f(0.0) = 2.0 guess: f(1.0) = -0.0973749108546258 guess: f(0.5) = 1.48402641497099 guess: f(0.75) = 0.8280665900087878 guess: f(0.875) = 0.3991152805228779 guess: f(0.9375) = 0.15932576721450564 guess: f(0.96875) = 0.033091500364295534 guess: f(0.984375) = -0.03161237042456033 guess: f(0.9765625) = 8.718568434926688E-4 guess: f(0.98046875) = -0.015337178739274648 guess: f(0.978515625) = -0.007224392079361319 guess: f(0.9775390625) = -0.003174200480549061 guess: f(0.97705078125) = -0.0011506550441016117 guess: f(0.976806640625) = -1.392699079345583E-4 guess: f(0.9766845703125) = 3.66325765717157E-4 guess: f(0.97674560546875) = 1.1353600339480963E-4 guess: f(0.976776123046875) = -1.2864933641498766E-5 guess: f(0.9767608642578125) = 5.0336039533860344E-5 guess: f(0.9767684936523438) = 1.8735679110370995E-5 answer: 0.9767723083496094 10 iteration -1 is f(1.0) = -1.0 iteration 0 is f(2.0) = 5.0 iteration 1 is f(1.5) = 0.875 iteration 2 is f(1.25) = -0.296875 iteration 3 is f(1.375) = 0.224609375 iteration 4 is f(1.3125) = -0.051513671875 iteration 5 is f(1.34375) = 0.082611083984375 iteration 6 is f(1.328125) = 0.014575958251953125 iteration 7 is f(1.3203125) = -0.018710613250732422 iteration 8 is f(1.32421875) = -0.0021279454231262207 iteration 9 is f(1.326171875) = 0.006208829581737518 iteration 10 is f(1.3251953125) = 0.002036650665104389 iteration 11 is f(1.32470703125) = -4.659488331526518E-5 iteration 12 is f(1.324951171875) = 9.94790971162729E-4 iteration 13 is f(1.3248291015625) = 4.7403881944774184E-4 iteration 14 is f(1.32476806640625) = 2.1370716262936185E-4

Section 2.2
2.a a. Iteration 0: 1.0 Iteration 1: 1.189207115002721 Iteration 2: 1.0800577526675623 Iteration 3: 1.1496714305893827 b. Iteration 0: 1.0 Iteration 1: 1.224744871391589 Iteration 2: 0.9936661590774817 Iteration 3: 1.228568645274987 c. Iteration 0: 1.0 Iteration 1: 1.1547005383792515 Iteration 2: 1.116427409872122 Iteration 3: 1.1260522330022757 d. Iteration 0: 1.0 Iteration 1: 1.1428571428571428 Iteration 2: 1.1244816900178953 Iteration 3: 1.1241231639401488 6 with g(x) = (x + 1)^(1/3) g(1.0) = 1.2599210498948732 g(1.2599210498948732) = 1.3122938366832888 g(1.3122938366832888) = 1.3223538191388249 g(1.3223538191388249) = 1.324268744551578 8 g(0.6666666666666666) = 0.6299605249474366 g(0.6299605249474366) = 0.6461940962535336 g(0.6461940962535336) = 0.6389637113846262 g(0.6389637113846262) = 0.6421740571213054 g(0.6421740571213054) = 0.6407466531251755 g(0.6407466531251755) = 0.6413809222615807 g(0.6413809222615807) = 0.6410990063331932 g(0.6410990063331932) = 0.6412242952378678 g(0.6412242952378678) = 0.6411686113968689 10 g(x) = 1/2(x + 25/x^2) g(2.5) = 3.25 g(3.25) = 2.8084319526627217 g(2.8084319526627217) = 2.9890442187463804 g(2.9890442187463804) = 2.8936110810306244 g(2.8936110810306244) = 2.9397020246523495 g(2.9397020246523495) = 2.916300893074756 g(2.916300893074756) = 2.9279068171987497 g(2.9279068171987497) = 2.92208094396572 g(2.92208094396572) = 2.924988061361489 g(2.924988061361489) = 2.9235330594214997 g(2.9235330594214997) = 2.9242601981441343 g(2.9242601981441343) = 2.9238965384011113 g(2.9238965384011113) = 2.9240783456547135 g(2.9240783456547135) = 2.923987436376247 total iterations of fixed point:14 Bisection method took 14

Section 2.3
6.a initial point:1.5 iteration 1:1.9564897211242105 iteration 2:1.8415330610420608 iteration 3:1.8295060132036514 iteration 4:1.8293836144941664 iteration 5:1.829383601933849 6.d initial point:1.5 iteration 1:1.4067209351351013 iteration 2:1.4123699572511934 iteration 3:1.4123911717250053 iteration 4:1.4123911720238844 initial point:3.0 iteration 1:3.0591673732008657 iteration 2:3.0571060546915994 iteration 3:3.0571035499984363 8.a initial points:1.0,2.0 iteration 1:1.6783084847673808 8.a initial points:1.0,2.0 iteration 1:1.5906161091496414 initial points:2.718281828459045,4.0 iteration 1:2.918568324791443 18.a guess at 0.0 is -6.0 guess at 0.48 is 9.894544843865265 guess at 0.24 is -5.060937494182507 guess at 0.36 is -3.874891826842797 guess at 0.42 is -2.105257145070144 guess at 0.44999999999999996 is 0.3137515146750314 guess at 0.43499999999999994 is -1.171182647807246 guess at 0.44249999999999995 is -0.5245211508218706 guess at 0.4462499999999999 is -0.13434976287736955 guess at 0.44812499999999994 is 0.08167438867622234 guess at 0.44718749999999996 is -0.028237440581283302 guess at 0.44765625 is 0.0262307752703812 18.c initial points:0.0,0.48 iteration 1:0.18119424169051174 iteration 2:0.2861871658222898 iteration 3:1.0919861065027499 iteration 4:-3.6922966654011073 iteration 5:-22.60064985474053 iteration 6:-57.22283247260205 iteration 7:3.5387581457345476 iteration 8:-113.94440504807905 iteration 9:-195.89499482451663 iteration 10:-2989.9400375314453 iteration 11:103968.24147228828 iteration 12:817646.8229212957 20.a initial point:-100.0 iteration 1:-48.74543849889916 iteration 2:-21.596769093971908 iteration 3:-11.484219569105008 iteration 4:-2.488158340945553 iteration 5:-1.2099747956554148 iteration 6:-1.3854492522796145 iteration 7:-1.3793702695076215 iteration 8:-1.3793645942270283 20.b initial point:-50.0 iteration 1:-24.425485656917687 iteration 2:-10.51864735412305 iteration 3:-1.0369893209490453 iteration 4:-1.4126229614936676 iteration 5:-1.37952504038751 iteration 6:-1.3793645982150824 iteration 7:-1.3793645942220307 20.c initial point:-25.0 iteration 1:-12.363754727065167 iteration 2:-6.065457253768812 iteration 3:-3.3549550041503813 iteration 4:1.2240872555068933 iteration 5:1.3843533642486068 iteration 6:1.379368417659703 iteration 7:1.3793645942242991 20.d initial point:25.0 iteration 1:12.363754727065167 iteration 2:6.065457253768812 iteration 3:3.3549550041503813 iteration 4:-1.2240872555068933 iteration 5:-1.3843533642486068 iteration 6:-1.379368417659703 iteration 7:-1.3793645942242991 20.e initial point:50.0 iteration 1:24.425485656917687 iteration 2:10.51864735412305 iteration 3:1.0369893209490453 iteration 4:1.4126229614936676 iteration 5:1.37952504038751 iteration 6:1.3793645982150824 iteration 7:1.3793645942220307 20.f initial point:100.0 iteration 1:48.74543849889916 iteration 2:21.596769093971908 iteration 3:11.484219569105008 iteration 4:2.488158340945553 iteration 5:1.2099747956554148 iteration 6:1.3854492522796145 iteration 7:1.3793702695076215 iteration 8:1.3793645942270283

public static void bisect(double[] indexes, Function function) {		assert indexes.length == 2; double a = indexes[0]; double b = indexes[1]; double midpoint = midpoint(a,b); double f_midpoint = function.evaluate(midpoint); if(f_midpoint <= 0) indexes[0] = midpoint; if(f_midpoint >= 0) indexes[1] = midpoint; }	public static double bisect(double a, double b, double threshold, Function function, int maxIterations) {		double[] indexes = new double[2]; double f_a = function.evaluate(a); double f_b = function.evaluate(b); if(f_a * f_b > 0) if(f_a == f_b) return f_a; else return Double.NaN; if(f_a < 0) {			indexes[0] = a;			indexes[1] = b;		} else {			indexes[0] = b;			indexes[1] = a;		} for( int i = 0; abs(indexes[0]-indexes[1]) > threshold && i < maxIterations; i++) {			bisect(indexes,function); }		return midpoint(indexes[0],indexes[1]); }	public static double midpoint(double a, double b)	{ return abs((b - a)) / 2 + min(a,b); }	public static double fixedPoint(Function g_x, final double threshold, double p)	{ double p0; do {			p0 = p;			p = g_x.evaluate(p0); System.out.println("g("+p0+") = "+p); }while(abs(p-p0) > threshold); return p;	} public static double newton(double p0, double tolerance, int[] iterations, Function f, Function f1) {		int i = 0; try {			System.out.println("initial point:" + p0); for(i < iterations[0]; i++) {				double p = p0 - f.evaluate(p0)/f1.evaluate(p0); System.out.println("iteration " + (i + 1)+":"+p); if(abs(p - p0) < tolerance) {					return p;				} else {					p0 = p;				} }			return NaN; }		finally {			iterations[0] = i;		} }	public static double secant(double p0, double p1, double tolerance, Function f, int maxIterations) {		System.out.println("initial points:" + p0 +"," +p1); double q0 = f.evaluate(p0); double q1 = f.evaluate(p1); for(int i = 0; true ; i++) {			double p = p1 - q1 * (p1 - p0) / (q1 - q0); System.out.println("iteration " + (i + 1)+":"+p); if(abs(p - p1) < tolerance || i >= maxIterations + 1) return p;			else {				p0 = p1; q0 = q1; p1 = p;				q1 = f.evaluate(p); }		}	}