Math 111: MATLAB Assignment 3

DUE DATE:  DECEMBER 10, 2012 (EXTRA CREDIT)

 

 

Your Assignment

In this assignment, we will be investigating the non-zero solutions to sin x = x3 using Newton's Method..

  1. Plot the functions on the same graph on the interval [0, 2] and notice that there are exactly two solutions, one of which is trivial, on this interval . (Your code from the second matlab assignment may be helpful here).

  2. Find a function f(x) where the roots of f(x) will be exactly at the x values where the curves in part one intersect. What does the fact that f(x) is odd tell you about the total number of roots?

  3. Revise the code below to use the function above with its derivative and consider the convergence of Newton's Method for the following initial guesses x0: Note which solution the method converges to and how fast.

  4. Provide the plot (on reasonable intervals in x and y) and relevant printout (that is the chart the code below makes) for each case and briefly discuss the convergence for each case. Which solution does the method converge to (if it does at all) in each case and how fast?

 

 The following MATLAB code should be quite useful for the assignment.

 

% MATLAB assignment 3 - Newton's method

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% First, define the function we wish to find a zero for

f = @(x) x.^2;       % = x^2

 

% Next define the derivative df/dx. Usually this would be

% estimated by picking a small number dx and using

%           df/dx ~ [f(x+dx) - f(x)]/dx

%                   OR

%           df/dx ~ [f(x+dx) - f(x-dx)]/(2dx)

% You can just enter the formula for df/dx.

dfdx = @(x) 2*x;

 

% set the maximum number of iterations of the method to use

max_iteration_count = 20;

 

% set aside space for Xn, f(Xn), and f'(Xn)

Xn   = zeros(1,max_iteration_count + 1); % The '+1' is for X0

FXn  = zeros(1,max_iteration_count);

dFXn = zeros(1,max_iteration_count);

 

% set the initial estimate X0

Xn(1) = -0.5;

 

% stopping criteria - if Xn changes by less than DX, then stop iterations

% we are assuming that this indicates convergence of the Xn

DX = 0.0001;

 

% for graphing

plot_title = 'student name x0=???'; % put your name at the top of the graph

my_xlim = [-2,2];           % how much should we show on the x axis?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% you should not need to modify anything after this point

% you should, however, look through the code to see what it does

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

% iterate through Newton's method

% note that because 'i' represents the square root of -1, it

% should never be used as a variable in your MATLAB programs

plot_to = max_iteration_count; % there is a chance that Newtons method will fail

                           % at some point, if so, we do not need the extra

                           % data in our plot or table

for ind = 1:max_iteration_count

    FXn(ind)    = f(Xn(ind)); % f(Xn)

    dFXn(ind)   = dfdx(Xn(ind)); % df/dx(Xn)

    if abs(dFXn) < eps

        % Newton's method fails if df/dx(Xn)=0. 'eps' is just a very small

        % number to see if the derivative is about zero

        plot_to = ind-1;

        fprintf('The derivative vanishes at iteration %1i.\n Terminating the procedure.\n',ind);

        break;

    else

        % if  df/dx(Xn) is not zero, continue with the method

        Xn(ind + 1) = Xn(ind) - FXn(ind)/dFXn(ind); %X(n+1) = Xn - f(Xn)/df/dx(Xn)

        % check stopping criteria

        if abs( Xn(ind+1) - Xn(ind) ) < DX

            plot_to = ind;

            break;

        end

    end

end

 

% Print a table with  Xn, f(Xn), and f'(Xn)

fprintf(' n       Xn       f(Xn)     df/dx(Xn)\n')

fprintf('---  ---------  --------  ----------\n')

for ind = 1:plot_to

fprintf(' %1i    %1.4f    %1.4f    %1.4f\n',ind-1,Xn(ind),FXn(ind),dFXn(ind));

end

fprintf(' %1i    %1.4f     --         --\n',plot_to,Xn(plot_to+1));

 

 

 

% create data for plotting the function

x = linspace(my_xlim(1),my_xlim(2),200);

y = f(x);

% open a new plot

figure;

% plot the function

plot(x,y,'k');

% plot the steps of Newtons method starting with blue and transitioning

% to red

if plot_to > 0

    hold on;

    for ind = 1:plot_to

       plot([Xn(ind),Xn(ind),Xn(ind+1)],[0,f(Xn(ind)),0],'-o','Color',[ind/plot_to,0,1-ind/plot_to],'MarkerSize',4);

    end

    hold off;

else

     plot(Xn(1),f(Xn(1)),'or');

end

grid on;

xlim(my_xlim);

% add descriptors in a legible font

set(gca,'FontSize',13)

title(plot_title);

xlabel('x');

ylabel('y');

 

 

 

 

 

Last update: 10/22/10