Math 111: MATLAB Assignment 1

DUE DATE:  November 19, 2012 for Math

 

 

In this assignment, we will learn to plot functions and investigate the limit behavior of some functions and their tangent lines at the limit point.

Here are some links to general information to help you get started in MATLAB. To begin, click on the Matlab icon on your desktop to open a Matlab screen. Click on File -> New -> m-file to create a new file. 

You can find more information to help you get started with MATLAB at Matlab Documentation.

 

 

Your Assignment

 

 

In this assignment, we will learn to plot functions and investigate the limit behavior of some functions and their tangent lines at the limit point.

The sample code given below considers the function y=sin(x)/x and investigates the shape of the curve. We know from class that the function has a removable discontinuity at x=0.  Thus, there cannot be a tangent line there. However, we can find the limit of the function as x->0. We can then take the slope of the function nearby to the left and right and average these to get an idea of what the tangent line to the curve would look like if the discontinuity were removed.

Try copying and running the code below. Then do the following assignment.

  1. Revise the code to consider the function f(x) = (e^x -1) / x. What is the limit at x-> 0? (You will need to figure this out by considering the limit as x-> of (e^x - e^0)/(x-0) to be a derivative in disguise). Find the derivative of the function. Do as we did for y=sin(x)/x above but for this function. That is, plot the curve on a reasonable interval around x=0 (also give reasonable limits for y). Find the slope of a "tangent-like" line by considering the derivative at values of x slightly less than and slightly greater than 0. Plot the "tangent-like" line on the same graph. Put your name in the title of the plot. Hand in your handwritten work for the limit and for the derivative of the function and the plot and the table as in the worked example. What is the slope of the curve at x=0 when the discontinuity is removed?

  2. Do the same as in part 1 but for the function f(x) = (sqrt(x)-3) / (x-9)  as x-> 9.

 

Label the plots with your name in addition to the particular curve (e.g. y=sin(x)/x for each case.)

 

Matlab note: The '*' sign will not, however, multiply two arrays together. If we want to multiply two arrays together, or divide by an array we need to use the matlab commands '.*' and './' rather than '*' or '/'. Similarly, to raise an array to a power, we need to use the '.^' command. Finally, use the matlab command 'sqrt(x)' to take the square root of x.

 

The following MATLAB code may be used as the basis for your work.

You need not hand in your codes -- just the plots and answers to the questions.

% The percent sign indicates a comment through the rest of the line

 %Helpful tip - The best way to learn MATLAB is to use the help menu. Hit F1 to start up help.

%Each command can be explained in there.

x = -6.29:0.01:6.29;         %This command makes a vector from -6.29 to 6.29 (-2pi to 2pi) with increments of 0.01.

%The semi-colon suppresses the output of this command from showing up in the command window.
% You can see what each command is doing if you remove the semi-colon from that line

y = sin(x) ./  x; %This command defines the function y = sin(x)/x. Note the period before the division sign.

% This is matlab syntax for dividing vector quantities term by term..

figure(1);

plot(x,y,'black','LineWidth',2); %Plot the function.

hold on; %Wait for more commands. Don't erase plot

grid on; %Display the grid.

F = @(x) (x * cos(x) - sin(x))/(x*x); %Create a function, you could have defined our function sin(x)/x this way.

% This is the derivative of sin(x)/x

Y2 = F(0.01); % This gives the derivative at x=0.01

Y1 = F(-0.01); % This gives the derivative at x=-0.01

slope = 0.5 * (Y1 + Y2); % Take the average of these slopes

plot(x,(slope * ( x - 0.0) + 1.0),'red','LineWidth',1);

 /* plots the "tangent-like" line through x=0, y=1 using slope just computed above

hold on;


plot(0,1,'ok','MarkerFaceColor','black','MarkerSize',5); %Mark the point (0, 1) -- that is we fill in the removable discontinuity

title('y = sin x / x','Fontsize',16); %Display the title y = sin(x)/x.

xlabel('x','Fontsize',14); %Display label for x-axis.

ylabel('y','Fontsize',14); %Display label for y-axis.

xlim([-6.29 6.29]); % plotting limits in x direction.

ylim([-0.5,1.2]);  % plotting limits in y direction

hold off;

% print a table with x and slope

fprintf(' x slope \n')

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

fprintf(' %4i %12.8f\n',-0.02,F(-0.02))

fprintf(' %4i %12.8f\n',-0.01,F(-0.01))

fprintf(' %4i %12.8f\n',0.0,F(0.0))

fprintf(' %4i %12.8f\n',0.01,F(0.01))

fprintf(' %4i %12.8f\n',0.02,F(0.02))



 

 

 

Last update: 10/27/2012