Академический Документы
Профессиональный Документы
Культура Документы
Programming
Introduction to MATLAB
Programming - 2
Programs are contained in m-files Plain files not binary files produced by word Files must have an .m extension m-files must be in path When you call an M-file function from either the command line or from within another M-file, MATLAB parses the function and stores it in memory. This prevents MATLAB from having to reparse a function each time you call it during a session. The compiled function remains in memory until you clear it using the CLEAR command, or until you quit MATLAB.
Programming - 3
2. Functions - provide extensibility to MATLAB. They allow you to add new functions to the existing functions
Introduction to MATLAB
Script M-files
Programming - 4
Standard ASCII text files Contains series of MATLAB expressions stored together in a predefined sequence. Saved with an .m extension and are called by simply typing the filename without the extension in the command window.
Downside of using Script files: - All variables created are added to the workspace. - Variables already existing in the workspace may be overwritten
Introduction to MATLAB
Functions M-files
Programming - 5
Functions are subprograms Use input and output parameters to communicate with other function
Differences between Script & Function M-files: Structural Syntax - must contain keyword FUNCTION at the beginning of the first line
Function Workspaces, Inputs & Outputs A function does not work with variables in the base MATLAB workspace. As a result, all information to be transferred between the MATLAB workspace and a function must be passed as inputs & outputs.
Introduction to MATLAB
Structure of a Function M-file
Keyword: function
Programming - 6
Output Argument(s)
function y = mean(x)
Input Argument(s)
if m == 1
m = n;
end
y = sum(x)/m; output_value = mean(input_value)
Introduction to MATLAB
Text Input and Output
Inputs to Functions :
Programming - 7
input function can be used to prompt the user for numeric or string input Input parameters to functions are preferred
Text output from functions: disp function for simple output fprintf function for formatted output.
Introduction to MATLAB
Prompting for User Input
Programming - 8
The input function can be used to prompt the user for numeric or string input. Syntax: entry = input (Text Display)
Introduction to MATLAB
Text Output with disp - Output to the command window is achieved with the disp
Programming - 9
function disp Simple to use. Provides limited control over appearance of output. Syntax: disp (variable) disp (text string) Examples: >>poly_roots = [ 1.2400 2.5600 5.6400 ] >> disp ('The roots of the cubic polynomial are') The roots of the cubic polynomial are >> disp (poly_roots) 1.2400 2.5600 5.6400 >> disp ('The roots of the cubic polynomial are') , disp (poly_roots) The roots of the cubic polynomial are 1.2400 2.5600 5.6400
Introduction to MATLAB
disp
Programming - 10
Introduction to MATLAB
Programming - 11
-Will output 1 for true conditions and 0 for false conditions - Can be used with scalars and arrays
Introduction to MATLAB
Programming - 12
Description
Returns true if all elements in a vector A are true. Returns false if one or more elements are false Returns 1 (true) if any element in a vector A is true (nonzero). Returns 0 (false) if all elements are false. If A is a vector, returns the indices of the nonzero elements If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used)
find (A>d)
Introduction to MATLAB
SUMMARY
Programming - 13
Relational operators involve comparison of two values. The result of a relational operation is a logical (True/False)
value.
Logical operators combine (or negate) logical values to
Introduction to MATLAB
Programming - 14
Example : Analysis of temp data The following were the daily max temp in (F) in Washington during the month of April,2002. 58 73 73 53 30 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80 59 69 56 64 63 66 64 74 63 69.
Use relational logical operators to determine the following: a. The number of days the temp was above 75 b. the number of days the temp was between 65 and 80
Introduction to MATLAB
Examples:
>> r = [ 8 12 9 4 23 19 10 ] r= 8 12 9 4 23 19 10 >>s=r<=10
Programming - 15
s=
1 t= 0
>>t=r(s) 8
w= 8 9 4
>> w=r(r<=10)
Introduction to MATLAB
fprint & sprintf
Programming - 16
Programming - 17
function areacircle r=input('Give radius of circle: '); a=pi*r^2; fprintf('The area of circle with radius %.2f is %.6f\n',r,a); end
Introduction to MATLAB
Programming - 18
Introduction to MATLAB
Example:
Programming - 19
The following script file will returns one of the following messages: teenager, adult and senior given as the input of the variable age. function check_age age=input('Enter your age: ') if age<=18 disp('teenager') elseif age<=52 disp('adult') else disp('senior') end