next up previous
Next: About this document ...

23 March 2008

University of Cincinnati
Department of Electrical & Computer Engineering and Computer Science


20 ENFD 112 - Fundamentals of Programming

\framebox{\sc Laboratory 1: Familiarization with Modeling Tool}

Spring 2008


{\bb 1. Objective}


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.



{\bb 2. Tasks}


The following three tasks should each take the approximate time shown in parenthesis, and should be performed by the student in the sequence shown.



{\bt 2.1 Log on and off an OCC PC}


  1. Log onto your PC using the id and password provided to you by the College of Engineering Office of College Computing.

  2. Log off the PC.

  3. Log back onto the PC and open several folders including those named Programming Tools and Internet Utilities. Observe the contents of those folders.



{\bt 2.2 Start MATLAB}


  1. From the desktop, raise the Start menu, open the MATLAB submenu, and select matlab.
  2. After a few seconds you should see the MATLAB workspace as shown in Figure 1. Particularly observe the history section (lower left), and the command line interface (right half). The » is the prompt indicating MATLAB is ready for your commands.

Figure 1: The MATLAB workspace.
\begin{figure}\centerline{\epsfysize=4in\epsfbox{Fig/matlab.ps}}
\end{figure}



{\bt 2.3 Experimenting with MATLAB}


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)                                     

Figure 2: The MATLAB editor showing a simple program for calculating the area of a circle and displaying the result.
\begin{figure}\centerline{\epsfysize=4in\epsfbox{Fig/editor.ps}}
\end{figure}



{\bb 3. Invoking the editor}


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                                                                                            .



{\bb 4. Using MATLAB}


Write MATLAB expressions that will solve the following problems. Try them out.

  1. Compute the sum of the product of 3 and 4 and the difference between 9 and 3.



                                                                                                         
  2. Express pi rounded to 3 decimals.



                                                                                                         
  3. Test whether 1+2+3+..+N $\approx$ N  2/2 and 1+2  2+3  2+..+N  2 $\approx$ N  3/3. Hint: use sum.



                                                                                                         
  4. Plot the expression (determined in modelling the growth of the US population)

    \begin{displaymath}P(t) = 197,273,000/(1 + e^{-0.0313(t - 1913.25)}). \end{displaymath}

    where $t$ is the date, in years AD, using $t = 1790 \mbox{ to } 2000$. What population is predicted in the year 2020?


                                                                                                         
  5. Make a good plot of the function $f(x) = sin(1/x)$ for $0.01 < x <
0.1$.


                                                                                                         



{\bb 5. Submission}


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.




next up previous
Next: About this document ...
John Franco 2008-04-02