You are on page 1of 7

# E7: Introduction to Computer Programming for Scientists and

Engineers
University of California, Berkeley, Fall 2017
Instructor: Michael Frenklach

Lab 4
Due: October 6, 2017 at 11:59 pm

The purpose of this lab is to develop familiarity with cell arrays, structures, tables, plotting and
debugging.
Use only functions contained in the basic MATLAB installation; do not use functions from optional toolboxes
(e.g. curve-fitting toolbox, optimization toolbox, etc.).
For this assignment, please submit the following to bCourses: function files addGameCell.m,
addGameStruct.m debugPlot.m in a zip-file named lastname firstname hw4.zip.

## Exercise Problem 1: Cell arrays

Cell arrays are arrays that can store arrays and/or values of different datatypes, and are very useful. Here
we will create a cell array that keeps track of the California Golden Bears 2017 football season to date.
Cals results through last week are shown in the table below.

Home Team Away Team Home Team Score Away Team Score Winner
UNC Cal 30 35 Cal
Cal Weber State 33 20 Cal
Cal Ole Miss 27 16 Cal
Cal USC 20 30 USC

Create a cell array that incapsulates the data above in the same order. When complete, your cell array
should look like the following:
>> CalSchedule

CalSchedule =

3 x5 cell array

## UNC Cal [30] [35] Cal

Cal Weber State [33] [20] Cal
Cal Ole Miss [27] [16] Cal

## Add this Saturdays Oregon game if you are so inclined.

This exercise will not be graded.

1
Homework Problem 1: Cell arrays

## Create a function with the following header:

function [ schedule ] = addGameCell ( schedule , hometeam , awayteam ,
homescore , awayscore )

where: schedule is a cell array with the same format as CalSchedule in Exercise 1 that holds the current
data (home team, away team, home score, away score and winner) and will be expanded to include a new
game, hometeam is a character array of the home team, awayteam is a character array of the away team,
homescore is the home teams final score and awayscore is the away teams final score.
Your function should be able to reproduce the following sequence:
>> calSchedule = addGameCell ({} , UNC , Cal , 30 , 35)

calSchedule =

1 x5 cell array

calSchedule =

2 x5 cell array

## UNC Cal [30] [35] Cal

Cal Weber State [33] [20] Cal

calSchedule =

3 x5 cell array

## UNC Cal [30] [35] Cal

Cal Weber State [33] [20] Cal
Cal Ole Miss [27] [16] Cal

## Create a function with the following header:

function [ schedule ] = addGameStruct ( schedule , hometeam , awayteam ,
homescore , awayscore )

2
where: schedule is a structure (with fields hometeam, awayteam, homescore, awayscore, winner) that
holds the current data and will be expanded to include a new game, hometeam is a character array of the
home team, awayteam is a character array of the away team, homescore is the home teams final score and
awayscore is the away teams final score.
Your function should be able to reproduce the following sequence:
>> calSchedule = addGameStruct ( struct , UNC , Cal , 30 , 35)

calSchedule =
struct with fields :
hometeam : UNC
awayteam : Cal
homescore : 30
awayscore : 35
winner : Cal

## >> calSchedule = addGameStruct ( calSchedule , Cal , Weber State , 33 , 20)

calSchedule =
1 x2 struct array with fields :
hometeam
awayteam
homescore
awayscore
winner

## >> calSchedule = addGameStruct ( calSchedule , Cal , Ole Miss , 27 , 16)

calSchedule =
1 x3 struct array with fields :
hometeam
awayteam
homescore
awayscore
winner

## >> calSchedule (3)

ans =
struct with fields :
hometeam : Cal
awayteam : Ole Miss
homescore : 27
awayscore : 16
winner : Cal

## For this problem submit the file addGameStruct.m.

3
Exercise 2: Tables

Create a table that stores the same information as the cell array in Problem 1 and the structure in Problem
2.
This exercise will not be graded.

Exercise 3: Plotting

Waves are important for every discipline of engineering. When considering sampled data of waves, a common
problem is aliasing, which is when a wave appears to have a longer period due to infrequent sampling.
Visualize this effect for yourself by plotting a simple sine wave from 0 to 12 radians using 10, 20 and 200
points on the same plot. You will notice that the 200-point wave appears to be completely smooth, the
20-point wave does not have a high enough sampling frequency to accurately capture the wave but is not
aliased, and the 10-point wave appears to have a period of 4 (rather than 2).
Your result should look identical to fig. 1, including title, axis labels and legend.

## This exercise will not be graded.

4
Exercise 4: Subplots

With this assignment you are given a MATLAB data file dx0025 203m.mat which contains three two-
dimensional arrays, udata, vdata, wdata, which are the three velocity components ~u = (u, v, w) for a
horizontal plane (203 meters above the surface) in a turbulent atmospheric simulation. The horizontal res-
olutions x and y are both 25 meters (i.e. the distance between each point and its neighbor in either the
x- or y-direction is 25 meters).

## Create a function with the following header:

function [] = p l o t V e l o c i t y C o m p o n e n t s ( plottingFunction , u , v , w , dx , nx )

where: plottingFunction is a handle to the type of plot to be used, u, v, w are the three two-dimensional
arrays to be plotted, dx is the horizontal resolution of the data (assume x = y ) and nx is the number of
points to plot (assume the same number of points are used in x- and y-directions).

You can load the data in the .mat file to your workspace using the command load dx0025 203m.mat. The
input data should be loaded before calling your function and passed as arguments; the file
should not be loaded inside the function.
Your figure should have three subplots, one for each velocity component (see figs. 2, 3). Each subplot should
have a title, labels on both axes and a colorbar (with a label).

Figure 2: u-, v- and w-velocity contours of data from dx0025 203m.mat using plottingFunction =
@contourf, dx = 25 and 100.

Figure 3: u-, v- and w-velocity contours of data from dx0025 203m.mat using plottingFunction =
@contour, dx = 25 and 500.

## This exercise will not be graded.

5
Homework Problem 3: Debugging

You are given a file debugPlot.m that is trying to produce a 3D surface plot with contours below the surface
(using surfc) of

## f (x) = sin(x) + cos(y) + sin(20y) + sin(x + y), (1)

however it has some errors. Debug the code so that the code runs without errors and produces a figure that
matches fig. 4.

The function should take the number of points to use in both x and y as a single input argument (square
domains only) and return the z data being plotted. There should be a point every 0.1 radians in x and y
Currently, the function is:
function [ z ] = debugplotting ()

dx = 0.1 pi ;

x = linspace (0 , nx * dx , nx ) ;
y = linspace (0 , nx * dx , nx ) ;

## wave1 = makewave ([1 ,0] , sin ) ;

wave2 = makewave ([0 ,1] , cos ) ;
wave3 = makewave ([200 ,20] , sin ) ;
wave4 = makewave ([1 ,1] , sin ) ;

## z = wave1 + wave2 + wave3 + wave4 ;

surfc (x ,y , z ) ;
title ( Some Waves )
xlabel ( x )
ylabel ( y )
zlabel ( amplitude )

## For this problem submit the (working) file debugPlot.m.

6
Figure 4: Plot of waves generated by the function debugWaves for nx = 100.