% % Math 451 9/24/12 class % % Today we went over Prob 4.4 from Driscoll on Newton's method % and tried 3 ways to stop the iterations; see newton, newton2, % newton3_tdelillo posted on my webpage % % Q: We just tried solving f(x) = x^2 -2 =0, but what happens if you % use Newton for f(x) = x^2 =0? % % created function newton_tdelilllo home f = @(x) x^2 - 2; fprime = @(x) 2*x; x0=1; [x1,f1]=newton(f,fprime,x0,tol) {??? Undefined function or variable 'tol'. } tol=e-4 {??? Undefined function or variable 'e'. } tol=1e-4 tol = 1.000000000000000e-04 home [x1,f1]=newton(f,fprime,x0,tol) {??? Undefined function or method 'newton' for input arguments of type 'function_handle'. } [x1,f1]=newton_tdelillo(f,fprime,x0,tol) x1 = 1.414213562373095 f1 = 4.440892098500626e-16 sqrt(2) ans = 1.414213562373095 uiopen('/home/user33/Documents/MATLAB/451_delillo/newton2_tdelillo.m', true); clear home f = @(x) x^2 - 2; fprime = @(x) 2*x; x0=1; tol=1e-6; [x1,f1]=newton2_tdelillo(f,fprime,x0,tol) x1 = 1.414213562373095 f1 = 4.440892098500626e-16 sqrt(2) ans = 1.414213562373095 x0=1 x0 = 1 home [x1,f1]=newton2_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x = 1.414215686274510 errx = 0.002450980392157 x = 1.414213562374690 errx = 2.123899820016817e-06 x = 1.414213562373095 errx = 1.594724352571575e-12 x1 = 1.414213562373095 f1 = 4.440892098500626e-16 tol =1e-2 tol = 0.010000000000000 x0=1; home [x1,f1]=newton2_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x = 1.414215686274510 errx = 0.002450980392157 x1 = 1.414215686274510 f1 = 6.007304882871267e-06 sqrt(2) ans = 1.414213562373095 abs(x1-sqrt(2)) ans = 2.123901414741169e-06 uiopen('/home/user33/Documents/MATLAB/451_delillo/newton3_tdelillo.m', true); home x0=1; tol=1e-4; [x1,f1]=newton3_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x = 1.414215686274510 errx = 0.002450980392157 x1 = 1.414215686274510 f1 = 6.007304882871267e-06 tol=1e-1; home [x1,f1]=newton3_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x1 = 1.416666666666667 f1 = 0.006944444444445 home (2 > 1) ans = 1 (2 < 1) ans = 0 (2 < 1) & (1 < 3) ans = 0 (2 < 1) && (1 < 3) ans = 0 (2 < 1) | (1 < 3) ans = 1 (2 < 1) || (1 < 3) ans = 1 home tol=1e-6; x0=1; [x1,f1]=newton3_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x = 1.414215686274510 errx = 0.002450980392157 x1 = 1.414215686274510 f1 = 6.007304882871267e-06 home [x1,f1]=newton3_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x1 = 1.416666666666667 f1 = 0.006944444444445 [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) x = 1.500000000000000 errx = 0.500000000000000 x = 1.416666666666667 errx = 0.083333333333333 x1 = 1.416666666666667 f1 = 0.006944444444445 n = 2 home f = @(x) x^2; fprime = @(x) 2*x; tol=1e-10; x0=1; [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) x1 = 7.629394531250000e-06 f1 = 5.820766091346741e-11 n = 17 f = @(x) x^2-2; home tol=1e-10; x0=1; [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) x1 = 1.414213562374690 f1 = 4.510614104447086e-12 n = 4 home x0=4; [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) x1 = 1.414213562373095 f1 = 4.440892098500626e-16 n = 6 home [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) errx = 1.750000000000000 errx = 0.680555555555556 errx = 0.147554080629302 errx = 0.007656077875069 errx = 2.072341514125497e-05 errx = 1.518369874276004e-10 x1 = 1.414213562373095 f1 = 4.440892098500626e-16 n = 6 f = @(x) x^2; home tol=1e-10; x0=1; [x1,f1,n]=newton3_tdelillo(f,fprime,x0,tol) errx = 0.500000000000000 errx = 0.250000000000000 errx = 0.125000000000000 errx = 0.062500000000000 errx = 0.031250000000000 errx = 0.015625000000000 errx = 0.007812500000000 errx = 0.003906250000000 errx = 0.001953125000000 errx = 9.765625000000000e-04 errx = 4.882812500000000e-04 errx = 2.441406250000000e-04 errx = 1.220703125000000e-04 errx = 6.103515625000000e-05 errx = 3.051757812500000e-05 errx = 1.525878906250000e-05 errx = 7.629394531250000e-06 x1 = 7.629394531250000e-06 f1 = 5.820766091346741e-11 n = 17 % what's happening? % when you try to solve f(x0=0 for f(x)=x^2? diary off