1

 Goals
 Learn programming fundamentals
 Learn how to create Mfile programs

2

 Developing an algorithm
 Program flow control
 Simple Mfile MATLAB programs

3

 A program is a sequence of actions to solve a problem.
 This sequence of actions is called an “algorithm”
 An algorithm states:
 the actions to be executed
 the order in which these actions are executed

4

 An algorithm for getting out of bed and going to work

5

 Implemented using an algorithm to solve the computing problem
 Often useful to state algorithm as “pseudocode”  an informal statement
of the actions and the sequence of those actions (algorithm) to solve
the problem

6

 As a starting point write down what you know
 what are the inputs?
 What is the desired action of the program?
 what outputs need to be produced?
 Use pseudocode to develop the algorithm

7

 Develop a MATLAB program that will create a window with two graphs of a
set of data contained in two arrays x and y. The left graph is a linear plot of the
data and the right graph is a loglog plot of the data.

8

 Write down what is known
 Inputs  two arrays x and y with the x and y values of the data to be
plotted
 Desired action  create two graphs in one window  one linear and one
loglog
 Outputs  the graph window

9

 MATLAB actions needed to solve the problem as pseudocode
 1. Create a graph window with 2 graphs
 2. Activate the left graph
 3. Plot y versus x as a linear plot
 4. Activate the right graph
 5. Plot y versus x as a loglog plot

10

 After developing the algorithm, the actual MATLAB Code needs to be
written

11

 Rarely do programs follow a linear sequence of actions as in the example
 Typical programs contain
 decision paths 
 do a sequence of actions A if a certain condition is true, otherwise
do a sequence of actions B
 and loops
 repeat a sequence of action a given number of times

12

 Selection Structures:
 If, if/else structure
 Switch structure
 Repetition Structures
 In this session we will talk about Selection Structure

13

 Simple decision
 if condition then seq_A else seq_B
 if condition then seq_A

14

 MATLAB relational expressions
 MATLAB logical expressions

15


16


17


18

 Goal : Create an Mfile for calculating the function shown below where t
is an array input and w is a scalar.

19

 What is known
 inputs: t  an array of values, w
 a scalar
 desired action  calculate one of two equations based upon value of w
 output  return the value of the calculated equation
 Algorithm
 if w <= 1 calculate 1st equation else calculate 2nd equation

20

 Create an Mfile function named myfun.m using the MATLAB editor.

21

 Plot the function over the range 0 to 20 for w = 1 and w = 2.

22

 Write a function to calculate and display the letter grades of all
students.
 Input to the function: grades of homework, lad and exams.
 Output: letter grade

23

 Based on the weights given, and the curve as follows:
 >=90 and <=100: A
 >=80 and <90: B
 >=70 and <80: C
 >=60 and <70: D
 <60: E
