Learning Objectives: This Lab is not to be worked in groups. Each student must work this Lab. By completing this Lab exercise you should be competent in the following learning objectives:
Instructions:
Read the instructions below carefully. Each student should perform the following on an individual basis. However, you should be able to get assistance from any student in your lab. If you can't solve it, ask the TA.
Passing of the Lab
will require that you answer all the questions
on the lab answer sheet
and submit your answers to the TA. Your TA should provide you with a
printed copy of the answer sheet.
Fill out the Questionnaire (Part 1 on the answer sheet). This information will be used by your TA to set up your Lab groups and Lab activities.
Log onto an ews account and follow the instructions for each of the next five sections.
Using your mouse, left click the Applications menu (located at the top of the screen) then choose EWS Software and finally Matlab.
Wait for the Matlab prompt ">>" to
appear and then type the following commands:
(When entering commands, don't include the ">>" symbol.)
Matlab works like a high powered hand calculator. Type the following
commands at the Matlab prompt:
>> 1+1
>> 1.0 + 1.0
>> 1.0e0 + 1.0e0
You should see,

Therefore , Matlab doesn't distinguish between the integer 1 and the decimal representation 1.0 and scientific notation 1.0e0 . The numbers 1 or 1.0 or 1.0e0 are called scalars in Matlab ( so 2, 2.0 and 2.0e0 are the same scalar too and 101.101 and 1.01101e2 are also the same Matlab scalars).
On the answer sheet fill in the blanks with the correct answers for
questions 1 - 5.
If the
answer gives a Matlab error message, don't write down the entire
message, just fill in the blanks with the word ERROR.
Before you type the following make sure to first type
>> format short e
at the Matlab prompt.
1.
>> 1 / 3
2.
>> 1 \ 3
3.
>> 2^5
4.
>> 2.0e0 - 3.0e1 * 5.0e1
5.
>> (2.0e0 - 3.0e1) * 5.0e1




6.
>> Inf + 1
7.
>> Inf - 1
8.
>> 1 / Inf
9.
>> Inf / Inf
10.
>> 0 * Inf
11.
>> (-1) * Inf
12.
>> Inf - Inf
13.
>> 1 / 0
14.
>> -1 / 0
>> i * i
>> format short
>> 1 + 2*i
>> 1 + 2i
>> (1 + 2i) + (3 + 4i)
>> (1 + 2i) * (3 + 4i)
>> (1 + 2i) / (3 + 4i)
>> [ 1 2 3]' (that's an apostrophe not one
double-quote character on your keyboard)
>> [1; 2; 3] + [4; 5; 6]
23.
>> [1; 2; 3] + [1 2 3]
24.
>> [ [1; 2; 3], [1 2 3]' ]
25.
>> x = -1 : 3
26.
>> x = 1 : -3 : 7
27.
>> x = [1 1 1];
>> x = x + [1 1 1]
28.
>> x
29.
>> y = [x, 2 3 4]
A Matlab function is a sequence of Matlab statements (the code) .
These Matlab statements are evaluated sequentially, using the
input values (if any) provided in the call to the function. The
function then may return a value or values. Functions differ from
scripts in that all function variables are local to the function. That
is, the variables in a function can't been seen or modified in the
Matlab command window.
The basic Math functions you find on your hand calculator are included
in Matlab for example,
>> sin( pi/2) (formally when we
type this and hit the Enter key in programming terminology we say we
"call" the sin function)
>> exp (1) ( e1)
>> log( exp(1)) (natural log, that is, log base e,
standard math notation is ln)
>> log10(10) ( log base 10)
>> abs(-1) ( absolute value)
>> sqrt( 4) (square root)
If you forget or want to get more details as to what these functions
do, you can use Matlab Help or type help xxx at the Matlab command
window.
Matlab has innumerably many functions. Consider a few we will use later
this semester.
On the answer sheet answer questions 30-42 .
30.
>> x = linspace(-1, 1, 4)
31.
>> x = linspace(0, 5, 1000); (don't forget this
semi-colon,
it means suppress out to screen)
>> min(x)
32.
>> max(x)
33.
>> x = [1 3 5];
>> sum(x)
34.
>> sum([1; 3; 5])
35.
>> z = [1 2 3; 4 5 6];
>> sum(z)
36.
>> x = [1 3 6 12];
>> diff(x)
37.
>> x = [1 2 3];
>> mean([1 2 3])
38.
>> x = [1 2 3];
>> median(x)
39.
>> x = [1 2 3 4];
>> median(x)
40.
>> x = [1 2 3 4];
>> cumsum(x)
41.
>> sqrt(-1)
42.
>> abs(1 + 2i)
All Matlab programs are either scripts or functions. In this part of
Lab 1 we will write a script. In Lab 2 we will write a function and
compare the differences between functions and scripts.
A MATLAB script file
contains any
Matlab command you can enter at the Matlab prompt.
To write a script you first open the Matlab editor using the "edit"
command. Then after your type in your code (Matlab statements) you save
your file. Script files
can have
any name you choose but their file extension must be ".m". To
execute a script file, while in the Matlab environment, type in the
name of the
script file.
All the commands (but not comments ) in
a script
file are executed. The execution starts with the first command at
the top
of the file and proceeds sequentially downward. A comment is anything
(text, numbers, ...) that follows (on the same line) the "%" symbol.
Comments are informative messages programmers include to help remind
themselves and explain to others what the code is trying to accomplish.
You may choose to comment a script file. A comment is not executable but gives information for the programmer.
Open the Matlab editor by typing (use
your last
name not "lastname")
>> edit lastname.m
If you are asked whether to create the file, choose yes.
in the Matlab command window. Type the following Matlab commands into ".m" and click File then select Save or Save AS from the menu.
% This is a comment.
% A comment begins with a percent symbol.
% All text following a percent symbol is ignored when the script is
% executed.
clc % Command to clear the Matlab screen.
r = 4;
x = [1 2 3] % No semicolon here
y = sin(x);
y

On the answer sheet answer questions 43-50 .
43. Before we run the script file, in the Matlab environment,
enter the who command, which lists all your variables:
>> who
List the variables: (Write "Nothing" if no variables appear)
44. Now type the following,
>> clear
>> who
List the variables:
45. Next type,
>> r = 1;
>> who
List the variables:
>> r
46.
What is the value of r? _________
Use the "what" system command to make sure that you have correctly saved your lastname.m file.
>> what (this lists the .m and .mat files in your directory)
If you don't see the "lastname" file ask your TA for help before you proceed.
47. Run the script by typing your last name at the prompt.
Write what is displayed on your screen:
48. Type
>>r
What is the value of r? _________
49. So, can running a script alter the value of a workspace variable? Y / N
50. Again, type
>> who
List the variables in your workspace:
Like Mathematica, Matlab can be used to perform symbolic
differentiation, integration and to compute limits. Symbolic operations
like differentiation, integration or limits require that we tell Matlab
which symbolic variables are in use in our formula. To do this we need
to use the syms command (not
to be confused with "The Sims").
Example 1: Find the indefinite integral of x2 with respect
to x
>> syms x ( we
tell Matlab that x is a symbolic variable not a variable in the sense
discussed above in Part 4)
>> int(x*x,x) ( the first argument in the call
to the int function is x*x and the second argument is x)
When there is no ambiguity, you can omit the argument that lists the
symbolic variable. Type,
>> int(x*x)
The result is symbolic so you can use the result is the input to
another function. For example, type,
>> result = int(x^2,x)
Let's check our answer by differentiating with respect to x
>> diff(result,x,1) ( 1 means first
derivative)
or since there is only one symbolic variable in result and there is no
ambiguity,
>> diff(result)
Example 2: find the limit of x/x as x goes to 0
>> limit(x/x, x, 0)
Example 3: The formula may contain non-symbolic variables as in,
>> a = 2.5
>> solution = int(a*x,x)
On the answer sheet answer questions 51-53 .
51. Compute the derivative of t3 with respect to t,
>> syms t
Note: When you execute the syms command, you may see a
page of red text. If you
do, please restart Matlab.
>> solution = _______________________________
(The solution should be
6t, but you should
write the
command
used to find the solution.)
52. Write the result of executing the following code,
>> syms y
>> limit(sin(y) / y, y, 0)
53. Write the result of executing the following code,
>> syms z
(this is called the fourth derivative of sin(z) with respect
to
z)
>> diff(sin(z), z, 4)
You can Exit matlab now by typing
>> exit
at the Matlab prompt ">>" .
On the answer sheet answer questions 54 - 55.
54. Find in the course syllabus: Find the
grade
range(in points) for an A- grade for CS101:
55. Click the "Staff " hyper-link: What is the email address of your lab TA?
For your reference, the course website is at:
http://www.ews.uiuc.edu/~cs101/
Although some lab work is done in groups, the MPs(machine problems) must be worked individually. It is the students responsibility to make sure that his/her code is not copied since this would constitute cheating--- see Lecture 0 of the class notes. When you are working on your MP and have to leave the lab for a brief time, you should choose Lock Screen from the System menu at the top of the screen. When you come back into the lab, click on the computer screen and type your password to unlock the terminal.
To complete the first Lab activity you should Lock Screen and have your TA sign that you have done this correctly in question 56 of your answer sheet.
56. Follow the instructions in Part 9.
Then, obtain the signature of your TA and submit this answer sheet to
your
lab TA.
Log out: Choose "Log Out" from the System menu.
NEVER leave your computer without logging out since someone else
entering
the lab after you would have access to all your files (MPs).
Congratulations you have just finished Lab #1 !!!
Submit your answer sheet to your lab TA.