Class Activities for CS1445 - Spring 2010

Mon Mar 29
Calling a function properly
In the following code segment, I wanted to compute y = sqrt(x) + 1. What is the difference between the two methods that are used in the main? i.e. between: myx1 = sqrtPlusOne1(x)

sqrtPlusOne2(x)

% Main program
clc
clear all
%
x = input('Enter Positive value for x: ');
x = abs(x);

myx1 = sqrtPlusOne1(x)

sqrtPlusOne2(x)
x

------------------------------------------
function y = sqrtPlusOne1(x)
y = sqrt(x) + 1

------------------------------------------
function sqrtPlusOne2(x)
x = sqrt(x) + 1

Fri Mar 5
Quiz (7)
Reading a text file and writing to a text file

% For Problem 2 Quiz (7)
% % Read the content of an inout file and convert
%
clear all
clc
fin=fopen('Q7input.txt');
text = fscanf(fin,'%c');
text
fclose(fin);

Fri Feb 26
Exam(1) - Lab Part during class
Ok Exam (1) the Lab part is behind us. I see major problems with understanding the application of for loops . Here is a few examples that should help:

How Summation and for loop are connected

Wed Feb 24
Exam(1) - Written Part during class

Mon Feb 22
We went through the last few slides at the end of the Control Flow (1) Lecture notes.
We learned how Boolean expressions are evaluated to 0 or 1 depending on the evaluation being true or false recpectively. For example:
if a = 2, b = -3, and c = 0, then
(a < b) | (a > c) would evalaute to 1, as a > c.
Then we solved the Quadratic Formula by:
Analaysing the problem depending on the value of a, b, and c. Then we designed an algorithm to solve it and drew a diagram, and then wrote a program. At the end we created test cases (different values of a, b, and c) to make sure all aspects of the program work correctly.

Fri Feb 19
Quiz(6)
Nested loops

Wed Feb 17
Snowday

Mon Feb 15
We went through the solution for Pre-Lab (5)
Pre-Labs Solutions and Programs
For loops
FAQ for MATLAB

Fri Feb 12
Quiz(5)
for loop and if statements together

Wed Feb 10
No Class due to snow, Please do Lab (5) on your own

Mon Feb 8
Solving Quiz(4)

Fri Feb 5
No class due to snow

Wed Feb 3
Finishing the Introduction to Programming in MATLAB (2)
Start the Control Flow notes

Mon Feb 1
Finished the Introduction to Programming in MATLAB (1)
Start the Introduction to Programming in MATLAB (2)
We talked about arrays and how each member of the array is recognized by the index of the cell in which that member is stored in the array. For example in the array:
a = [2 4 6 8]
We refer to 6 by its index in a, as a(3).
We also talked about how the entire row or column in an array can be addressed using the index. Here is an example:
a = [2 3 4; 5 6 7; 8 9 10]
a(1,:) refers to all the members in row1, i.e. [2 3 4]
and a(:,2) refers to all members of column 2, i.e.
[3
6
9]

We mentioned that if you want to replace the entire row or column of a matrix, use this type of indexing. For example, to multiply row2 by 3 and update the array a with new values, we use:
a(2,:) = a(2,:)*3
if you display a now, it will have the new values:
[2 3 4; 15 18 21; 8 9 10]
We mentioned that there are pre-defined functions that produce arrays or matrices as their return value. For examplei function clock returns 6 members:
myClock = clock
HELP -- CLOCK = [year month day hour minute seconds]
Then we can access each of the members using the index of the element. For example if you wish to find the year, then you can use:

We talked about scaling and how we can scale from one range to another. For example if we want to scale numbers that are in the range 10-100 to a range 1 to 5. We came up with the generic formula as:
y = YL + (YH - YL)*(x - XL)/(XH - XL)
where x's are for the old range and y's represent the new range. Thus
XL and XH represent the min and max values for the old range
and YL and YH represent the min and max values for the new range.
Also, x is the value in the old range we wish to map and y is its equivalent in the new range.

Friday Jan 29
Quiz(3)

Wed Jan 27
Lecture Note (2)
Lab(3)

Mon Jan 25
Talk about the post-lab for Lab (1), request re-submission by the end of today.
Briefly talk about Quiz(2), notes are posted on Friday's activity.
Finishing Lecture (1) notes
Starting Lecture(2) notes

Fri Jan 22
Quiz (2) was given
We learned about the submit command that we will use to submit our programs using this command on our Linux machine. In general, every time you want to work on a lab or quiz, create a directory for that activity and complete your work there. For example to do quiz2, you went to directory 1445 (cd 1445) and there you created quiz (mdkir quiz2). Then we went to that directory (cd quiz2) and complete Quiz (2) in file we called quiz2.m. Then we asked you to submit the file quiz2.m using:
submit bs quiz2 quiz2.m

Here are some of the things we had on Quiz(2)
Use proper name for a variable. For example number of books:
no_of_books = 7
is good. Do not use no.of.books, it wont work. Also, you cannot use no-of-books, the - will be seen as minus sign. Using a number first is not a good idea either, so 2rd = 7, is not good.
We learned that naming a variable with the same name as the name of a pre-defined function will inactivate that function. Here is an example:
sqrt = 4; % here we named a variable sqrt
sqrt(25) % here we are calling the pre-defined function sqrt
??? Index exceeds matrix dimensions.
So you get an error as sqrt( ) function is no longer recognized. To correct this problem you can use the clear command.
clear sqrt

We learned to define an array of integer and called it rl
% a)
>> rl = [2 4 6 8]

We learned that we can multiply an array by a constant and when we do this all members of the array gets multiplied by that constant. So, if we do 2*rl:
% b)
2*rl
then we will get:
4 8 12 16

We created another array of integers of the same size and we called it im
% c)
im = [2 -3 0 6]
Note that these are 4 integers and there is no signifcance to them.

%d)
We then created a complex array that had rl as its real part and im as its imaginary part.
complex = rl + i*im
Note that by multiplying im by i, we indicated that that part is the imaginary part. Having im as the name for this variable was for convenience only. We could have done this as im + i*rl which would produce another set of complex number. So, by naming the variable rl and im, I had a note to ourselves which part is which, as theoretically these two are bunch of integers.
Also note that we named the variable complex, which happen to be the name of a pre-defined function complex, real, imaginary). But it is now inactive. We could have created a complex number using the complex function as: using
comp = complex(rl, im)
which would produce the same result as complex = rm+i*im. But this

We learned to compute the complex conjugate of a complex number by using the conj pre-defined function.
e)
conj(complex)

We used the abs() pre-defiend function to compute the magnitude of the complex value.
f)
abs(complex)

g)
We could have used a different technique to compute the magnitude: using
sqrt(rl .* rl + im .* im) or even better using

sqrt(rl.^2+im.^2)
Or this:
complex .* conj(complex)
Quiz (2) contained several important concepts that we described above.

Wed Jan 20
Continued with Lecture (1) slides - Problem Solving & Programming
How a computer works, how a program gets converted to something the hardware on your computer understands.
Talk about memory (primary and secondary) and the unit of storage
Talk about bytes, bits, and how they are used in computers

Mon Jan 18
MLK Holiday

Fri Jan 15
Quiz (1)
Problem Solving and Programming
How labs are done, MATLAB questions

Wed Jan 13
Introduction to Problem Solving and Programming
What is problem solving and why it is always mentioned with Programming
Lab on Wed 2:00-4:00 Room 439

Mon Jan 11
Introduction, Syllabus, Computer Usage Policy, discussion about the lab
Textbook discussions