Download presentation
Presentation is loading. Please wait.
Published byDarleen Johnson Modified over 8 years ago
1
MATLAB Technical Computing Environment
2
MATLAB Matlab is an interactive computing environment that enables numerical computation and data visualization. Matrix Operations Differential Equations Fourier Transforms Statistics and much more..
3
Running MATLAB MATLAB Programming Environment M-File (Script) Editor (We will cover this in the following weeks)
4
MATLAB Interface Command Window Workspace/ Current Directory Command History
5
Getting Help help, by itself, lists all help topics help topic provides help for the speci.ed topic help command provides help for the specified command help help provides information on use of the help command helpwin – On-line help, separate window for navigation. helpdesk – Comprehensive hypertext documentation and troubleshooting demo – Run demonstrations intro – Interactive introduction to Matlab
6
Arithmetic Operations OperatorsAlgebraicMATLABExample Additiona+b 5+3 Substractiona-b 5-3 Multiplicationaxba*b5*3 Right Divisiona/b 49/7 Left Divisionb/aa\b7/56 Exponentiationabab a^b5^2
7
Precedence of Operations Since several operations can be combined in one expression, there are rules about the order in which these operations are performed: Parentheses, innermost first Exponentiation (^), left to right Multiplication (*) and division (/ or \) with equal precedence, left to right Addition (+) and subtraction (−) with equal precedence, left to right
8
Variables and Assignment Statements Variable names can be assigned to represent numerical values in Matlab. The rules for these variable names are: Must start with a letter May consist only of the letters a-z, digits 0-9, and the underscore character (_) 31 characters max Is case sensitive: items, Items, itEms, and ITEMS are all different variable names Variable assignment in MATLAB variable = numberunitcost = 0.30 numberofitems = 5 variable = expression totalcost = unitcost * numberofitems
9
Special Variables ans: the answer of previous calculation pi: ratio of circle circumference to its diameter, π = 3.1415926... Scientific notation 12x10 -27 12e-27
10
Commands involving variables: who: lists the names of defined variables whos: lists the names and sizes of defined variables clear: clears all variables, resets default values of special variables clear var: clears variable var clc: clears the command window, but does not affect variables. clf: clears the current figure and thus clears the graph window. more on: enables paging of the output in the command window. more off: disables paging of the output in the command window. When more is enabled and output is being paged, advance to the next line of output by pressing Enter ; get the next page of output by pressing the spacebar. Press q to exit out of displaying the current item.
11
Command Reuse and Editing Commands can be reused and editted using the following operations: Press the up arrow cursor key (↑) to scrolls backward through previous commands. Press Enter to execute the selected command. The down arrow cursor key (↓) scrolls forward through commands The left (←) and right arrow (→) cursor keys move within a command at the Matlab prompt, allowing the command to be edited. The mouse can also be used to reposition the command cursor, by positioning the mouse cursor and pressing the left mouse button. Once a scrolled or edited command is acceptable, pressing Enter with the cursor anywhere in the command tells Matlab to process it. Escape key Esc erases the current command at the prompt.
12
Punctuation and Comments Semicolon (;) at the end of a command suppresses the display of the result Commas and semicolons can be used to place multiple commands on one line, with commas producing display of results, semicolons supressing Percent sign (%) begins a comment, with all text up to the next line ignored by Matlab Three periods (...) at the end of a command indicates that the command continues on the next line. A continuation cannot be given in the middle of a variable name.
13
Basic Mathematical Functions abs(x):Absolute value |x| sign(x):Sign, returns -1 if x 0 exp(x):Exponential e x log(x):Natural logarithm ln x log10(x):Common (base 10) logarithm log10 x sqrt(x):Square root √x rem(x,y):Remainder of x/y. For example, rem(100,21) is 16. (enter help elfun for a more complete list)
14
Display Options Matlab Example Comments format short 50.8333 4 decimal digits format long 50.83333333333334 14 decimal digits format short e 5.0833e+001 4 decimal digits plus exponent format long e 5.083333333333334e+001 14 decimal digits plus exponent format bank 50.83 2 decimal digits Note: The display formats do not change the internal representation of a number; only the display changes.
15
Displaying Values and Text There are three ways to display values and text in Matlab, to be described in this section: By entering the variable name at the Matlab prompt, without a semicolon. By use of the command disp. By use of the command fprintf.
16
disp() There are two general forms of the command disp that are useful in displaying results and annotating them with units or other information: disp(variable): Displays value of variable without displaying the variable name. disp(string): Displays string by stripping of the single quotes and echoing the characters between the quotes. String: A group of keyboard characters enclosed in single quote marks (’). The quote marks indicate that the enclosed characters are to represent ASCII text. >> temp = 78; >> disp(temp); disp(’degrees Celcius’) 78 degrees Celcius
17
fprintf() fprintf(’format string’, list of variables) w.d%fDisplay as fixed point or decimal notation (defaults to short), with a width of w characters (including the decimal point and possible minus sign, with d decimal places. Spaces are filled in from the left if necessary. Set d to 0 if you don’t want any decimal places, for example %5.0f. Include leading zeros if you want leading zeroes in the display, for example %06.0f. w.d%e Display using scientific notation (defaults to short e), with a width of w characters (including the decimal point, a possible minus sign, and five for the exponent), with d digits in the mantissa after the decimal point. The mantissa is always adjusted to be less than 1. \nNew line (skip to beginning of next line)
18
fprintf() The w.d width specifiers are optional. If they are left out, default values are used. Examples: >> fprintf(’The temperature is %f degrees F \n’, temp) The temperature is 78.000000 degrees F >> fprintf(’The temperature is %4.1f degrees F \n’, temp) The temperature is 78.0 degrees F
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.