Docsity

Introduction to Scientific Computing and Problem Solving: Lecture 6 - Plotting, Lab Reports of Computer Science

Partial preview of the text

Download Introduction to Scientific Computing and Problem Solving: Lecture 6 - Plotting and more Lab Reports Computer Science in PDF only on Docsity!

Introduction to

Scientific Computing and

Problem Solving

Lecture # 6

Plotting

Announcements

  • Assignments
    • Review lecture slides
    • Study examples and corresponding M-files
      • Work on your own examples!
    • Get help when needed!
      • TA hours
    • Ask questions if some concepts are not well understood!
  • Please always check the course web page
    • Announcements, updated documents, lecture materials…
  • No labs but regular TA hours over Long Weekend! :)
    • lab 3 = []; TA_hours_during_long_weekend = true;
  • HW#2 due on Thu 2:30pm (^) Now 11:59pm!

Data types

  • So far: double , char , logical
    • Some more to come…
  • They determine the type of data stored at

variables

  • How much memory space is used
  • How data is represented
  • What types of operations can be applied to data
  • Built-in conversion functions
  • E.g., num2str , str2num , int2str , char , double
  • Often: source of errors
  • E.g., assignments of inconsistent data types

Motion of a Projectile

  • Laws of motion

describing the

trajectory of an object

with initial velocity of v

0

m/s at an angle of !°

  • Appears in two of the

problems of HW#

Motion of a Projectile (cont.)

  • Total flying time: solve for y = 0
  • Horizontal velocity is constant and position is hmax y y tot t g v y v t gt t 2 2 0 or 2 (^1 ) 0 =! = = = x v t 0 x =

Linear indices

  • m " n array
    • Elements are stored in column major order
    • One-dimension alternative indexing
    • A=[0 2 3;4 5 0]; A(5) is equal to A(1,3)=
  • Some functions use linear indices
    • Referencing to elements of arrays according to

their column major order

  • E.g., find(A) returns column vector [2 3 4 5]

If-end

if

Commands

True

False

...| MATLAB commands ...| if conditional expression ...| ...| MATLAB commands ...| end ...| ...| MATLAB commands ...| m-files: Ex 5 _ 2

If-else-end

if

Commands Group 1

True

False

...| MATLAB commands ...| if conditional expression ...| ...| Group 1 commands ...| else ...| ...| Group 2 commands ...| end ...| ...| MATLAB commands ...| Commands Group 2

If-elseif-else-end

if Commands Group 1 True False ...| ...| MATLAB commands ...| if conditional expression ...| ...| Group 1 commands ...| elseif conditional expression ...| ...| Group 2 commands ...| else ...| ...| Group 3 commands ...| end ...| ...| MATLAB commands ...| Commands Group 2 elseif Commands Group 3 False True In general: elseif can appear 0 or more times and else at most once) Nesting is possible: conditional statements are commands themselves!

switch

  • Expression is scalar or string
  • Many cases can be included
  • Statements associated with first matching case are executed
  • If no matches, otherwise group is executed
  • At most one group (block) is executed
  • Each case may include more than one value to test matching: { valueA , valueB } switch switch expression case value 1 ...| ...| Group 1 commands ...| case value 2 ...| ...| Group 2 commands ...| case value 3 ...| ...| Group 3 commands ...| otherwise ...| ...| Group 4 commands ...| end m-files: lect 5 _ 12

plot Command

  • plot(x,y)

where x and y are vectors with same number

of elements

  • plot(x,y,'line specifiers',

'PropertyName', PropertyValue)

  • Line Specifiers
    • Style: solid, dashed, dotted, dash-dot
    • Color: red, green, blue, cyan, etc.
    • Marker: plus, circle, asterisk, point, square, etc.
    • doc plot m-files: lect 6 _ 1

plot Command (cont.)

  • plot(x,y,'line specifiers',

'PropertyName', PropertyValue)

  • Property Name and Value
    • LineWidth
    • MarkerSize
    • MarkerEdgeColor
    • MarkerFaceColor
    • doc plot m-files: lect 6 _ 2

Plotting Functions

  • fplot command fplot('function','limits','line specifiers')
  • Function typed as a string using any letter as the

independent variable

**' 8 *x^ 2 + 5 *cos(x)' ' 8 t^ 2 + 5 cos(t)'

  • Can't include any previously defined variables.
  • Limits is a vector [ xmin , xmax ] or [ xmin , xmax , ymin , ymax ]
  • Example
4 sin( 2 ) 1 for 3 3

2

y = x + x " "! x! m-files:^ lect^6 _^3

Multiple Graphs

  • Using plot command
    • If x, y, u, v, t, h are pairs of vectors of equal length

plot(x,y,u,v,t,h)

  • Example
    • A function and its first and second derivatives.
    • A derivate describes how a function changes as its

dependent variable changes (e.g., position, velocity,

acceleration)

y x

y x

y x x

2 3

m-files: lect 6 _ 4