]> Examples

Examples

  1. Find the roots of the function y= x 2 +5 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqr1ngBPrgifHhDYfgasaacH8YjY=vipgYlh9vqqj=hEeeu0xXdbba9frFj0=OqFfea0dXdd9vqai=hGuQ8kuc9pgc9q8qqaq=dir=f0=yqaiVgFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEaiabg2da9iabgkHiTiaadIhadaahaaWcbeqaaiaaikdaaaGccqGHRaWkcaaI1aaaaa@3E19@

    First, plot the function. The range -3 to 3 will show the roots we are interested in.

    upside down parabola with max at (0,5) and roots near -2 and 2

    Place this code in a new m-file and run to see the plot shown here.

    % Find and plot the roots of y=-x^2+5 from -3 to 3
    xx = -3:0.1:3;
    yy = -xx.^2 + 5;  % element-wise for vector use
    plot([-3 3],[0 0],'-.k', xx,yy,'r-'); 
    
  2. Use fzero to find the roots of the function in Example 1. Place this code in the same m-file as you created for Example 1:

    % Find the roots of the function y=-x^2+5 and save them in vectors x and y
    x0 = [-2 2];    % initial guesses for the roots
    
    for n = 1:length(x0)
        x(n) = fzero('-x^2+5',x0(n));  % find the root and save it in x
        y(n) = -x(n)^2 + 5;            % evaluate at that root
    end
    
    % Plot the function and the roots
    hold on
    plot( [-3 3],[0 0],'-.k', xx,yy,'r-' ); % plot y=0 and the curve
    
    % Display each root and a label for it on the plot
    for n = 1:length(x)
        plot( x(n), y(n), 'bo' );
        text( x(n)-0.5 , y(n)+0.5 , ...
            ['(' num2str(x(n),3) ',' num2str(y(n),1) ')' ] ); 
    end
    hold off
    

    Here is the figure that is produced, showing the curve and its roots.

    upside down parabola with max at (0,5) and roots at (-2.24,0) and (2.24,0)
  3. Find the roots of the function y= 3 2 x 3 6x+15 MathType@MTEF@5@5@+=feaafiart1ev1aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLnhiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqr1ngBPrgifHhDYfgasaacH8YjY=vipgYlh9vqqj=hEeeu0xXdbba9frFj0=OqFfea0dXdd9vqai=hGuQ8kuc9pgc9q8qqaq=dir=f0=yqaiVgFr0xfr=xfr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaamyEaiabg2da9maalaaabaGaaG4maaqaaiaaikdaaaGaamiEamaaCaaaleqabaGaaG4maaaakiabgkHiTiaaiAdacaWG4bGaey4kaSIaaGymaiaaiwdaaaa@421B@ A plot of the function is shown for your convenience.

    cubic polynomial from -3 to 3 with root near -3

    This command will find the root near -3

        >> fzero( '3/2*x.^3-6*x+15', -3 );