The goals of this laboratory are to learn how to find, launch, and
operate MATLAB in an effective and reproducible manner. The student
will input a few elementary expressions and observe the results.
The following three tasks should each take the approximate time shown
in parenthesis, and should be performed by the student in the sequence
shown.
Arithmetic expressions:
Type in each of the following commands and record the output.
| » 7+9 | |
| » 3*2 | |
| » 3 2 | |
| » 2*(3+4) | |
| » 2+3*4 | |
| » (2+3)*4 | |
| » 355/113 | |
| » ans*2 | |
| » 3,14 | |
| » 3.14 | |
| » 2/3 | |
| » format long | |
| » 2/3 |
Some functions:
MATLAB provides some built-in functions to make modeling easy.
| » round(ans) | |
| » ceil(3.1) | |
| » floor(3.1) | |
| » help | |
| » help ops | |
| » helpwin | |
| » helpdesk | |
| » sin(.5) | |
| » asin(.5) | |
| » cos(.5) | |
| » acos(.5) | |
| » atan(-25) | |
| » exp(1.2) |
Some constants:
MATLAB provides some built-in constants as the following show.
| » pi | |
| » eps | |
| » eps/2 | |
| » eps/100000000000000 | |
| » help eps | |
| » 1+eps == 1 | |
| » 1+eps/2 == 1 | |
| » a=10 | |
| » a=a 2 | |
| » Inf=Inf+1 | |
| » Inf*2 | |
| » Inf/2 | |
| » 1/eps | |
| » 1/0 | |
| » -1/0 | |
| » 0/0 | |
| » Inf-Inf | |
| » Inf/Inf | |
| » 0*Inf |
Variables:
Now try these.
| » a=3 | |
| » b=5; | |
| » b | |
| » c=a+2*b; | |
| » c=c+1 | |
| » c=a*d; | |
| » base=3 | |
| » height=5 | |
| » area=base*height/2 |
Strings:
Strings must be treated differently from numbers.
| » s=1+2 | |
| » s='1+2' | |
| » eval(s) | |
| » f='2*x' | |
| » eval(f) | |
| » x=7 | |
| » eval(f) | |
| » f(2) | |
| » f(3) | |
| » ['text1' 'text2'] | |
| » ['text' 7] | |
| » ['text' num2str(7)] | |
| » help num2str | |
| » disp(['f(x)=' num2str(eval(f))]) |
Vectors and Lists:
Now try these.
| » v=[4 1 7 5] | |
| » v(3) | |
| » v(3)=8 | |
| » v(5) | |
| » w=[1 2 3 4] | |
| » v+w | |
| » v-w | |
| » v+1 | |
| » 2*w | |
| » w/4 | |
| » sum(w) | |
| » v*w | |
| » v.*w | |
| » w. 2 | |
| » [v w] | |
| » ans' | |
| » u=1:4 | |
| » u=0:2:10 | |
| » u=0:3:9 | |
| » u=0:3:10 | |
| » u=0:.2:.6 | |
| » u(2:3) | |
| » help linspace | |
| » linspace(0,1,6) |
Boolean values and conditionals:
| » 2<3 | |
| » 2>3 | |
| » -3<2 | |
| » 1+1 == 2 | |
| » 2 == 3 | |
| » 2 = 3 | |
| » i=1; | |
| » if i == 1 disp('i is one'); end | |
| » if i == 1 disp('i=1'); else disp('i!=1'); end | |
| » 0<=1 | |
| » 1<=1 | |
| » 1<2 | 1>2 | |
| » 1<2 & 1>2 | |
| » (1<2) |
Matrices:
| » m=[1 2 3; 4 5 6] | |
| » m(2,1) | |
| » m(1,3) | |
| » m(1,3)=7 | |
| » v | |
| » w | |
| » [v;w] | |
| » v' | |
| » ans(3) | |
| » w' | |
| » [v' w'] | |
| » ans' | |
| » [v';w'] | |
| » help save | |
| » save junk |
Graphics:
| » x=0:.2:1 | |
| » y=x. 2 | |
| » plot(x,y) | |
| » plot(x,1-x/2) | |
| » plot(x,sin(10*x)) | |
| » x=0:.01:1; | |
| » plot(x,sin(10*x)) | |
| » xlabel('x') | |
| » ylabel('y') | |
| » xlabel('x_1'); ylabel('x_2') | |
| » title('My first plot') | |
| » hold on | |
| » plot(x,x. 2) | |
| » grid on | |
| » clf | |
| » subplot(211); plot(x,sin(10*x)) | |
| » subplot(212); plot(x,x. 2) | |
| » get(gcf) |
![]() |
Use the editor to save sequences of commands, or macros, for reuse.
The editor may be invoked by selecting from the Desktop Tools
submenu of the Start menu. The editor looks as in
Figure 2 (showing some code) and is fairly easy to pick up.
You should invoke the editor and add lines that will examine the
elements of a two dimensional vector v and rearrange them, if
needed, so that the elements of v are in increasing order.
The lines you add should involve a simple test followed by an
assignment (maybe 4 or 5 lines if you typed them into the MATLAB
interpretor). When done, select Save as... and give the file
the name ordervec.m. Now return to the interpretor and type
| » v=[2 1] |
| » ordervec |
| » v |
and say what happens .
Write MATLAB expressions that will solve the following problems. Try
them out.
Submit m files for each solution to problems of
Section 4 on or before April 6 using blackboard. See the course
webpage at http://gauss.ececs.uc.edu/Courses/HTML/E112.html
for instructions.