UNIVERSITY OF CALIFORNIA, BERKELEY
Engineering 7 – Spring 2009
Department of Civil and Environmental Engineering
Instructor: Professor Rector
Functions, Switches, Conditionals
Friday, 02/06/2009 at 12:00 pm
Remember to use the
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)
notation and the
notation for arrays are similar. Create the 5x5 identity
matrix, store it in
and use the
notation to extract the rows 1, 3 and 5 of
the elements in the 2
column of those rows. Copy the commands you typed into your report, as well as the
You should only need 1 line of code to create the identity matrix, and 1 line of code to retrieve the values
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.
exercise, make sure you identify the important parts of a function and recognize different ways of calling
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.
Read the function below.
Rewrite the function’s code in MatLab, but with complete comments as
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];
% < briefly describe the purpose of the following 4 lines here >