Introduction To Matlab Class 2 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Introduction To Matlab Class 2

Description:

Introduction To Matlab Class 2 Instructors: Hristiyan (Chris) Kourtev and Xiaotao Su, PhD Double click the matlab icon When prompted click Skip – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 18
Provided by: Robert1971
Category:

less

Transcript and Presenter's Notes

Title: Introduction To Matlab Class 2


1
Introduction To MatlabClass 2
  • Instructors Hristiyan (Chris) Kourtev
  • and Xiaotao Su, PhD
  • Double click the matlab icon
  • When prompted click Skip

2
Character Vectors And Numeric Vectors
  • 'hello world' is referred to as a character
    string or a character vector
  • 35, 26.3, 11, 4 is a numeric vector
  • These are the two main data types in matlab

h e l l o w o r l d
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
35 26.3 11 4
(1) (2) (3) (4)
3
Character Vectors (aka Strings) And Numeric
Vectors
  • How to convert between these two data types? What
    if I have a string, '39.5' and I want to add to
    it?
  • num2str str2num
  • str2num('39.5') 5
  • 'hello w', 'orld'
  • a 3, 9
  • b 12.6, 8
  • a, b
  • c '1', num2str(39.5)
  • str2num(c) 200

4
Formatting strings with sprintf
  • result sprintf(format_string, input1, input2 )
  • Format string flagswidth.precisionlength
    specifier
  • Common Specifiersc characters
    stringd integere or E scientific
    notationf floating pointm 44.5 k hello
    worldres sprintf(s, it is d degrees
    outside, k, m)res will be hello world, it is
    44.5 degrees outside

5
Formatting strings with sprintf
  • Flags- left justify default is right0 left
    pads the number with zeroes if padding specified
  • Width just a number
  • Precision just a number
  • Examplessprintf('01.2f', 12.4) outputs
    12.40sprintf('010.2f', 12.4) outputs
    0000012.40sprintf('.3e', 362525200) outputs
    3.625e8
  • For further reference go tohttp//php.net/manual
    /en/function.sprintf.php http//www.cplusplus.com
    /reference/clibrary/cstdio/sprintf/

6
Taking inputs
  • Create a new program called math.m
  • math.m
  • Carries out a series of calculations on two
  • numbers
  • written by Your Name July 2010

7
  • clear all
  • take inputs
  • num1input('what is the first number?')
  • num2input('what is the second number?')
  • show calculations
  • disp(num2str(num1), '', num2str(num2), '',
    num2str(num1num2))
  • disp(num2str(num1), '-', num2str(num2), '',
    num2str(num1-num2))

8
boolean expressions
  • Boolean expressions either return 1 for true or
    0 for false
  • Possible operators

gt gt lt lt
equals greater than greater than or equal to less than greater than or equal to and or
9
  • 11
  • (1 1)2
  • (5 5) lt 10
  • (5 5) lt10
  • (1 1) (23)
  • (1 1) (23)
  • 5 (5 lt 10)

Note Put operations inside parenthesis so they
are preformed first.
10
Conditionals (if statements)
  • if(Boolean Expression)
  • do something
  • end
  • you can also do this
  • if(boolean expression)
  • do something
  • else
  • do something else
  • end

or this if(boolean expression) do
something elseif(boolean expr2) do something
else end
11
Back to math.m
  • if ( (round(num1)num1) (round(num2)num2) )
  • disp('num1 and num2 are integers')
  • end

12
  • if ( (round(num1)num1) (round(num2)num2) )
  • disp('num1 and num2 are integers')
  • elseif(round(num1)num1)
  • disp('only num1 is an integer')
  • elseif(round(num2)num2)
  • disp('only num2 is an integer')
  • else
  • disp('neither number is an integer')
  • end

13
Task 1
  • Have math.m tell me which number is larger then
    the other
  • the biggest number is

gt gt lt lt
equals greater than greater than or equal to less than greater than or equal to and or
14
Tic and toc
  • tic starts a timer
  • toc tells you the number of seconds since when
    you ran toc

15
While loop
  • while is a type of loop that runs while a
    condition is true
  • while(boolean statement)
  • do something many times
  • end
  • you would stop repeating the loop when the
  • boolean statement is false OR if you use
  • break

16
While program
  • tic
  • loops 0
  • while(toclt5)
  • loops loops1
  • disp('number of loops ', num2str(loops))
  • disp('number of seconds ', num2str(toc))
  • end

17
While program stopping after 10 loops
  • tic
  • loops 0
  • while(toclt5)
  • loops loops1
  • disp('number of loops ', num2str(loops))
  • disp('number of seconds ', num2str(toc))
  • if(loopsgt10)
  • break
  • end
  • end
Write a Comment
User Comments (0)
About PowerShow.com