Here are some example programs we had

Pascal Triangle for P6.m of exam 1

Finished the Monte Carlo pi claculation on Control flow Notes 1, moved to Control Flow Notes 2 up to the Switch Statement.

Exam (1) during the lab time

Went through Control Flow Lecture (1), wrote the program for the quadratic equation. Started talking about the Monte Carlo pi calculation. Finished drawing a full circle.

Exam (1) Assignment was given due next Tue before exam. Exam (1) Assignment

Nested loop

Nested loop with conditional statement

Here are some example programs we had

Looked at the if end, if ifelse end examples. Defined our first function.

Click here to see the programs Feb 8 & 10

Click here to see the programs How to conduct a sum with a for loop

Looked at the if end, if ifelse end examples. Defined our first function.

Click here to see the programs Thursday Feb 3

Conditional Statements, Program Flow Control

Think of a flow chart for this:

Assign two values to x and y

if y is not 0, divide x by y, otherwise do nothing

x = 3

y = 4

if y ~= 0 % y ~= 0 is conditional statement

z = x/y

end

The conditional statement produces true (1) or false (0). As you can see in the above example (y ~= 0) the TRUE case is where y IS NOT 0 and the FALSE case in where y is 0.

Another example was when you have an else case:

Assign two values to x and y

if y is not 0, divide x by y, otherwise display a message that Division by 0 is not possible.

x = 3

y = 4

if y ~= 0 % y ~= 0 (y not equal 0) is conditional statement

z = x/y

else % this is the default direction

disp('Division by 0 is not possible.');

end

An interesting example was this one:

if x > y, divide x by y if y is not 0

if y > x, divide y by x if x in not 0

In this case, we tried two different approach:

1) we checked first to see whether x is larger than y, then checked for y not being 0, and did the same for y larger than x.

2) we first checked to see whether either x or y (one of the two) is zero, and make a decision on where to go next based on that.

We talked about how we created 1-D and 2-D matrices.

a = [1 2 3 4]

b = 2*a will produce another 1-D array as [2 4 6 8], note that all members are multiplied by 2.

x = [2 4 5 ; 8 0 9] produces a 2-D matrix, as

2 4 5

8 0 9

Or you could do this:

x = [2 4 5 8 0 9]

which also produces:

2 4 5

8 0 9

We developed an algoritm on paper and draw a diagram for searching for a letter within a word (string). For example searching for letter 'l' in word Hello. The chart is in the Problem Solving notes. We mentioned that it was important that we check to make sure the string was not blank.

We learned about rand function and learn how to create a set of random integers using randi function and a set of float random number using the rand function. We learned that if we want to crate a set of float numbers between any arbitrary min or max (low or high) we could use x = min + rand*(max - min), thus to create a single number between min = 17 and max = 22, we could use: x = min + (max - min)*rand

if we wanted to creat n random numbers within that range we could use:

x = min + (max - min)*rand(1,n) where n is the number of data you want to create. For example to get 10 numbers between 17 and 22 we could use:

x = min + (max - min)*rand(1,10) as a row or

x = min + (max - min)*rand(10,1) as a column

We talked about types of variables, for now intgers, float, and double and mentioned several functions such as floor, ceil, round, and fix that will convert a float value to its integer equivalent by treating the decimal point differently.

Arrays are like containers of various size with some number of partitions that keep the values of certain types. An example of an array was the time array we worked with last Thursday:

t = [0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1];

The array t kept 11 values of type float that represented time at 10 intervals. We know time at the first location in the array (container) t is t(1) which is 0. At the 5th location in the array, i.e. t(5), we kept 0.4.

You can actually change the content of a location in the array. For example you can make the fifth location hold 0.45 instead of 0.4 if you wanted to, in this case you could simoly say: a(5) = 0.45.

An array also can keep characters of a word. For example,

word = ['H', 'e', 'l', 'l', 'o'];

nextWord = ['W', 'o', 'l', 'l', 'd'];

Oops, I had ann error, I actually wanted to say World. No problem, we can fix this easily. The error happened in the 3rd location in the nextWord array, so I can say netWord(3) = 'r';

and you can test that the correction is made.

Here is something interesting you can do. What if you to save in the first one "Hello you". You can add a you at the end of Hello, or you could write the entire Hello you at once.

word = ['H', 'e', 'l', 'l', 'o', 'y', 'o', 'u'];

OR even this will work:

word(6) =' '; % adding a blank space at location 6

word(7) ='y'; % add y in location 7

word(8) ='o'; % add o in location 8

word(9) ='u'; % add u in location 9 or you could write a new word, new = [' ', 'y', 'o', 'u']; and then connect the first and this new one to make the new word.

word = ['H', 'e', 'l', 'l', 'o'];

new = ['y', 'o', 'u'];

word = [word, ' ', new]

Note that you learned to put blank space in the array too.

There is a way to create containers that have rows and columns. We call this type of containers, matrix. The type of container which has rows and columns are called 2 dimenssional matrix. Here is an example:

A = 4;

twoD = [A A A ; A A A]

This generates two rows of As in three columns.

also, it is possible to put any character in a matrix:

anotherTwoD = ['a' 'a' 'c' 'c' ; 'c' 'c' 'a' 'a']

Quiz (1), Microlab (1)

Wrote a small program in MATLAB, documentation is important

a must be included in your programs. Below you will see a sample

program we wrote in class to find the height of a projectile.

Comments are marked with % at the beginning of the lines

%RT

% my script to compute height in projectile motion

h0 = 0 ; % initial height

v0 = 40; % meter per second

g = 9.8; % meter per second^2

theta = 45; % degree

% define time

t = [0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1];

% or define t this way, same thing

t = [0: 0.1: 1];

%

% computing height of the object at t

h = (-0.5)*g*t .^ 2 + v0*sind(theta)*t + h0;

% show the time and height in table format

[t' h']

%

% plot height vs time

plot(t' , h')

Accessing MATLAB on the PCs in the lab and on own PCs

Talking about variables, how they are defined. Problem Solving

Lab (2)

Lab (1)

Introduction, Syllabus, Computer Usage Policy, discussion about the lab

Textbook discussions

Problem Solving Strategies, Microlab 1