11-Nov-2013 1> % salvare newton.m 1> % (http://www.dm.unibo.it/~achilles/calc/Programmi_CS4a/newton.m) 1> % in una delle cartelle dove Octave/MATLAB cerca i propri m-file 1> help newton 'newton' is a function from the file C:\cygwin\home\achilles\programmi_matlab/Programmi_CS4a\newton.m NEWTON Trova uno zero di una funzione. ZERO=NEWTON(FUN,DFUN,X0,TOL,NMAX) approssima lo zero ZERO della funzione definita nella function FUN, continua e derivabile, usando il metodo di Newton e partendo da X0. FUN e la sua derivata DFUN accettano in ingresso uno scalare x e restituiscono un valore scalare. Se la ricerca dello zero fallisce, il programma restituisce un messaggio d'errore. FUN e DFUN possono essere inline function, anonymous function o function definite in M-file. ZERO=NEWTON(FUN,DFUN,X0,TOL,NMAX,P1,P2,...) passa i parametri P1,P2,... alle funzioni FUN(X,P1,P2,...) e DFUN(X,P1,P2,...). [ZERO,RES,NITER]= NEWTON(FUN,...) restituisce il valore del residuo RES in ZERO ed il numero di iterazioni NITER necessario per calcolare ZERO. Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc ' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help@octave.org mailing list. 2> f = inline('x^2-a','x','a') f = f(x, a) = x^2-a 3> df = inline('2*x') df = f(x) = 2*x 4> newton(f, df, 1, 1.e-6, 50, 2) ans = 1.4142 5> newton(f, df, 1, 1.e-6, 50, 3) ans = 1.7321 6> newton(f, df, 1, 1.e-6, 50, 16) ans = 4.0000 7> [alpha, residuo, iter] = newton(f, df, 1, 1.e-6, 50, 16) alpha = 4.0000 residuo = 4.0501e-013 iter = 6 8> [alpha, residuo, iter] = newton(f, df, 1, 1.e-12, 50, 16) alpha = 4 residuo = 0 iter = 7 9> [alpha, residuo, iter] = newton(f, df, 3, 1.e-12, 50, 16) alpha = 4 residuo = 0 iter = 5 10> format long 11> x = 1; 12> for k = 1:50 x = cos(x) end; x = 0.739085132739254 x = 0.739085133535737 x = 0.739085132999216 x = 0.739085133360623 x = 0.739085133117175 x = 0.739085133281165 x = 0.739085133170699 x = 0.739085133245110 x = 0.739085133194986 x = 0.739085133228750 x = 0.739085133206006 x = 0.739085133221327 x = 0.739085133211007 x = 0.739085133217959 x = 0.739085133213276 x = 0.739085133216430 x = 0.739085133214305 x = 0.739085133215737 x = 0.739085133214773 x = 0.739085133215422 x = 0.739085133214985 x = 0.739085133215279 x = 0.739085133215081 x = 0.739085133215215 x = 0.739085133215124 x = 0.739085133215185 x = 0.739085133215144 x = 0.739085133215172 x = 0.739085133215153 x = 0.739085133215166 x = 0.739085133215157 x = 0.739085133215163 x = 0.739085133215159 x = 0.739085133215162 x = 0.739085133215160 x = 0.739085133215161 x = 0.739085133215160 x = 0.739085133215161 x = 0.739085133215160 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 x = 0.739085133215161 13> cos(x)-x ans = 0 14> quit