Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1-3 – Matlab Overview

Similar presentations


Presentation on theme: "Chapter 1-3 – Matlab Overview"— Presentation transcript:

1 Chapter 1-3 – Matlab Overview
ENGR 1320

2 Variables A symbol used to contain a value
Used to write mathematical expressions Variable naming convention Must begin with a letter Must contain < 32 characters Is case sensitive May contain letters, digits, & underscore Scalar variable - a single number Array variable - a collection of numbers Can be handled by MATLAB as a single variable

3 Variables Assigning values to variables Use “=“ as assignment operator
Variable name on left-hand side is replaced by value generated by right-hand side Right-hand side must have computable value Array assignment “[]” contains numbers being assigned Commas separate elements in row Semi-colons separate rows One and only one variable name is allowed on the left hand side of the assignment operator

4 Variables Array assignment (cont)
Special format for assigning row array with regularly spaced numbers Type first number, last number, with spacing in middle All numbers separated by colon “:” One and only one variable name is allowed on the left hand side of the assignment operator

5 Variables Array addressing Use just variable name in expressions
Operation is performed on every element in the array IF matrix operation is “legal” Use variable name & array index in expressions Point to a particular element in the array Use if operation to be performed is not “legal” matrix operation One and only one variable name is allowed on the left hand side of the assignment operator

6 Variables MATLAB has predefined variables
Avoid using names of predefined variables as variable names

7 Mathematical Expressions
Order of precedence Highest order of precedence first Operators of equal precedence from left to right

8 Functions MATLAB mathematical functions
Pair of parentheses after function name encloses function’s argument Can be part of mathematical expression There is an extensive list of functions in Chapter 3 Or you can use the online help

9 Functions Users can define functions
MATLAB commands and functions for managing the work session There is an extensive list of functions in Chapter 3 Or you can use the online help

10 Plotting Graphics window Appears in Desktop when a plot is generated

11 Plotting Simple rectilinear plot Function plot(x,y)
Generates a plot with x-values on the horizontal axis and y-values on the vertical axis Function xlabel(‘xxxx’) Places text within single quotes as label for horizontal axis Function ylabel(‘yyyy’) Places text within single quotes as label for vertical axis Functions are all written on same command line, separated by commas

12 Plotting Simple rectilinear plot (cont) Function title(‘text’)
Places text at top of plot Overlay plots Include another set or sets of values in plot function

13 Script Files Program files saved with .m extension Use Editor window
In Command window, select File, New, M-file This opens editor window When finished, select File, Save from Editor window

14 Script Files Naming convention Same as for naming variables
Not the same name as a variable or a MATLAB function Use exist(‘name’) function to verify that a prospective filename is not already in use

15 Script Files Comments “%” - Not executed by MATLAB
Comments section – at beginning of program 1st line - name of the program, keywords 2nd line - date created and programmer’s name 3rd line – description of program’s purpose Variable definitions Input Output Internal to program Include units for each variable, if applicable.

16 Script Files Comments Programming in MATLAB
Additional comments throughout program Break program into sections. Describe purpose for calculations Programming in MATLAB Relational operators Used to make comparisons Relational operators not needed for the first assignment

17 Script Files Programming in MATLAB (cont) Conditional statements
Allow decision-making if expression commands elseif expression else end Syntax for conditional statements and looping statements are case sensitive

18 Script Files Programming in MATLAB (cont) Loops Repeated calculations
For  Number of passes is known ahead of time While  Looping terminates when a condition is met for k=1:10 commands end while condition commands end Syntax for conditional statements and looping statements are case sensitive You will need to use a looping statement because you will not be able to do simple matrix calculations when you are calculating the material properties

19 Class Exercise Do the following in the Command Window:
>>clc >>30/10 >>r = 30/10 >>r >> s = 10*r >>v = sin(s) >>clear r >>s >>r = s >>r = 10; >>clear >>help elfun >>help sin >>lookfor sine Write an m-file script that performs the following calculation: 𝑓 𝑥 = 𝑥 2 cos⁡(2𝑥+1) 6𝑥 log⁡(𝑥) Syntax for conditional statements and looping statements are case sensitive You will need to use a looping statement because you will not be able to do simple matrix calculations when you are calculating the material properties


Download ppt "Chapter 1-3 – Matlab Overview"

Similar presentations


Ads by Google