Lab 1-1
Lab 1-1
Lab 1-1
Aims
Pre-Lab:
Operations on variables and plotting graphs in MATLAB
On a PC that is installed with MATLAB, start MATLAB. A command window will
appear where one can type in and execute MATLAB commands. Execute the set
of commands/codes in the boxes and check the results. This self-study method is
one of the fastest ways to master the basic MATLAB commands.
In a report, document what each command does. Focus on the specific actions
and purposes, rather than the execution results. For commands that return an
error message, document the reasons.
Page 1
Laboratory 1: Introduction to MATLAB 1.2
In addition, explain why the same command executed twice in item 13 and item
14 generates different results.
1.F. Boolean operations and plotting graphs over a limited range of the
x axis.
1. A=[0 1 2 3 4]; 7. C=([1 0 1 1 1]== [1 0 1 0 13. y=(1<x)&(x<4);
2. A<3 0]) 14.plot(x,y); axis([0
3. B=(A>2) 8. C=([1 0 1 1 1] ≠ [1 0 1 0 10 -2 2]); grid;
4. C=([1 1 0 0] & [ 1 0]) 15.y=1<x<4;
1 1 0]) 9. x=0:0.01:10; 16.plot(x,y); axis([0
5. C=([1 1 0 0] | [ 1 10.y=(x<3); 10 2 2]); grid;
1 1 0]) 11.figure 17.plot(x,y); axis([0
6. C=~ [1 0 1 0 0] 12.plot(x,y); axis([0 10 -2 10 -2 2]); grid;
2]); grid;
In addition, explain the difference between items 13 and 15.
Main Lab
Using Symbolic Math
In the symbolic math in MATLAB, the characters (or words) such as a, b, and temp
are treated as symbolic variables, not numeric variables. Mathematical
expressions can be computed or manipulated in symbolic forms. Find out what
else can be done using symbolic math in the following problems.
Laboratory 1: Introduction to MATLAB 1.4
2.A. Write down what each of the lines in the following box does and capture the
execution result.
>>syms a b c x t
>>y=sin(t);
>>diff(y)
>>int(y)
>>int(y, t, 0, pi)
>>z=int(xˆ2*exp(-x),x,1,3)
>>double(z)
>>limit(sin(t)/t,t,0)
>>symsum(xˆ2, x,1,4)
>>T=solve(a*xˆ2+b*x+c,x)
>>T2=solve(a*xˆ2+b*x+c,b)
>>a=1;b=2;c=3;
>>z=eval(T)
>>a=t;
>>z=eval(T)
2B. Verify the following quantities by using the symbolic math. Capture the
calculation results.
∫ √
∑( ) ( )
( )
3.A Follow the steps below and learn how to create and execute an m-file.
Step 1. Open a new script file editing window.
Step 2. Shown in the box below is an m-file that plots y = x sin(ax), for the
cases of a = 0.1, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7 and 0.8 over the range 0 < x < (10 + D),
where D = the last digit of your student ID number. Write this m-file and save it as
Lab1 3A.m. The m-file name must begin with a letter, files with a name that
begins with a number will not executed in MATLAB. Be sure not to use a space or
mathematical operator in the file name.
clear
x=0:0.1:(10+The last digit of your student ID number);
for n=1:8
a=n/10;
if (a==0.2)
a=0.25;
end
y(n,:)=x.*sin(a*x);
end
plot(x,y)
xlabel('x')
ylabel('y=x sin(ax)')
legend('a=0,1','a=0.25','a=0.3','a=0.4','a=0.5','a=0.6','a=0.7','a=0.8')
grid on
3.A-1 Add a comment to explain each line in the m-file. Capture the commented
m-file.
3.A-2 Execute the m-file you have created. You can either click 'run' button in the
menu bar of the m-file editor or press the F5 key on the keyboard or type the m-
file name in the command window as
>>CH1 3A
Laboratory 1: Introduction to MATLAB 1.6
3.A-3 Execute the following commands in the MATLAB command window. Based
on the results, document the meanings of the two variables. Do not capture the
execution results.
>>x
>>y
4.A Let us write a MATLAB function that converts a number in linear scale into dB
scale. In MATLAB, open the script file editor and write the following m-file. Save
the m-file as lin2dB.m (if you click "save," the default file name will be lin2dB.m).
function xdB=lin2dB(x)
xdB=10*log10(x);
4.B Let us write a MATLAB function that plots the Gaussian probability density
function.
Laboratory 1: Introduction to MATLAB 1.7
4.B-1 Write the following m-file and save it. Add your comments explaining what
each line does or means.
4.B-2 Execute plot.gaussian(0,1) in the command window and capture the result.
4.C Write a user-defined function swap(A,row0col1,c,d) that swaps two rows (or
columns) of a matrix. If row0col1 is 0, then swap(A, row0col1,c,d) swaps the c-th
row and the d-th row of a matrix A and returns the swapped matrix. If row0col1 is
1, then swap(A,row0col1,c,d) swaps the c-th column and the d-th column of a
matrix A and returns the swapped matrix.
function e=swap(A,row0col1,c,d)
e=A;
if row0col1==0
e(d,:)=A(c,:);
e(?,:)=A(?,:);
end
if row0col1==1
??;
??'
End
4.C-2 Execute the following command lines and capture the results. Check
whether or not your swap function works correctly.
>>x=rand(4,5)
>>y=swap(x,0,2,4)
>>z=swap(y,1,5,1)
Laboratory 1: Introduction to MATLAB 1.8
Rubrics
Punctuality Rubrics No participation Average Excellent
(0 points) (1 point) (2 points)
Attendance T> 15min 07<T<15 min T< 7min
Reaching time Or Absent
Rule violation Any rule violation would be given -1 mark each time
To be observed each time and would be deducted from the total marks earned.