This is the html version of the file http://mpcs.usip.edu/faculty/vas/Math201/MATLAB_forMA201.PDF.
G o o g l e automatically generates html versions of documents as we crawl the web.
To link to or bookmark this page, use the following url: http://www.google.com/search?q=cache:S3KhgVNSB84J:mpcs.usip.edu/faculty/vas/Math201/MATLAB_forMA201.PDF+TI83%2B+%22Linear+Equations%22&hl=en


Google is not affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted: ti83+ linear equations 

Page 1
- 1 -
WELCOME TO MATLAB
by Lia Vas
Content
0. Getting Started
1. Algebra
1.1 Basic arithmetic
Practice problems 1
1.2 Solving equations
Practice problems 2
2. Graphics
Practice problems 3
3. Calculus
3.1 Representing a function
3.2 Differentiation
Practice problems 4
3.3 Integration
Practice problems 5
3.4 Limits
3.5 Basics of Differential Equations
Practice problems 6
4. Graphics Continued
4.1 Parametric Plots
4.2 Special effects
Practice problems 7
5. Differential Equations Continued
5.1 Direction Fields
5.2 Numerical Solutions
5.3 Second Order Equations
Practice problems 8
6. M-Files
Practice problems 9
0. GETTING STARTED
You start MATLAB as you would any other software application (via Start menu, in
Programs) if MATLAB is installed on the computer you are using. Listed below are
instructions of how to access MATLAB from computers around campus.
- right click on NETWORK NEIGHBORHOOD (MY NETWORK PLACES)
- choose MAP NETWORK DRIVE
- type: \\usp-csserv\matlab$
After you start the program, the Command Window will open. Click in the window and
you can begin entering commands. You can exit the window by typing exit.

Page 2
- 2 -
1. ALGEBRA
1.1 Basic Arithmetic
MATLAB can do anything your calculator can do and much more. You can use +, -, *, \
and ^ to add, subtract, multiply, divide or exponentiate, respectively. For example if you
enter:
>> 2^3 - 2*2
MATLAB gives you the answer:
ans =
4
If you want to perform further calculations with the answer, you can type ans rather than
retyping the whole answer. For example,
>> sqrt(ans)
ans =
2
You can perform symbolic calculations. To do that, use syms to declare the variables
you plan to use. For example, suppose that you need factor x²-3x+2. Note that you must
type 3*x for 3x. Then you type:
>> syms x (you are declaring that x is a variable)
>> factor(x^2-3*x+2)
ans =
(x-1)*(x-2)
Besides factor command, you have simplify and expand.
Practice problems 1
1. Factor x³+3x²y+3xy²+y³.
2. Simplify
2
8
3
x
x
.
3. Expand (x²+1)(x-5)(2x+3).
1.2 Solving Equations
For solving equations, you can use the command solve. For example, to solve the
equation x³-2x-4, you type:

Page 3
- 3 -
>> solve('x^3-2*x-4=0')
and get the following answer:
ans =
[ 2]
[ -1+i]
[ -1-i]
Here i stands for the imaginary number
1
. This answer tells us that there is just one
real solution, 2.
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]
By changing 3 in the command vpa(ans, 3) you can specify the number of digits in the
answer.
You can solve more than one equation simultaneously. For example suppose that we
need to solve the system x²+ x+ y² = 2 and 2x-y = 2. We can type:
>> solve( 'x^2+ x+ y^2 = 2', '2*x-y = 2')
and get the answer:
ans =
x: [2x1 sym]
y: [2x1 sym]
This answer tells us just that the solutions are two points. MATLAB interprets the
solution as two vectors. To get the solution vectors displayed, we can type:
>> [x,y] = solve( 'x^2+ x+ y^2 = 2', '2*x-y = 2')
And get the solutions
x =

Page 4
- 4 -
[ 2/5]
[ 1]
y =
[ -6/5]
[ 0]
You can solve an equation in two variables for one of them. For example:
>> solve('y^2-5*x*y-y+6*x^2+x=2', 'y')
ans =
[ 3*x+2]
[ 2*x-1]
Practice problems 2
1. Solve sin x = 2-x for x.
Note: to get the function sin x, you type sin(x). Similarly, some other functions of interest
in MATLAB are, cos(x) , tan(x), exp(x) for exponential function, log(x) for natural
logarithm (not for logarithm with base 10).
2. Solve 5x+2y+4z = 8, -3x+y+2z = -7, 2x+y+z = 3 for x, y and z.
3. Solve y²-5xy-y+6x²+x = 2 for x.
2. GRAPHICS
The simplest way to graph a function is to use the command ezplot (easy plot). For
example, to graph the function x²+x+1, we use:
>> ezplot('x^2+x+1')
If we want to see the graph just on the interval [-2, 2], we use:
>> ezplot('x^2+x+1', [-2, 2])
If we want to see the graph for x in the interval [-2, 2] and for y in the interval [1, 4], we
type
>> axis([-2 2 1 4])
For the alternative command for graphics, plot, you can find more details by typing help.
To plot multiple curves on the same window, you can also use the ezplot command. For
example:
>> ezplot('sin(x)')

Page 5
- 5 -
>> hold on
>> ezplot('exp(-x^2)')
>> hold off
Practice problems 3
1. Graph
x
x
x
1
3
+
+
for x in [-4 4] and y in [-10, 10].
2. Graph ln(x+1) and 1-x² on the same window for x in [-2 6] and y in [-4 4].
3. CALCULUS
3.1 Representing a function
To represent a function, use the command inline. Here is how to define the function
x²+3x-2:
>> f = inline('x^2+3*x-2', 'x')
f =
Inline function:
f(x) = x^2+3*x-2
After defining a function, we can evaluate it at a point. For example,
>> f(2)
ans =
8
In some cases, we will need to define function f as a vector. Then we use:
>> f = inline(vectorize('x^2+3*x-2'), 'x')
f =
Inline function:
f(x) = x.^2+3.*x-2
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

Page 6
- 6 -
3.2 Differentiation
To differentiate a function, we use the command diff. For example,
>> diff('x^3-2*x+5')
ans =
3*x^2-2
Alternatively, you can declare the variable x to be symbolic
>> syms x
and then use the command diff without the quotes.
>> diff(x^3-2*x+5)
To get the second derivative, use:
diff(x^3-2*x+5, 2)
ans =
6*x
Similarly, you can find any higher derivative.
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(vectorize(ans), 'x') (then we representative the derivative as a vector
function)
f =
Inline function:
f(x) = 2.*x+3
>> f(2) (and, finally, we evaluate the derivative at 2)
ans =
7

Page 7
- 7 -
The command diff can be used to compute partial derivatives. For example, to find the
first partial derivative with respect to y of the function x²y³, we use
>> syms x y
>> diff(x^2*y^3, y)
ans =
3*x^2*y^2
To find the second partial derivative with respect to x, we use
>> diff(x^2*y^3, x, 2)
ans =
2*y^3
For the mixed second partial derivative, we can use:
>> diff( diff(x^2*y^3, x), y)
ans =
6*x*y^2
Practice problems 4
1. Find the first derivative of the function
x
e
x
x
+ )1
ln(
sin
2
and evaluate it at x=3.
2. Find the 12th derivative of the function
65
)1
2
( +
x
.
3. Find all of the first and second partial derivatives of the function
xy
e
x
sin
2
.
3.3 Integration
We can use MATLAB for computing both definite and indefinite integrals using the
command int. For the indefinite integrals, consider the following example:
>> int('x^2')
ans =
1/3*x^3
Similarly as for diff command, we do not need the quotes if we declare x to be a
symbolic variable. Therefore the command
>> int('x^2')
is equivalent to
>> syms x

Page 8
- 8 -
>> int(x^2)
For the definitive integrals:
>> int(x^2, 0, 1)
ans =
1/3
In MATLAB Inf stands for positive infinity. MATLAB can evaluate the (convergent)
improper integrals as well. For example:
>> int(1/x^2, 1, Inf)
ans =
1
For the divergent integrals, MATLAB gives us the answer infinity. For example:
>> int(1/x, 0, 1)
ans =
inf
MATLAB can evaluate the definitive integrals of the functions that do not have
elementary primitive functions. Recall that the integrals
,
sin
dx
x
x
,
dx
x
e
x
dx
e
x
2
can not be represented via elementary functions. Suppose that we need to find the
integral of
x
x
sin
from1 to 3. The command
>> int(sin(x)/x, 1, 3)
doesn't gives us a numerical value. We have just:
ans =
sinint(3)-sinint(1)
To find the numerical value, we can find the indefinite integral, represent it as a function f
and then calculate f(3)-f(1).
>> int(sin(x)/x)
ans =
sinint(x)
>> f = inline(vectorize(ans), 'x')
f =

Page 9
- 9 -
Inline function:
f(x) = sinint(x)
>> f(3)-f(1)
ans =
0.9026
Practice problems 5
1. Evaluate the integrals
dx
x
x
4
5
2
+
and
dx
x
x
4
5
2
1
0
+
.
2. Using MATLAB determine if the following integrals converge or diverge. If they
converge, evaluate them.
+
+
1
2
6
5
3
dx
x
x
+
2
1
)1
)(1
(
1
dx
x
x
3. Evaluate the integral (that you are going to need for you chemistry class):
dx
e
x
2
3.4 Limits
You can use limit to compute limits, left and right limits as well as infinite limits. For
example, to evaluate the limit when x 2 of the function
2
4
2
x
x
, we have:
>> syms x
>> limit((x^2-4)/(x-2), x, 2)
ans =
4
For left or right limits:
>> limit(abs(x)/x, x, 0, 'left')
ans =
-1
>> limit(abs(x)/x, x, 0, 'right')
ans =
1
Limits at infinity:

Page 10
- 10 -
>> limit(exp(-x^2-5)+3, x, Inf)
ans =
3
3.5 Basics of Differential Equations
We can use MATLAB to solve differential equations. The command for finding the
symbolic solution is dsolve. For that command, the derivative of the function y is
represented by Dy. For example, suppose that we want to find the solution of the
equation x y' - y = 1. We will have:
>> dsolve('x*Dy-y=1', 'x')
ans =
-1+x*C1
This means that the solution is any function of the form y = -1+ cx, where c is any
constant.
If we have the initial condition f(1) = 5, we can get the particular solution on the following
way:
>> dsolve('x*Dy-y=1', 'y(1)=5', 'x')
ans =
-1+6*x
To draw this solution, we type:
>> ezplot(ans)
We can draw a couple of different solutions on the same graph. For example, let us look
at the equation y' = x+y. Let us make a function that depends on x and c that will
represent a general solution of this differential equation.
>> s = dsolve('Dy = x+y', 'y(0)=c', 'x')
s =
-x-1+exp(x)*(1+c)
Let us draw the solutions for values of c=-2, -1, 0, 1, 2, 3, 4.
>> for cval = -2:4
hold on
ezplot(subs(s, 'c', cval))
hold off

Page 11
- 11 -
end
Finally, we can get a better picture by making the axis as follows:
>> axis([-6 6 -100 100])
You can find more details for the command ode45 which produces the numerical
solution of the equation, by typing help.
Practice problems 6
1. Find the limit of
1
2
5
4
6
3
3
+
x
x
x
when x
.
2. Find the general solution of the differential equation xy'+2y = sin x. Then find the
solution that satisfies the condition y(ð/2)=1 (in MATLAB ð is pi).
3. Draw the solutions of the differential equation y' = y² + x for the values of constant c in
the general solution being integer numbers between 1 and 5.
4. GRAPHICS CONTINUED
4.1 Parametric Plots
We can use the command ezplot to graph a parametric curve as well. For example, to
graph a circle x = cos t, y = sin t for 0 t 2 ð, we have:
>> ezplot('cos(t)', 'sin(t)', [0, 2*pi])
4.2 Special effects
You can change the title above the graph by using the command title. For example,
>> ezplot('x^2+2*x+1')
>> title 'a parabola'
You can add labels to x and y axis by typing xlabel and ylabel.
You can add text to your picture. For example, suppose that we want to add a little arrow
pointing to the minimum of this function, the point (-1, 0). We can do that with the
command:
>> text(-1, 0, '(-1, 0) \leftarrow a minimum')

Page 12
- 12 -
We can also make three-dimensional plots nicer by
- using perspective (camproj('perspective'); undo by camproj('orthographic'))
- showing bounding box (box on; undo by box off),
- making axis units equal (axis tight, axis equal; undo by axis normal),
- showing grid (grid on; undo by grid off) and
- allowing rotation by mouse dragging (rotate3d on; undo by rotate3d off).
For example,
>> camproj('perspective');
>> box on;
>> axis tight;
>> axis equal;
>> grid on;
>> rotate3d on;
>> ezsurf('sqrt(x^2+y^2)', [-7, 7], [-7, 7])
You can produce an animated picture with comet. This command produces a parametric
plot of a curve just as ezplot does, except that you can see the curve being traced out in
time. For example, we can trace the motion on the circle x = cos t, y = sin t by using
>> t = 0:0.1:4*pi; (meaning that t has values between 0 and 4ð, 0.1 step away from
each other)
>> comet(cos(t), sin(t))
If the point is moving too fast, you can reparameterize the same circle as follows
>> t = 0:0.1:200*pi;
>> comet(cos(t/50), sin(t/50))
Practice problems 7
1. Graph the parametric curve x = t cos t, y = t sin t for 0 t 10 ð.
2. Graph the cubic curve y = x³. Label the axis and put a title "a cubic curve" on the top
of the graph. Indicate on the graph that the point (0,0) is an inflection point.
3. Trace the curve x = t cos (t/20), y = t sin (t/20) in time for 0 t 200 ð.
5. DIFFERENTIAL EQUATIONS CONTINUED
5.1 Direction Fields
Suppose that we need to plot the direction field for a first order linear equation. We can
do that by using the command quiver together with the command meshgrid. The

Page 13
- 13 -
following set of commands gives the direction field of the autonomous differential
equation y' = -1/10 y(y-2)(y-4).
>> [X, Y] = meshgrid(-4:0.2:4, -4:0.2:4);
>> P = inline(vectorize('-(1/10)*Y*(Y-2)*(Y-4)'), 'X', 'Y');
>> S = P(X, Y);
>> K = inline(vectorize('1/sqrt(1+S*S)'),'S');
>> L=K(S);
>> quiver(X, Y, L, S.*L, 0.5)
>> axis equal tight
5.2 Numerical Solutions
Many differential equations cannot be solved explicitly in terms of elementary functions.
for those equations, we will need the numerical methods to get the approximate solution.
The approximate solutions of the equations can be found by using the command ode45.
We can illustrate the command ode45 on the initial value problem y' =
2
x
e
, y(0)=1.
The command ode45 requires that the function on the right side of the equation is
represented as a vector. So, we can start with the command:
>> f=inline(vectorize('exp(-x^2)'),'x','y');
The command ode45 plots the graph of the initial value problem on the specified
interval. Suppose that we want to graph the solution on the interval [0, 2]. Finally, we
must enter the y value at x=0 from the initial condition:
>> ode45(f, [0 2], 1)
We can obtain the numerical values of the solution as well.
>> [x, y] = ode45(f, [0 2], 1)
The values between 0 and 2 at which ode45 calculates the solution are stored in x, and
the value of solution at these values is stored in y.
If we want the graph without the circles around the points at which ode45 calculates the
solution, we can do the following:
>> [x, y] = ode45(f, [0 2], 1);
>> plot(x, y)
5.3 Second Order Equations
The second order linear equations can be solved similarly as the first order differential
equations by using dsolve or ode45. For the command dsolve, recall that we represent

Page 14
- 14 -
the first derivative of the function y with Dy. The second derivative of y is represented
with D2y. For example, the command for solving y''-3y'+2y = sin x.
>> dsolve('D2y-3*Dy+2*y=sin(x)', 'x')
ans =
3/10*cos(x)+1/10*sin(x)+C1*exp(x)+C2*exp(2*x)
If we have the initial conditions y(0) = 10, y'(0)=-10, we would have:
>> dsolve('D2y-3*Dy+2*y=sin(x)', 'y(0)=1', 'Dy(0)=-1', 'x')
ans =
3/10*cos(x)+1/10*sin(x)+5/2*exp(x)-9/5*exp(2*x)
For the equations that can not be solved in terms of elementary functions, we use the
numerical methods. We can illustrate the ode45 command for the second order
equations on the equation y'' + x y' + y = 0. For ode45, the second order differential
equation must be solved for y''. In this case we have that y'' = -x y' - y. The first derivative
of y is represented with y(2) and function y with y(1). Suppose that we have initial
conditions y(0)=1 and y'(0)=0. As for the first order equations, we then indicate the
interval on which we want to graph the solution, say [0, 5] and the initial conditions.
>> f=inline('[y(2); -y(2)*x-y(1)]','x','y');
>> ode45(f, [0 5], [1 0])
The output represents two functions. The blue function is the solution of the equation
and the green function is the derivative.
We can obtain the numerical values of the solution as well.
>> [x, y] = ode45(f, [0 5], [1 0])
The vector y will consist of two columns. The first consists of the values of the solution y
at points between 0 and 3 and the second consists of the values of the derivative y' at
these points.
Practice problems 8
1. Graph the direction field of the differential equation y' = (y²-1)(x²-1). Let x takes the
values between 0 and 5 with step size .2 and y takes the values between -3 and 3 with
same step size.
2. Graph the solution of the initial value problem y' = -y + 5 cos 10x - sin 2x, y(0)=0 on
the interval [0, 5]. Find the value of the solution at 3.
3. Find the exact solution of the equation y''-4 y'+4 y=
x
e
+x². Then plot the solution of
the initial value problem y(0)=3, y'(0)=-2

Page 15
- 15 -
4. Using ode45 graph the solution of the initial value problem y'' + x² y' + y = 0, y(0)=1,
y'(0)=-1 on the interval [0, 4].
6. M-FILES
Suppose that you want to perform the same operation many times for different input
values. MATLAB allows you to create a function or a script that you can execute
repeatedly with different input values. Such function or a script is called M-file. M-file is
an ordinary text file containing MATLAB commands. You can create them using any text
editor that can save files as plain ASCII text. You can also create an M-file by using File
and choosing New M-file options from your MATLAB command window.
For example, suppose that you want to solve the equation ax² + bx + c = 0 using the
quadratic formula.
We can create a function which will have as input values of a, b and c and which will
give the solution(s) of the quadratic equation.
Then we can open a M-file window and type the following
function [x1 x2] = quadratic(a, b, c)
x1 = (-b+sqrt(b^2-4*a*c))/(2*a)
x2 = (-b-sqrt(b^2-4*a*c))/(2*a)
Then save the file in your working directory. You execute the file as follows.
>> quadratic(1, -3, 2)
This means that you want to solve the equation -3 x + 2 = 0. MATLAB gives you the
answer:
x1 =
2
x2 =
1
This function gives us the complex values if the solutions are complex numbers. For
example,
>> quadratic(1, 0, 4)
x1 =
0 + 2.0000i
x2 =
0 - 2.0000i

Page 16
- 16 -
Let us now look at the function file that uses the Euler method to approximate the
solution of the initial value problem y' = f(x,y), y(x0) = y0 on the interval [x0, b] using n
steps. The input is the inline function f, x0, y0, b and n.
function [x, y] = euler(f, xinit, yinit, b, n)
h = (b - xinit)/n; (calculates the step size)
x = zeros(n+1, 1);
y = zeros(n+1, 1); (initialize x and y as column vectors of size n+1)
x(1) = xinit;
y(1) = yinit; (the first entry in the vectors x and y is x0 and y0 respectively)
for i = 1:n
x(i + 1) = x(i) + h; (every entry in vector x is the previous entry plus the step size h)
y(i + 1) = y(i) + h*f(x(i), y(i)); (Euler Method formula)
end
Practice problems 9
1. Write down the function M-file that will calculate the polar coordinates (r, è) of the
given point in Cartesian coordinates (x,y).
2. Using the for loop, create a function M-file that will calculate the factorial of a given
integer m.
3. Write down the function M-files that will calculate the Left, Right and Middle sum of a
given function on a given interval using the given number of steps. You can look at your
TI83 (TI83+) program LFTRGTMD for hints.
4. Do the same for Simpson's and Trapezoidal sum. You can look at your TI83 (TI83+)
programs TRAPEZ and SIMPSON for hints.