LAB1
LAB1
LAB1
1
Vectors, Functions, and Plots In MATLAB
In this book > will indicate commands to be entered in the command window. You do not actually type
the command prompt > .
Entering vectors
In MATLAB, the basic objects are matrices, i.e. arrays o f numbers. Vectors can be thought of as special
matrices. A row vector is recorded as a 1 × n matrix and a column vector is recorded as an m × 1 matrix. To
enter a row vector in MATLAB, type the following at the prompt (>) in the command window:
> v = [0 1 2 3]
and press enter. Matlab will print out the row vector. To enter a column vector type
> u = [9; 10; 11; 12; 13]
You can access an entry in a vector with
> u(2)
and change the value of that entry with
> u(2)=47
You can extract a slice out of a vector with
> u(2:4)
You can change a row vector into a column vector, and vice versa easily in Matlab using
> w = v’
(This is called transposing the vector and we call ’ the transpose operator.) There are also useful shortcuts to
make vectors such as
> x = -1:.1:1
and
> y = linspace(0,1,11)
Basic Formatting
To make Matlab put fewer blank lines in its output, enter
> format compact
To make Matlab display more digits, enter
> format long
Note that this does not change the number of digits Matlab is using in its calculations; it only changes
what is printed.
Plotting Data
Consider the data in Table 1.1. We can enter this data into Matlab with the following commands entered
in the command window:
T (C◦ ) 5 20 30 50 55
µ 0.08 0.015 0.009 0.006 0.0055
Built-in Functions
If we wish to deal with formulas for functions, Matlab contains a number of built-in functions, including
all the usual functions, such as sin( ), exp( ), etc.. The meaning of most of these is clear. The dependent
variable (input) always goes in parentheses in Matlab. For instance
> sin(pi)
should return the value of sin π, which is of course 0 and
> exp(0)
will return e0 which is 1. More importantly, the built-in functions can operate not only on single numbers
but on vectors. For example
> x = linspace(0,2*pi,40)
> y = sin(x)
> plot(x,y)
will return a plot of sin x on the interval [0, 2π]
Some of the built-in functions in Matlab include: cos( ), tan( ), sinh( ), cosh( ), log( ) (natural
logarithm), log10( ) (log base 10), asin( ) (inverse sine), acos( ), atan( ). To find out more about a
function, use the help command; try
> help plot
LAB 1.2
MATLAB Programs
In Matlab, programs may be written and saved in files with a suffix .m called M-files. There are two types
of M-file programs: functions and scripts.
Function Programs
Begin by clicking on the new document icon in the top left of the Matlab window (it looks like an empty
sheet of paper).
In the document window type the following:
Look back at the program. All function programs are like this one, the essential elements are:
• Begin with the word function.
• There is an input and an output.
• The output, name of the function and the input must appear in the first line.
• The body of the program must assign a value to the output.
Functions can have multiple inputs, which are separated by commas. For example:
3
Functions can have multiple outputs, which are collected into a vector. Open a new document and type:
f u n c t i o n [ x2 x3 x4 ] = m y p o w e r s ( x )
x2 = x .^2;
x3 = x .^3;
x4 = x .^4;
Save this file as mypowers.m. In the command window, we can use the results of the program to make
graphs:
> x = -1:.1:1
> [x2 x3 x4] = mypowers(x);
> plot(x,x,’black’,x,x2,’blue’,x,x3,’green’,x,x4,’red’)
Script Programs
Matlab uses a second type of program that differs from a function program in several ways, namely:
• There are no inputs and outputs.
• A script program may use and change variables in the current workspace (the variables used by the
command window.)
Below is a script program that accomplishes the same thing as the function program plus the commands in
the previous section:
x2 = x .^2;
x3 = x .^3;
x4 = x .^4;
p l o t ( x , x , ’ b l a c k ’ , x , x2 , ’ b l u e ’ , x , x3 , ’ g r e e n ’ , x , x4 , ’ r e d ’ )
Type this program into a new document and save it as mygraphs.m. In the command window enter:
> x = -1:.1:1;
> mygraphs
Note that the program used the variable x in its calculations, even though x was defined in the command
window, not in the program.
Many people use script programs for routine calculations that would require typing more than one
command in the command window. They do this because correcting mistakes is easier in a program than in
the command window.
Program Comments
For programs that have more than a couple of lines it is important to include comments. Comments allow
other people to know what your program does and they also remind yourself what your program does if you
set it aside and come back to it later. It is best to include comments not only at the top of a program, but
also with each section. In Matlab anything that comes in a line after a % is a comment.
For a function program, the comments should at least give the purpose, inputs, and outputs. A properly
commented version of the function with which we started this section is:
4
For a script program it is often helpful to include the name of the program at the beginning. For example:
% mygra phs
% pl ot s the g r a p h s of x , x ^2 , x ^3 , a nd x ^4
% on the i n t e r v a l [ -1 ,1]
% c a lc ula te powe rs
% x1 is just x
x2 = x .^2;
x3 = x .^3;
x4 = x .^4;
The Matlab command help prints the first block of comments from a file. If we save the above as
mygraphs.m and then do
> help mygraphs
it will print into the command window:
mygraphs
plots the graphs of x, x^2, x^3, and x^4
on the interval [-1,1]
Exercises
1.2.1 Write a function program for the function x2 ex , using entry-wise operations (such as .* and .^).
To get ex use exp(x). Include adequate comments in the program. Plot the function on [−5, 5]. Show
the program and the graph to the Lab Instructor.
1.2.2 Write a script program that graphs the functions sin x, sin 2x, sin 3x, sin 4x, sin 5x and sin 6x on the
interval [0, 2π] on one plot. (π is pi in Matlab.) Include comments in the program. Show the program
and the graph to the Lab Instructor.
LAB 1.3
Discretization Error
Introduction
Below an example is given. A derivative of a function is evaluated by using the values of the function only. To
accomplish this Taylor series expansion is required. Since Taylor series is infinite some terms are left out which
introduces an error.
Example Consider the problem of approximating the derivative f ‘(x0) of a given smooth function f (x ) at the point x
= x0. For instance, let f (x ) = sin(x ) be defined on the real line−∞ < x < ∞, and set x0 = 1.2. Thus, f (x0) = sin(1.2) ≈
0.932 ... .
Further, consider a situation where f (x ) may be evaluated at any point x near x0, but f ‘(x0) may not bedirectly
available or is computationally expensive to evaluate. Thus, we seek ways to approximate f ‘(x0) by evaluating f at x
near x0.
A simple algorithm may be constructed using Taylor’s series. This fundamental theorem is given on the preceding
page. For some small, positive value h that we will choose in a moment, write
5
The MATLAB Script below gives the plot for log-log of the absolute error versus h.
Exercises
1.3.1 Approximate the derivative of the function f(x)=e-2x evaluated at x0 =0.5(refer to the above example and
observe the difference and similarities).
1.3.2 Carry out derivation and calculation analogous to the above example by using the expression