UNIVERSITY OF CALIFORNIA, BERKELEY
Engineering 7 – Spring 2009
Department of Civil and Environmental Engineering
Instructor: Professor Rector
1
Lab 02
Topics
:
Functions, Switches, Conditionals
Assigned
:
Friday, 02/06/2009
Due
:
Friday, 02/06/2009 at 12:00 pm
Type
:
Take Home
Remember to use the
help
command if you are not familiar with a command. It will be much
faster than waiting for a GSI to answer your question. Moreover, a list of useful commands is
provided at the end of this document.
For this lab, you may submit your report as one Word document or one PDF file. Remember to
submit both a hard copy of your report and an electronic copy on Bspace. Thanks!
1. Link Between colon Notation and Array Manipulation (revisited)
The
start:increment:end
notation and the
colon
notation for arrays are similar. Create the 5x5 identity
matrix, store it in
Z
and use the
start:increment:end
notation to extract the rows 1, 3 and 5 of
Z,
but only
the elements in the 2
nd
and 4
th
column of those rows. Copy the commands you typed into your report, as well as the
output.
You should only need 1 line of code to create the identity matrix, and 1 line of code to retrieve the values
from
Z
.
2. Inputs and Outputs of Functions: An Exercise in Output Prediction
The purpose of this exercise is to make you more flexible with how you treat MatLab functions.
In this
exercise, make sure you identify the important parts of a function and recognize different ways of calling
the function.
Also please note that commenting for all exercises in the future is strongly encouraged; it is
both helpful for us to grade your work and good programming practice.
(a)
Read the function below.
Rewrite the function’s code in MatLab, but with complete comments as
indicated.
function [distance, X, Y] = dist_plot(x1,y1,x2,y2)
% The name of the function is < put function’s name here >
% This function’s 4 inputs are the variables:
%
< define each of the 4 input variables here! >
% The function’s 3 outputs are the variables:
%
< define each of the 3 output variables here! >
% < briefly describe the purpose of the following 2 lines here >
distance = ((x1-x2).^2 + (y1-y2).^2) ^.5;
['The distance between the two points is ' num2str(distance)]
% < briefly describe the purpose of the following 3 lines here >
X = [x1,x2];
Y = [y1,y2];
plot(X,Y,'ro-');
% < briefly describe the purpose of the following 4 lines here >
grid on;