Docsity

ENGR 0012 Spring 2007 - Programming Concepts and MATLAB Review, Study notes of Engineering

Toggle sidebar
bg1
1
ENGR 0012 –Spring 2007
Thursday, January 4th
Dr. Lund
Course Website:
www.engr.pitt.edu/~eng12
My Website:
www.engr.pitt.edu/freshman/academic/
engr0715/engr0012.html
Programming (In a nutshell)
•Take input
(e.g. variable values, data, signals…)
•Process input
(Tools include math functions, decision
structures, while loops, for loops…)
•Generate output
(e.g. variable values, plots, control signals…)
Good Practices
•Know your programming language
•Comment, comment, comment
•Define problem and desired output
•Break problem down into primary components
•Write code in components and debug as you go
MATLAB Review Topics for Today
•While Loops
•Decision Structures
–IF –ELSEIF –ELSE
–SWITCH –CASE –OTHERWISE
•Loop Counters
•Array Indexing
•Plotting
pf3
pf4

Partial preview of the text

Download ENGR 0012 Spring 2007 - Programming Concepts and MATLAB Review and more Study notes Engineering in PDF only on Docsity!

ENGR 0012 – Spring 2007

Thursday, January 4

th

Dr. Lund

Course Website:

www.engr.pitt.edu/~eng

My Website:

www.engr.pitt.edu/freshman/academic/

engr0715/engr0012.html

Programming (In a nutshell)

• Take input

(e.g. variable values, data, signals…)

• Process input

(Tools include math functions, decision structures, while loops, for loops…)

• Generate output

(e.g. variable values, plots, control signals …)

Good Practices

  • Know your programming language
  • Comment, comment, comment
  • Define problem and desired output
  • Break problem down into primary components
  • Write code in components and debug as you go

MATLAB Review Topics for Today

  • While Loops
  • Decision Structures
    • IF – ELSEIF – ELSE
    • SWITCH – CASE – OTHERWISE
  • Loop Counters
  • Array Indexing
  • Plotting

While Loops

disp('Start of problem') a = 4 while a > 1 b = 2; while b <= a b = b + 1; disp(ab) end a = a - 1 end disp('End of problem')*

Nested while loop

Loop counters

(Notice how all loop counters must be initialized before the loop)

If - Elseif – Else Branching

a = [5 8 -4 -3 7 1 9]; N=0; while (N <= 5) N=N+ if ( N==1 | N > 6) Output = a(N) ; disp(Output) elseif ( (N == 3) | (N >= 5) ) Output = a(N+1); disp(Output) else Output = a(5); disp(Output) end end

Loop counter

Array Indexing

Switch – Case

Branching

v=[1 2 3 4 5 6 7]; k=5; while k > 0 switch (k+1) case {1,5} new=v(k)+v(k+1) case {4,6} new=v(k) case 2 new=- otherwise new=v(k+2) end k = k - 1; end

Array indexing

Loop counter

Array Indexing

array = [1 2 3 4 5 6 7]

Array indexing can be used to isolate specific

values from an array. Elements of an array

can be used or manipulated independently of

one another.

Subplots

(^00 2 4 6 )

2

4

6

8 Linear Plot

x

y

0 2 4 6 8

10 0.

10 0.

Semi-Log Plot

LN(y)

x 10

0

10 0.

10 0.

Log-Log Plot

LN(y)

LN(x)

  • Creates plots in tiled positions.
  • SUBPLOT(m, n, p), breaks the Figure window into an m-by-n matrix of small plots, selects the p-th axes for for the current plot.
  • The axes are counted along the top row of the Figure window, then the second row, etc.
  • For example,

SUBPLOT(2,1,1), PLOT(x,y) SUBPLOT(2,1,2), PLOT(x,z)

plots x vs. y on the top half of the window and x vs. z on the bottom half.

Subplot command

Subplot

  • Type help subplot in command window
  • Click on doc subplot link at bottom of help

output