Docsity

MATLAB Predefined Functions and Programming - Lecture Notes | BAE 200, Study notes of Agricultural engineering

Toggle sidebar
bg1
BAE 200
Week 3 Lecture notes:
9/9/08
MATLAB Predefined Functions and Programming (Engineering Analysis and
MATLAB7 - Chapters 6 and 8 )
Goals:
1. Overview of predefined functions available in MATLAB (will not be able to cover all
of them, but can demonstrate a few and show students how to locate them in HELP).
They will reference this Chapter in later labs for some of these functions
2. Introduce the concept of programming, input/output, logic, and family of if statements
Notes:
Predefined MATLAB Functions From Chapter 6 (5 minutes)
• MATLAB is programmed with a number of special characters (+ - ^ ; [ ]etc.)
commands (i.e. clc, clear, help, quit, whos) and functions (i.e. sin, plot, median,
xlable)
• These all work together to make MATLAB a powerful interface for number
crunching and plotting, giving it an advantage over high-level programming
languages due to the ease of programming. It can execute more slowly that C++ or
FORTRAN, but it excels when dealing with matrix calculations
• You had some examples in the previous week on utilizing a few of these
• Time nor our patience will allow for us to go through every MATLAB function
o Chapter 6 gives a good overview of the structure and application of
Elementary math Functions (p 120)
• abs(x), sqrt(x), exp(x), log(x), etc.
Trigonometric Functions (p 124)
• sin(x), cos(x), tan(x), etc.
Data Analysis Functions (p 128-132, 137-140)
• max(x), min(x), mean(x), median(x), etc.
o A summary of those discussed in Chapter 6 are found on p 157 for easy
reference
o Appendix 1 (p 327-334) provides a reference for Characters, Commands,
and Functions
o help command, Help in Windows interface, or www.mathworks.com can
direct you to more information as needed.
pf3
pf4
pf5

Partial preview of the text

Download MATLAB Predefined Functions and Programming - Lecture Notes | BAE 200 and more Study notes Agricultural engineering in PDF only on Docsity!

BAE 200

Week 3 Lecture notes: 9/9/

MATLAB Predefined Functions and Programming (Engineering Analysis and MATLAB7 - Chapters 6 and 8 )

Goals:

  1. Overview of predefined functions available in MATLAB (will not be able to cover all of them, but can demonstrate a few and show students how to locate them in HELP). They will reference this Chapter in later labs for some of these functions
  2. Introduce the concept of programming, input/output, logic, and family of if statements

Notes:

Predefined MATLAB Functions From Chapter 6 (5 minutes)

  • MATLAB is programmed with a number of special characters (+ - ^ ; [ ]etc.) commands (i.e. clc, clear, help, quit, whos) and functions (i.e. sin, plot, median, xlable)
  • These all work together to make MATLAB a powerful interface for number crunching and plotting, giving it an advantage over high-level programming languages due to the ease of programming. It can execute more slowly that C++ or FORTRAN, but it excels when dealing with matrix calculations
  • You had some examples in the previous week on utilizing a few of these
  • Time nor our patience will allow for us to go through every MATLAB function o Chapter 6 gives a good overview of the structure and application of ƒ Elementary math Functions (p 120) - abs(x) , sqrt(x), exp(x), log(x), etc. ƒ Trigonometric Functions (p 124) - sin(x), cos(x), tan(x), etc. ƒ Data Analysis Functions (p 128-132, 137-140) - max(x), min(x), mean(x), median(x), etc. o A summary of those discussed in Chapter 6 are found on p 157 for easy reference o Appendix 1 (p 327-334) provides a reference for Characters, Commands, and Functions o help command, Help in Windows interface, or www.mathworks.com can direct you to more information as needed.
  • Activity #1 - Type help in the command window o Notice all of the help topics that come on the screen. You can choose these individual topics and find functions you need or learn what they are for and how they work
  • Clicking on? or Help tab will give you the option of using indexing or to see MATLAB examples, etc. Try this
  • We will be referring to these Functions with examples for the rest of the MATLAB lectures and laboratories.

Programming in MATLAB

  • Up until now we have been using MATLAB mostly as a calculator in the Command Window
  • Now lets expand our use of MATLAB to include programming techniques and the Edit Window
  • MATLAB Inputs and Outputs (p 208-215) (10 minutes)

o MATLAB allows the user to control how data is input into programs and how output is displayed o input command allows the programmer to request input ƒ syntax for input of a scalar (Activity #2)

  • flow_v = input (‘Enter a value for the velocity of the stream in m/s: ’)
  • The user will enter a value, say of 2
  • MATLAB will display flow_v = 2
  • ; will suppress output if you desire
  • Note space given after m/s: to allow for user to input. Not needed but looks better ƒ Syntax for vector (one row matrix) or matrix the same, but the user needs to be prompted to enter the data in correctly with [] and ; if necessary o MATLAB Outputs - solutions as scalars, matrices, tables, and plots. Outputs can be formatted to become personalized or unique with output statement string (i.e ‘The results of the simulation give outflow equal to: ) ƒ Display function - disp(x)
  • Can be used to display a string (i.e. text) or a variable anytime. Most likely it will be used to display results of a program solution ƒ Formatted Output - fprintf
  • Syntax - fprintf (‘format string’,variable1, variable2,…)
  • Can be used to display tables they way that you want o Syntax -fprintf(‘format string’, table_1)
  • Statement level Control Structures (p 222) (25 minutes)- Sections of Computer code can be categorized as o Sequences - a list of command that are executed one after another ƒ These are the types of programs from Lab #2 (example)

o Selection Structures - allows programmer to execute one or more commands based on a certain criteria being true or false. ƒ The program selects a “path” to continue on based on a logical condition. They often contain Relational or Logical operators

  • Selection structures o Relational or Logical operators - before you can understand how the program uses selection structures, must understand the relational and logical operators that MATLAB uses to make decisions on where the program will go to execute the next set of commands

ƒ Relational operators (Table 4 p 223)

  • less than (<), less than or equal to (<=) greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=) ƒ Logical operators (Table 5 p 224) and (&), not (~), or (|)

MATLAB

Functions Executed one after another

MATLAB Can execute different commands that may take the program on different paths based on logical conditions.

ƒ Activity #5 Examples of the use of operators

  • In the Command Window type in the following:
  • x=1;
  • y= 2;
  • Test the following relational operators (ans = 1 true, ans= false) o x>y o x<y o x= =y
  • Test the following combination of relational and logical operators o x<y&x= =y o x>y|x<y o x<y~x>y

ƒ What if you are comparing 2 matrixes? Every element is compared, and if the condition is not satisfied for every comparison, MATLAB sees the result as false ƒ find - unique command to MATLAB. Best to use the find command rather than use relational or logical operators (see p224- 228 for more details)

o If, else, elseif (p229-233) ƒ Considered CONDTIONAL STATEMENTS ƒ Work much better with scalars rather than matrices

  • A conditional statement is used to tell the program to execute the following set of commands only if a certain condition is met; otherwise, the program skips the group of commands and goes to the end. The ‘if’ group of conditional statements include ‘ if-end ’, ‘ if-else-end ’, and ‘ if-elseif-else-end ’ ƒ ‘ if-end ’: Here, if the condition is met, the program performs the set of commands within the loop; otherwise, it goes to the end of the program.

-------- (Matlab commands) if (conditional statement)


-------- (Matlab commands) end

  • For example, let’s say that all biological activity in an organic medium will stop if its moisture content falls below a threshold value (say 1%). Above that threshold value, we can simulate the biological activity using some type of relationship (Matlab commands shown inside loop).