octave:2> F = @(x) [x(1) ^ 2 + x(2) ^ 2 - 1; sin(pi * x(1) / 2) + x(2) ^ 3]; octave:3> F([1,2]) ans = 4 9 octave:4> F([1,2]') ans = 4 9 octave:5> JF=@(x) [2*x(1),2*x(2);cos(pi*x(1)/2)*pi/2,3*x(2)^2]; octave:6> JF([4;9]) ans = 8.0000 18.0000 1.5708 243.0000 octave:7> x0=[1;1] x0 = 1 1 octave:8> tol=[1e-8,1e-8] tol = 1.0000e-08 1.0000e-08 octave:9> [x,delta,its]=newton(F,JF,x0,tol,9) x = 0.47610 -0.87939 delta = 7.0069e-12 its = 9 octave:10> [x,delta,its]=newton(F,JF,x0,tol,9) ans = 2.4144 ans = 1.1626 ans = 0.45504 ans = 0.20920 ans = 0.042336 ans = 0.0015990 ans = 2.7104e-06 ans = 7.0069e-12 x = 0.47610 -0.87939 delta = 7.0069e-12 its = 9 octave:11> JF=@(x) [2*x(1),2*x(2);cos(pi*x(1)/2),3*x(2)^2]; octave:12> [x,delta,its]=newton(F,JF,x0,tol,9) ans = 2.5912 ans = 1.1882 ans = 0.46950 ans = 0.37695 ans = 0.10931 ans = 0.017807 ans = 0.0037146 ans = 7.7675e-04 warning: newton: tolerance not met warning: called from newton at line 39 column 3 x = 0.47623 -0.87932 delta = 7.7675e-04 its = 9 octave:13> [x,delta,its]=newton(F,JF,x0,tol,12) ans = 2.5912 ans = 1.1882 ans = 0.46950 ans = 0.37695 ans = 0.10931 ans = 0.017807 ans = 0.0037146 ans = 7.7675e-04 ans = 1.6358e-04 ans = 3.4406e-05 ans = 7.2386e-06 warning: newton: tolerance not met warning: called from newton at line 39 column 3 x = 0.47609 -0.87939 delta = 7.2386e-06 its = 12 octave:14> [x,delta,its]=newton(F,JF,x0,tol,120) ans = 2.5912 ans = 1.1882 ans = 0.46950 ans = 0.37695 ans = 0.10931 ans = 0.017807 ans = 0.0037146 ans = 7.7675e-04 ans = 1.6358e-04 ans = 3.4406e-05 ans = 7.2386e-06 ans = 1.5228e-06 ans = 3.2037e-07 ans = 6.7399e-08 ans = 1.4179e-08 x = 0.47610 -0.87939 delta = 1.4179e-08 its = 16 octave:15> [x,delta,its]=newton(F,JF,x0,tol,20) ans = 2.5912 ans = 1.1882 ans = 0.46950 ans = 0.37695 ans = 0.10931 ans = 0.017807 ans = 0.0037146 ans = 7.7675e-04 ans = 1.6358e-04 ans = 3.4406e-05 ans = 7.2386e-06 ans = 1.5228e-06 ans = 3.2037e-07 ans = 6.7399e-08 ans = 1.4179e-08 x = 0.47610 -0.87939 delta = 1.4179e-08 its = 16 octave:16> [x,delta,its]=newton(F,JF,x0,tol,20) x = 0.47610 -0.87939 delta = 1.4179e-08 its = 16 octave:17> epsilon=1e-4 epsilon = 1.0000e-04 octave:18> x=randn(2,1) x = 0.41279 1.05440 octave:19> y=randn(2,1) y = -1.40135 -0.51575 octave:20> JF(x)*y ans = -2.2446 -2.8371 octave:21> (F(x+epsilon*y)-F(x))/epsilon ans = -2.2443 -3.4747 octave:22> JF=@(x) [2*x(1),2*x(2);cos(pi*x(1)/2)*pi/2,3*x(2)^2]; octave:23> JF(x)*y ans = -2.2446 -3.4747 octave:24> f=@(x) exp(x); octave:25> epsilon=1e-4;(f(0+epsilon)-f(0))/epsilon ans = 1.0001 octave:26> format long e octave:27> epsilon=1e-4;(f(0+epsilon)-f(0))/epsilon ans = 1.00005000166714e+00 octave:28> epsilon=1e-6;(f(0+epsilon)-f(0))/epsilon ans = 1.00000049996218e+00 octave:29> epsilon=1e-12;(f(0+epsilon)-f(0))/epsilon ans = 1.00008890058234e+00 octave:30> epsilon=1e-14;(f(0+epsilon)-f(0))/epsilon ans = 9.99200722162641e-01 octave:31> epsilon=1e-4;imag(f(0+1i*epsilon)/epsilon) ans = 9.99999998333333e-01 octave:32> epsilon=1e-6;imag(f(0+1i*epsilon)/epsilon) ans = 9.99999999999833e-01 octave:33> epsilon=1e-8;imag(f(0+1i*epsilon)/epsilon) ans = 1.00000000000000e+00 octave:34> epsilon=1e-14;imag(f(0+1i*epsilon)/epsilon) ans = 1.00000000000000e+00 octave:35> F=@(x)[x(1)^2+x(2)^2+x(3)^2-1;x(3)-1/2;x(1)+x(2)-1]; octave:36> F([1;2;3]) ans = 1.30000000000000e+01 2.50000000000000e+00 2.00000000000000e+00 octave:37> format octave:38> JF=@(x) [2*x(1),2*x(2),2*x(3);0,0,1;1,1,0]; octave:39> JF([1;2;3]) ans = 2 4 6 0 0 1 1 1 0 octave:40> x0=[1/2;1/2;1/2] x0 = 0.50000 0.50000 0.50000 octave:41> norm(x0)^2 ans = 0.75000 octave:42> newton(F,JF,x0) warning: matrix singular to machine precision warning: called from newton at line 27 column 7 warning: matrix singular to machine precision warning: called from newton at line 32 column 9 warning: matrix singular to machine precision warning: called from newton at line 32 column 9 warning: matrix singular to machine precision warning: called from newton at line 32 column 9 warning: matrix singular to machine precision warning: called from newton at line 32 column 9 warning: matrix singular to machine precision warning: called from newton at line 32 column 9 ans = 0.53973 0.53973 0.58632 octave:43> JF(x0) ans = 1 1 1 0 0 1 1 1 0 octave:44> x0=[0.4;0.6;1/2] x0 = 0.40000 0.60000 0.50000 octave:45> newton(F,JF,x0) ans = 0.14645 0.85355 0.50000 octave:46> x=newton(F,JF,x0) x = 0.14645 0.85355 0.50000 octave:47> x(1)+x(2) ans = 1 octave:48> norm(x)^2 ans = 1.00000 octave:49> F(x) ans = 2.2204e-16 0.0000e+00 0.0000e+00 octave:50> % da non fare octave:50> f=@(x) [x(1)^3-x(2)^2+exp(x(1))*x(2)]; octave:51> jf = @(x) [3*x(1)^2+exp(x(1))*x(2),-2*x(2)+exp(x(1))]; octave:52> x0=[-0.8;0.8] x0 = -0.80000 0.80000 octave:53> newton(f,jf,x0) ans = -0.38700 0.57898 octave:54> f(ans) ans = -9.9848e-13 octave:55> % fine delle cose da non fare octave:55> diary off