x = linspace(0,10,100);
fun1 = @(x) tan(x);
fun2 = @(x) (2./pi).*x;
y1 = fun1(x);
y2 = fun2(x);
plot(x,y1,x,y2)
x = linspace(0,100,1000);
y1 = fun1(x);
y2 = fun2(x);
close all
plot(x,y1,x,y2)
x = linspace(0,10,100);
y1 = fun1(x);
y2 = fun2(x);
close all
plot(x,y1,x,y2)



clear


fun1 = @(x) exp(-pi.*x)

fun1 =

  <a href="matlab:helpPopup function_handle" style="font-weight:bold">function_handle</a> with value:

    @(x)exp(-pi.*x)

fun2 = @(x) (1-x)./(1+x)

fun2 =

  <a href="matlab:helpPopup function_handle" style="font-weight:bold">function_handle</a> with value:

    @(x)(1-x)./(1+x)

x = linspace(0,10,100);
y1 = fun1(x);
y2 = fun2(x);
plot(x,y1,x,y2)
x = linspace(0,2,100);














y1 = fun1(x);

















y2 = fun2(x);















plot(x,y1,x,y2)
fun3 = @(x) exp(-pi.*x) - (1-x)./(1+x);
fun3(0.9)

ans =

    0.0065

fzero(fun3,0.9)

ans =

    0.8823

x_true = ans

x_true =

    0.8823

err = abs(0.9-x_true)/abs(x_true)

err =

    0.0201












grid
plot(x,y1,x,y2)
grid
clear
clc


help newton
newton is a script.

clear
newton

i =

     1


i =

     2


i =

     3


i =

     4


L'approssimazione della x e' 1.345474
Errore = 2.553818e-07




newton

i =

     1


i =

     2


i =

     3


i =

     4


L'approssimazione della x e' 1.345474
Errore = 2.553818e-07









clear
clc
close all
newton

L'approssimazione della x e' NaN in 2 iterazioni
Errore = NaNnewton
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('newton')" style="font-weight:bold">newton</a>
Denominatore nullo! Cambia punto iniziale
} 




newton
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('newton')" style="font-weight:bold">newton</a>
Denominatore nullo! Cambia punto iniziale
} 
prova
{Unrecognized function or variable 'prova'.
} 
clear
clc
help fzero
 <strong>fzero</strong>  Single-variable nonlinear zero finding. 
    X = <strong>fzero</strong>(FUN,X0) tries to find a zero of the function FUN near X0, 
    if X0 is a scalar.  It first finds an interval containing X0 where the 
    function values of the interval endpoints differ in sign, then searches 
    that interval for a zero.  FUN is a function handle.  FUN accepts real 
    scalar input X and returns a real scalar function value F, evaluated 
    at X. The value X returned by <strong>fzero</strong> is near a point where FUN changes 
    sign (if FUN is continuous), or NaN if the search fails.  
 
    X = <strong>fzero</strong>(FUN,X0), where X0 is a vector of length 2, assumes X0 is a 
    finite interval where the sign of FUN(X0(1)) differs from the sign of 
    FUN(X0(2)). An error occurs if this is not true.  Calling <strong>fzero</strong> with a
    finite interval guarantees <strong>fzero</strong> will return a value near a point where
    FUN changes sign.
 
    X = <strong>fzero</strong>(FUN,X0), where X0 is a scalar value, uses X0 as a starting 
    guess. <strong>fzero</strong> looks for an interval containing a sign change for FUN and 
    containing X0.  If no such interval is found, NaN is returned.  
    In this case, the search terminates when the search interval 
    is expanded until an Inf, NaN, or complex value is found. Note: if
    the option FunValCheck is 'on', then an error will occur if an NaN or 
    complex value is found.
 
    X = <strong>fzero</strong>(FUN,X0,OPTIONS) solves the equation with the default optimization
    parameters replaced by values in the structure OPTIONS, an argument
    created with the OPTIMSET function.  See OPTIMSET for details.  Used
    options are Display, TolX, FunValCheck, OutputFcn, and PlotFcns. 
 
    X = <strong>fzero</strong>(PROBLEM) finds the zero of a function defined in PROBLEM. 
    PROBLEM is a structure with the function FUN in PROBLEM.objective, 
    the start point in PROBLEM.x0, the options structure in PROBLEM.options,
    and solver name 'fzero' in PROBLEM.solver. 
 
    [X,FVAL]= <strong>fzero</strong>(FUN,...) returns the value of the function described 
    in FUN, at X.
 
    [X,FVAL,EXITFLAG] = <strong>fzero</strong>(...) returns an EXITFLAG that describes the
    exit condition. Possible values of EXITFLAG and the corresponding exit
    conditions are
 
      1  <strong>fzero</strong> found a zero X.
     -1  Algorithm terminated by output function.
     -3  NaN or Inf function value encountered during search for an interval
          containing a sign change.
     -4  Complex function value encountered during search for an interval 
          containing a sign change.
     -5  <strong>fzero</strong> may have converged to a singular point.
     -6  <strong>fzero</strong> can not detect a change in sign of the function.
 
    [X,FVAL,EXITFLAG,OUTPUT] = <strong>fzero</strong>(...) returns a structure OUTPUT
    with the number of function evaluations in OUTPUT.funcCount, the
    algorithm name in OUTPUT.algorithm, the number of iterations to
    find an interval (if needed) in OUTPUT.intervaliterations, the
    number of zero-finding iterations in OUTPUT.iterations, and the
    exit message in OUTPUT.message.
 
    Examples
      FUN can be specified using @:
         X = fzero(@sin,3)
      returns pi.
         X = fzero(@sin,3,optimset('Display','iter')) 
      returns pi, uses the default tolerance and displays iteration information.
 
      FUN can be an anonymous function:
         X = fzero(@(x) sin(3*x),2)
 
      FUN can be a parameterized function.  Use an anonymous function to
      capture the problem-dependent parameters:
         myfun = @(x,c) cos(c*x);  % The parameterized function.
         c = 2;                    % The parameter.
         X = fzero(@(x) myfun(x,c),0.1)
    
    Limitations
         X = fzero(@(x) abs(x)+1, 1) 
      returns NaN since this function does not change sign anywhere on the 
      real axis (and does not have a zero as well).
         X = fzero(@tan,2)
      returns X near 1.5708 because the discontinuity of this function near the 
      point X gives the appearance (numerically) that the function changes sign at X.
 
    See also <a href="matlab:help roots -displayBanner">roots</a>, <a href="matlab:help fminbnd -displayBanner">fminbnd</a>, <a href="matlab:help function_handle -displayBanner">function_handle</a>.

    <a href="matlab:doc fzero">Documentation for fzero</a>

clear
clc
close all




f = @(x) cos(4.*x) - 2.*x.^2 + 3;
fd = @(x) -4*sin(4*x) - 4*x;
x0 = 1;
tol = 1e-5;
i_max = 100;
[x_kp1, i, err] = newton_fun(f,fd,x0,tol,i_max)

x_kp1 =

    1.3455


i =

     4


err =

   2.5538e-07

val_fd
{Unrecognized function or variable 'val_fd'.
} 
newton

L'approssimazione della x e' 1.345474 in 4 iterazioni
Errore = 2.553818e-07\
 \
 ↑
{Invalid use of operator.
} 









[x_kp1, i, err] = newton_fun(f,fd,x0)
{Not enough input arguments.

Error in <a href="matlab:matlab.internal.language.introspective.errorDocCallback('newton_fun', '/Users/kate/work/did/LabCalcNum/22_23/lezioni/lezione4/newton_fun.m', 8)" style="font-weight:bold">newton_fun</a> (<a href="matlab: opentoline('/Users/kate/work/did/LabCalcNum/22_23/lezioni/lezione4/newton_fun.m',8,0)">line 8</a>)
while (err > tol) && (i < i_max)
} 
[x_kp1, i, err] = newton_fun(f,fd,x0)

x_kp1 =

    1.3455


i =

     4


err =

   2.5538e-07

[x_kp1, i, err] = newton_fun(f,fd,x0,[],200)

x_kp1 =

    1.3455


i =

     4


err =

   2.5538e-07

[x_kp1, i, err] = newton_fun(f,fd,x0,[],2)

x_kp1 =

    1.3458


i =

     2


err =

    0.0075

[x_kp1, i, err] = newton_fun(f,fd,x0,1e-8,[])

x_kp1 =

    1.3455


i =

     5


err =

   2.7230e-13





test_newton

x_kp1 =

    1.3455


i =

     4


err =

   2.5538e-07


L'approssimazione della x e' 1.345474 in 4 iterazioni
Errore = 2.553818e-07