CS1132 Fall 2014 Assignment 2
due 11/20 11:59pm
Adhere to the Code of Academic Integrity. You may discuss background issues and general strategies
with others and seek help from course sta, but the implementations that you submit must be your own.
In part
Matrix vs. Cell Array
Vectors and matrices store values of the same type in all components A cell array is a special array whose individual components may contain different types of data
Strings in Cell Arrays
-4 c s 1 0 1 5x1 matrix 3 4 -2 3 .2 0
-1
. c
Array Addressing
Sub-vector
To access a value in a vector, use parentheses to enclose the index value. For example, x(2) is the value in the 2nd cell of vector x. To access a sub-vector, specify the index values using a colon notation. Type the following
Todays Lecture:
2-d arraymatrix
Function & subfunction
Details on for-loop (see blecture)
Announcements:
Friday: lab session in Upson B7
Assignment 1b due Tuesday 11:59pm
Test 1 on Wednesday in class; review on Monday.
c
2-d array: matrix
r
An array is a
Todays Lecture:
Cell arrays
File input/output
Announcements:
Lab 3 exercise is very important for A2 and Test2. Make
sure that you learn that material
Test 1 (second run, optional) Wednesday 2:30p
Assignment 2 due next Thursday at 11:59pm
Array vs. Cell
Transition to Matlab
CS1132 Fall 2013
K.-Y. Daisy Fan
Yue Gao
http:/www.cs.cornell.edu/courses/cs1132/
Todays agenda
Course goals
Organization and requirements
Logistics
Matlab desktop
Script vs. function
2
Course Goal
Learn how to program in Matlab!
Topi
Todays Lecture:
Vectorized computation
Introduction to graphics
Announcements:
Assignment 1a: due Tues at 11:59pm, at which time
submission on CMS will close. Will re-open for resubmission later.
Assignment 1b: due Tues Nov 5 at 11:59pm
1
Monte Carlo App
Todays topics
Loops
Conditionals
More on user-defined function
1-d array
Announcement/Reminder:
Assignment 1a is due Tues, Oct 29th at 11:59pm
Monte Carlo Approximation of
Throw N darts
L
L/2
Sq. area = N = L L
Circle area = Nin
= L2/4
2
Monte Carlo App
CS1132 Fall 2013 Assignment 1a
Adhere to the Code of Academic Integrity. You may discuss background issues and general strategies
with others and seek help from course sta, but the implementations that you submit must be your own.
In particular, you may d
CS1132 Fall 2013 Assignment 1b
Adhere to the Code of Academic Integrity. You may discuss background issues and general strategies
with others and seek help from course sta, but the implementations that you submit must be your own.
In particular, you may d
Todays Lecture:
Cell arrays
File input/output
Announcements:
Lab 3 exercise is very important for A2 and Test2. Make
sure that you learn that material
Test 1 (second run, optional) Wednesday 2/20 2:30p in
PHS 101
Assignment 2 due Thursday 2/28 at 11:59pm
CS1132 Spring 2013 Assignment 2
Adhere to the Code of Academic Integrity. You may discuss background issues and general solution strategies with others and
seek help from course staff, but the homework you submit must be the work of just you. When submitt
Avoid a Common Error in Using the for - loop
for k = 4:6 disp(k) k= 9; disp(k) end
4
5
6
Start of 1st pass: k takes the first value promised, 4 display 4 k gets 9 display 9
for k = 4:6 disp(k) k= 9; disp(k) end
4
5
6
Start of 1st pass: k takes the first v
Creating Arrays
1-Dimensional Array: Vector
An array is a named collection of data values organized into rows and/or columns. A 1-d array is a row or a column, also known as a vector. An index is a positive integer that identies the position of a value in
Todays topics
Loops
Conditionals
More on user-defined function
1-d array
Announcement/Reminder:
Assignment 1a is due Tuesday at 11:59pm
Monte Carlo Approximation of
Throw N darts
L
L/2
Sq. area = N = L L
Circle area = Nin
= L2/4
2
Monte Carlo Approximat
Todays Lecture:
2-d arraymatrix
Function & subfunction
Details on for-loop (see blecture)
Announcements:
Friday: lab session in Upson B7
Assignment 1b due Tuesday 11:59pm
Test 1 on Thursday in class; review on Tuesday.
c
2-d array: matrix
r
An array is a
Todays Lecture:
Cell arrays
File input/output
Announcements:
Lab 3 is very important for A2 and Test2. Make sure that
you learn that material
Test 1 (second run, optional) Wedensday 11/12
Assignment 2 due 11/20 at 11:59pm
Assignment 1b Regrade Saturday 1
CS1132 Fall 2014 Assignment 1a
due 9/9 11:59pm
Adhere to the Code of Academic Integrity. You may discuss background issues and general
strategies with others and seek help from course sta, but the implementations that you submit
must be your own. In parti
Review of topics for Test 1
n
Announcements/Reminders:
Assignment 1b due Nov 4th11:59pm
nTest 1 this Wednesday
th
nAssignment 1a regrade till this Saturday(8 )
n
Characters & strings
We have used strings already:
n= input(Next number: )
fprintf(Answer i
CS1132 Fall 2014 Assignment 1b Due Nov 4th 11:59pm
Adhere to the Code of Academic Integrity. You may discuss background issues and general strategies
with others and seek help from course sta, but the implementations that you submit must be your own.
In p
Vectorized code to support plotting
f ( x) =
Vectorized Logical Operations
sin( 5 x ) exp( x / 2) 1+ x2
for -2 <= x <= 3
x = linspace(-2,3,200); y = sin(5*x).*exp(-x/2)./(1 + x.^2); plot(x,y)
1 0.5 0 -0.5
-1
-1.5 -2
-1
0
1
2
3
Vectorized code to support p
Writing data to a text le
There are three steps to writing data to a le: (1) open the le, (2) write the data, and (3) close the le. Below
are two examples for writing data to a plain text le; the rst example deals with characters and the second
deals with
Reading text data from a le
There are three steps to reading text from a le: (1) open the le, (2) read in the data, and (3) close the le. Here is an example for reading a plain text le (ASCII characters) and storing the text in a cell array.
function C =
x r y
User-defined function
Polar coordinates Cartesian coordinates
Write a function that translates polar coordinates (r,) to Cartesian coordinates (x,y)
function [x, y] = polar2xy(r,theta) % Convert polar coordinates (r,theta) to % Cartesian coordinates
Approximation
Consider the infinite series
1+
n 1 1 1 +L+ 2 = 2 22 n 6 k =1 k
for - loop
How good is the approximation if n is 1000?
1 We need a program to calculate 2 k for k from 1 to 1000.
Syntax of the for-loop
Colon expression
for
index variable
=
st
CS1132 Lab Exercise 3
1
Not string but chars
In Matlab, there is the type char but not the type string. What we call a string is really an array of chars.
Type each of the following statements in the Command Window and note the result.
a= pi;
b= pi
% A nu