Matlab Notes For Calculus 1: Lia Vas
Matlab Notes For Calculus 1: Lia Vas
Lia Vas
Content
1. Basic arithmetic
2. Solving equations using solve
3. Representing functions
4. Graphics
5. Solving equations using fzero
6. Limits
7. Differentiation
8. Optimization
9. Integration
10. Practice Problems
1. Basic Arithmetic
You can use +, -, *, \ and ^ to add, subtract, multiply, divide or exponentiate, respectively. For
example if you enter:
If you want to perform further calculations with the value of the answer, you can type ans
rather than retyping the specific answer value. For example,
>> sqrt(ans)
ans = 2
To perform symbolic calculations in Matlab, use syms to declare the variables you plan to
use. For example, suppose that you need factor x²-3x+2. First you need
>> syms x (you are declaring that x is a variable)
Note that we entered 3*x to represent 3x in the command above. Entering * for
multiplication is always necessary in Matlab.
For solving equations, you can use the command solve. The command solve is always
followed by parenthesis. After that, the equation you would like to solve should be entered in
single quotes. Separated by a coma, the equation is followed by the variable for which you
are solving the equation in (single) quotes. Thus, the command solve has the following form
Matlab can give you both symbolic and numerical answer. For example, let us solve
the equation 3x²-8x+2=0.
>> solve('3*x^2-8*x+2=0','x')
ans = [ 4/3+1/3*10^(1/2)] [ 4/3-1/3*10^(1/2)]
If we want to get the answer in the decimal form with, say, three significant digits, we can use
the command vpa.
>> vpa(ans, 3)
ans = [ 2.38] [ 0.28]
You can solve an equation in two variables for one of them. For example the command
>> solve('y^2-5*x*y-y+6*x^2+x=2', 'y')
solves the given equation for values of y in terms of x. The answer is:
ans = [ 3*x+2] [ 2*x-1]
3. Representing a function
The following table gives an overview of how function or symbol representation in MATLAB
e^x exp(x)
most commonly used functions or expressions ln x log(x)
are represented in Matlab. log x log(x)/log(10)
To represent a function, use the command log. base a of x log(x)/log(a)
inline. Similarly to solve, this command is sin x sin(x)
followed by parenthesis and has the following cos x cos(x)
form: arctan(x) atan(x)
π pi
In this case, we can evaluate a function at more than one point at the same time. For
example, to evaluate the above function at 1, 3 and 5 we have:
>> f([1 3 5]) ans = 2 16 38
As when using the calculator, one must be careful when representing a function. For example
1
should be represented as 1/(x*(x+6)) not as 1/x*(x+6) nor as 1/x(x+6),
x ( x+6 )
3
2
should be represented as 3/(x^2+5*x+6) not as 3/x^2+5*x+6,
x + 5x+6
should be represented as exp(5*x^2) not as e^(5*x^2), exp*(5*x^2),
2
e 5x
exp(5x^2) nor as exp^(5*x^2).
ln(x) should be represented as log(x), not ln(x).
2
log3(x ) should be represented as log(x^2)/log(3) not as log(x)/log(3)*x^2.
4. Graphics
x^2+x+1
50
Let us start by declaring that x is a variable: 45
>> syms x 40
35
20
ezplot(function) 15
10
>> ezplot(x^2+x+1) 0
-8 -6 -4 -2 0 2 4 6 8
A new window will open and graph will be displayed. To copy the figure to a text file, go to
Edit and choose Copy Figure. Then place cursor to the place in the word file where you want
the figure to be pasted and choose Edit and Paste.
We can specify the different scale on x and y axis. x^2+x+1
60
To do this, the command axis is used.
It has the following form 50
40
axis([xmin, xmax, ymin, ymax])
30
Note that the domain of function did not change by command axis. To see the graph on the
entire domain (in this case [-10, 10]), add that domain after the function in the command
ezplot: x^2+x+1
60
ezplot(function, [xmin, xmax])
50
In this case, 40
>> ezplot(x^2+x+1, [-10, 10])
will give you the desired graph. 30
20
For the alternative command for graphics, plot, you can find
more details by typing help. 10
To graph multiple curves on the same plot, you can also use -15 -10 -5 0 5 10 15
To graph multiple curves on the same window, you can use the ezplot command in
combination with hold on and hold off on the following way:
ezplot(1st function)
hold on
ezplot(2nd function)
ezplot(3rd function)
...
ezplot(n-th function)
hold off
For example to graph the functions sin(x) and e-x^2 , you can use: >> ezplot(sin(x))
>> hold on >> ezplot(exp(-x^2)) >> hold off
In some cases, the command solve may fail to produce all the solutions of an equation. In
those cases, you can try to find solutions using fzero (short for "find zero") command. In order
to use the command, first you need to write equation in the form
f(x)=0.
Thus, put all the terms of th equations on one side leaving just zero on the other. To find a
solution near the x-value x=a, you can use
The command fzero, similarly as solve is always followed by expression in parenthesis. The
equation should be in single quotes.
If it is not clear what a convenient x-value a should be, you may want to graph the function on
the left side of the equation first, check where it intersects the x-axis. Alternatively, you can
graph left and right side of the equation that is not in f(x)=0 form and see where the two
functions intersect. Then decide which x-value you should use.
Example. To solve the equation ex^2-2=x+4, we can first graph the functions on the left and
right side of the equation using
syms x ezplot(exp(x^2)-2) hold on ezplot(x+4) hold off
From the graph, we can see that the two functions intersect at a value near -1 and at a value
near 1. To use fzero, we need to represent the equation in the form ex^2-2-(x+4)=0 (or
simplified form ex^2-x-6=0). Then, we can find the positive solution by using fzero to find a
zero near 1 and then to find the negative solution near -1, for example. Thus, both solutions
can be obtained by:
>> fzero('exp(x^2)-2-(x+4)', 1) ans = 1.415
>> fzero('exp(x^2)-2-(x+4)', -1) ans = -1.248
Note also that the command solve('exp(x^2)-2=x+4', 'x') returns just the positive solution.
Thus, knowing how to use fzero command may be really useful in some cases.
6. Limits
You can use limit to compute limits, left and right limits as well as infinite limits. For example,
x 2 −4
to evaluate the limit when x → 2 of the function , we have:
x−2
>> syms x
>> limit((x^2-4)/(x-2), x, 2) ans = 4
You can also evaluate left and right limits. For example:
>> limit(abs(x)/x, x, 0, 'left') ans = -1
>> limit(abs(x)/x, x, 0, 'right') ans = 1
Limits at infinity:
>> limit(exp(-x^2-5)+3, x, Inf) ans = 3
7. Differentiation
Start by declaring x for a variable. The command for differentiation is diff. It has the following
form
diff(function)
For example,
>> syms x
>> diff(x^3-2*x+5) ans = 3*x^2-2
For example, to get the second derivative of x3-2x+5, use: >> diff(x^3-2*x+5, 2)
ans = 6*x
Similarly, the 23rd derivative of sin(x) is obtained as follows. >> diff(sin(x), 23)
ans =-cos(x)
To evaluate derivative at a point, we need to represent the derivative as a new function. For
example, to find the slope of a tangent line to x²+3x-2 at point 2, we need to find the derivative
and to evaluate it at x=2.
>> diff(x^2+3*x-2) (first we find the derivative) ans = 2*x+3
>> f = inline('2*x+3', 'x') (then we representative the derivative as a function)
f = Inline function: f(x) = 2*x+3
>> f(2) (and, finally, we evaluate the derivative at 2) ans = 7
8. Optimization
Recall the steps needed in order to find minimum or maximum values of a given function
(using second derivative test)
For example, to find extreme values of x3-2x+5, start by finding first derivative:
>> diff(x^3-2*x+5) ans = 3*x^2-2
Then find critical point(s):
>> solve('3*x^2-2=0', 'x') ans = [6^(1/2)/3] [-6^(1/2)/3]
or, using vpa(ans, 3) ans = [.816] [-.816]
9. Integration
We can use Matlab for computing both definite and indefinite integrals using the command
int. For the indefinite integrals, start with syms x followed by the command
int(function)
For example, the command
>> int(x^2)
evaluates the integral and gives us the answer ans = 1/3*x^3
Matlab can evaluate the definitive integrals of the functions that do not have elementary
x
sin x
dx, ∫ e dx, ∫ e x dx
2
primitive functions. Recall that the integrals ∫
x x
can not be represented via elementary functions. Suppose that we need to find the integral of
sin x
from 1 to 3. The command >> int(sin(x)/x, 1, 3)
x
doesn't gives us a numerical value. We have just: ans = sinint(3)-sinint(1)
Using the command vpa, we obtain the answer in numerical form. For example,
>> vpa(ans, 4) gives us ans = 0.9026
10. Practice problems
x 3 −8
1. Factor x³+3x²y+3xy²+y³. 2. Simplify .
x−2
3. Evaluate the following expressions. (a) sin(π/6) (b)
√ 5+3 (c) log2(5)
√3−1
4. Solve the following equations and express the answers as decimal numbers.
(a) x3-2x+5=0 (b) log2(x2-9)=4.
3
x +x+1
5. Let f(x)= (a) Represent f(x) as a function in Matlab and evaluate it at 3 and -2.
x
(b) Find x-value(s) that corresponds to y-value y=2. (c) Graph f(x) on domain [-4 4].
6. Graph ln(x+1) and 1-x² on the same plot for x in [-2 6] and y in [-4 4].
7. Find the limits of the following functions at indicated values.
12 3
x −1 6x −4x+5
(a) f(x)= 3 , x→1 (b) f(x)= 3+e-2x, x → ∞ (c) f(x)= 3 , x→ ∞
x −1 2x −1
x 3 +x+1
8. Let f(x)= Find the first derivative of f(x) and evaluate it at x=1.
x
9. Let f(x)=e 3x^2+1. (a) Find the first derivative of f(x). (b) Find the slope of the tangent line to
f(x) at x=1. (c) Find the critical points of f(x).
x 65
10. Find the 12th derivative of the function ( + 1) .
2
11. Find the extreme values of (a) x3-4x+8 (b)
Solutions.
1. syms x y followed by factor(x^3+3*x^2*y+3*x*y^2+y^3) gives you ans=(x+y)^3
2. syms x followed by simplify((x^3-8)/(x-2)) gives you ans=x^2+2x+4
3. (a) sin(pi/6) ans=.5 (b) (sqrt(5)+3)/(sqrt(3)-1) ans=7.152 (c) log(5)/log(2) ans=2.3219.
4. (a) solve('x^3-2*x+5=0', 'x') ans= -2.09. (b) solve('log(x^2-9)/log(2)=4','x'). ans= 5, -5.
5. (a) >> f=inline('(x^3+x+1)/x', 'x'), >> f(3) ans= 10.333, >>f(-2) ans=4.5.
x 3 +x+1
(b) The problem is asking you to solve equation =2. Using solve command,
x
solve('(x^3+x+1)/x=2','x'). you get ans=-1.3247 (c) ezplot((x^3+x+1)/x, [-4,4]).
6. hold on ezplot(log(x+1)) ezplot(1-x^2) hold off axis([-2 6 -4 4])
7. (a) syms x limit((x^12-1)/(x^3-1), x, 1) ans=4
(b) limit(3+exp(-2*x), x, Inf) ans=3 (c) limit((6*x^3-4*x+5)/(2*x^3-1), x, Inf) ans=3
8. (a) syms x diff((x^3+x+1)/x) ans = 2*x-1/x^2 or (2*x^3-1)/x^2.
(b) Inline the derivative: g=inline('2*x-1/x^2','x'). Then g(1) gives you ans=1.
9. (a) diff(exp(3*x^2+1)) ans=6*x*exp(3*x^2+1)
(b) Represent the derivative as function: g=inline('6*x*exp(3*x^2+1)','x'). Then evaluate g(1). Get
6*exp(4). To see the answer as a decimal number (say to five nonzero digits) use vpa(ans, 5). Get 327.58.
(c) solve('6*x*exp(3*x^2+1)=0','x') ans=0
10. diff((x/2+1)^65, 12)
11. (a) max (-1.15, 11.079), min (1.15, 4.92). (b) max (.333, .1226), no min.
12. (a) syms x int(x*exp(-3*x)) ans=-1/3*x*exp(-3*x)-1/9*exp(-3*x)
(b) int(x*exp(-3*x), 0,1) ans=-4/9*exp(-3)+1/9 vpa(ans, 4) ans=.08898