Previous Chapter | Return to all notes | Next Chapter |
This chapter covers how to perform many mathematics that arises from Calculus using a CAS like Matlab. As we will see, a CAS will often do limits and derivatives quite easily, allowing us to investigate the geometry of the derivative using a plot or a table of values, which are often helpful for gaining intuition about limits and derivatives.
Limits
You may need to review the basics of a limit from Calculus. If we say
And from the plot it appears that the limit is 1, however, you can never say precisely with a plot, but this can help verify a result.
Another way to attack this is with a table. Let’s find the value of the function arrayfun
function. If
syms x
f(x) = x^2
xvals=[0.9,0.99,0.999,1.001,1.01,1.1]
yvals=double(arrayfun(@(x) f(x),xvals))
will produce the function values of all of the
[.81, .9801, .998001, 1.002001, 1.0201, 1.21]
Note: to find yvals
must have a double
as the outer function to switch back to floating point numbers.
We can make the this look a bit more like a table with
transpose([xvals;yvals])
it will look like:
As you can now see from the table above, as
Exercise
Reproduce the table above using
Using Matlab’s built-in Limit function
Now, of course, Matlab (and any CAS) will do the limit for us. If we enter
limit(f(x),x,1)
then Matlab returns 1.
Exercise: Limits
Try evaluating the following limits:
One-Sided Limits
Recall that a one-sided limit is a limit as
and we can plot this (nicely) with
fplot(x+1,[-3,0],'blue')
hold on
fplot(x^2,[0,3],'blue')
hold off
set(gca, 'XAxisLocation', 'origin', 'YAxisLocation', 'origin')
which results in:
Looking at the plot, the left- and right-handed limits appear to be:
and we can use Matlab to do this with
limit(g(x),x,0,'left')
limit(g(x),x,0,'right')
returns 1 and 0 respectively.
Exercise: One-sided Limits
Let
Find the following one-sided limits:
Secant and Tangent Lines
Recall that a secant line through a function is the line that passes through the function at two points. For example, consider the secant line through the function
and you can see from the plot that indeed the line passes through the given points. Also, recall to find the slope of the secant line, we’ll need to evaluate the function at the
f(x)=x^2
m = (f(2)-f(1))/(2-1)
which is 3 in this case. We can then use the point-slope form of the line with either the point
The reason that we discuss secant lines in Calculus, is that we want to know the tangent line, which is the line that just touches at a single point. We can’t find a line given only one point, but if we find a number of secant lines that pass through (1,1), we hopefully can find a trend that leads to the tangent line. Then we will use calculus to do this precisely.
If instead of defining the slope of the line through
syms t
slope(t) = (f(t)-f(1))/(t-1)
Now this will allow us to find the slope of the secant line for any point
xvals = [1.5,1.9, 1.99, 1.999, 2.001, 2.01, 2.1,2.5]
mvals = arrayfun(@(t) double(slope(t)),xvals)
transpose([xvals; mvals])
And you will see
We can use this to estimate the tangent line of
The tangent line
Recall that the slope of the tangent line to a curve
For the function
limit((x^2-2^2)/(x-2),x,2)
and return 4. Therefore the tangent line is
tanLine=4*(x-2)+2^2
and a plot of the function with its tangent line at
Exercise: Tangent Line
Use the definition of the tangent line like above to find the tangent line to the curve
Asymptotes
In precalculus you were introduced to asymptotes of functions. In generally, an asympote of a function is a line that the function approaches far from the origin. In calculus, there is a more specific definition.
Horizontal Asymptotes
The line
or
For example, consider the function
which we will
A plot of this is
and (it’s bit hard to see), but there is a horizontal asymptote which is about
Since this is hard to see, we can use the definition above to find the limit. Doing
and
returns 2 in both cases. Therefore
Vertical Asymptotes
The definition of a vertical asymptote is a bit more complicated. The line
In other words, the limit (one-sided or both-sided) is infinite.
For the function seen above, the line
but we can use limits to do this with:
f(x) = (2*x-1)/(x-3)
limit(f(x),x,3,'left')
limit(f(x),x,3,'right')
which returns
Now this provides a way to prove that a vertical line is a vertical asymptote, but not how to find them. If you have a rational function (polynomial over a polynomial), then possible vertical asympotes are where the denominator is 0. For example, let’s look at the function
We can find the possible vertical asymptotes by finding the zeros of the denominator or
R(x)=(x^2-1)/(x^3-13*x+12)
solve(x^3-13*x+12==0)
which returns 1,3,-4.
If you check if each zero above is a vertical asymptotes, you will notice that the limit at
Plots of functions with their asymptotes
Consider the function
Q(x) = (4*x^2-20*x+16)/(x^2+2*x-15)
fplot(Q(x),[-15,15])
set(gca, 'XAxisLocation', 'origin', 'YAxisLocation', 'origin')
and the result is:
One can see the two vertical asymptotes,
limit(Q(x),x,inf)
limit(Q(x),x,-inf)
these both return 4, so the horizontal asymptote is
fplot(Q(x),[-15,15]);
hold on
fplot(4,[-15,15],'--')
hold off
set(gca, 'XAxisLocation', 'origin', 'YAxisLocation', 'origin')
resulting in
Derivatives
Since Matlab can handle limits and a derivative is a limit, it should come at no surprise that Matlab can handle derivatives. For example, recall that the definition of the derivative of
If you enter in the limit
syms x h
limit(((x+h)^2-x^2)/h,h,0)
you get
Of course, Matlab can do the derivative directly without the use of the limit using the diff
command:
diff(x^2,x)
returns
Exercise: Derivatives
Find the following derivatives:
Higher Derivatives
Matlab can also do 2nd-order and higher derivatives quite easily. Here’s an example. Consider the function
g(x)=sin(pi*x)
then one can find the first three derivatives or
diff(g(x),x)
diff(g(x),x,2)
diff(g(x),x,3)
Plots of a function with its tangent line
Consider the function
f(x)=x*sin(2*x)
Above, we saw the details of finding the secant line through this function to approximate the tangent line. Here, however, we will just plot a function and it’s tangent line.
Of course, we need the derivative of the function, which we will define as a function:
df(x) = diff(f(x),x)
then we can find the tangent lines using the slope-intercept form:
tanLine1 = df(pi/6)*(x-pi/6)+f(pi/6)
tanLine2 = df(pi/2)*(x-pi/2)+f(pi/2)
and then plotting the function and the lines can be done with
fplot([f(x),tanLine1,tanLine2],[0,pi])
legend("x sin(2x)","tangent line at \pi/6","tangent line at \pi/2")
S = sym(0:pi/6:pi);
xticks(double(S))
xticklabels(arrayfun(@texlabel,S,'UniformOutput',false))
where the tick labels have been set to be multiples of
Implicit Differentiation
If we have a function, then taking the derivative is done by hand using the rules of differentiation and in Matlab using one of the techniques above. However, we don’t always have a function to represent a curve. The classic example is a circle. Consider the circle with radius 5:
This is where implicit differentiation helps us out. By hand, taking the derivative implicitly on both sides of the equation above, leads to
To do this in Matlab, we can do the following. First define the function like
syms y(x)
circle = x^2+y(x)^2 ==25
where it is important to write any y(x)
and we have to tell Matlab that
dcirc = diff(circle,x)
which returns:
Now unfortunately, Matlab doesn’t have the ability to solve for the derivative (or substitute something else for the derivative), so we will retype this:
syms dydx
solve(2*x+2*y*dydx==0,dydx)
which returns
This is a little clunky. We now show an alternative to this.
Implicit Function Theorem
So technically this comes from Multivariate Calculus, but it’s not too difficult.
Implicit Function Theorem: If we have an equation of the form
We can do this in Matlab with
syms x y
F(x,y) = x^2+y^2
dydx = -diff(F(x,y),x)/diff(F(x,y),y)
where we needed to tell Matlab now y
is a symbolic variable (not a function, like it was defined above). The result is
Implicit Derivative Exercise
Find
Implicit Plots
We can plot the implicit equation fimplicit
command as
syms x y
fimplicit(x^2+y^2-25, [-6 6 -6 6])
axis equal
set(gca, 'XAxisLocation', 'origin', 'YAxisLocation', 'origin')
and results in
We’ll see fimplicit
a little later in the book.
Finding and plotting the tangent line to an implicit curve
Now that we have an expression for the derivative, we can find the tangent line as we did above. However, the main difference is that we need a point (both
Consider finding the tangent line to the circle above at the point dydx
as
A plot of both the circle and the tangent line is
fimplicit(x^2+y^2==25)
hold on
fplot(tanline)
hold off
xlim([-6 6])
ylim([-6,8])
axis equal
set(gca, 'XAxisLocation', 'origin', 'YAxisLocation', 'origin')
Note that the xlim
and ylim
lines are there to improve the plotting window, the axis equal
line sets the aspect ratio to 1 (so the circle looks like a circle) and the set...
line makes the origin like we’re used to it.
The result is
Exercise: Tangent lines of implicit curves
There is a class of implicit curves called elliptic curves that have the form
- Plot the curve. You should see a connected loop as well as a part that looks similar to half a hyperbola.
- Show that the point
is on the curve. Hint: plug the point into the equation and check if satisfies it. - Find
using the implicit function theorem shown above. Define such that the equation is . - Find the tangent line to the curve at
. - Plot the curve and the tangent line.
Finding implicitly
Add this section.
Previous Chapter | Return to all notes | Next chapter |