190 likes | 201 Views
Learn the basics of MatLab functions and arrays with practical examples in this beginner's guide. Understand function attributes, parameters, return values, and the creation and manipulation of arrays.
E N D
Announcements • If You Would Like to Change Your Major to Computer Science, Please Let Me Know • (Because we can get you help) • Hotlines • Support Groups • De-programming Camps • If You Would Like to Change Your Major to Computer Science, Please Don't Let Anyone Else Know • (For Your Own Good)
Functions Command Window or Script File M File with Function displayHi.m function z = displayHi() disp('Hi'); … displayHi(); … displayHi(); … … displayHi();
Functions • Function: A Discrete Piece of Code that Performs a Specific Operation or Task • Called procedure, module, method in other languages • Named with a Descriptive Identifier; Stored in FunctionName.m File • Called from Command Window or Scripts • When Called, Program Control (Execution) Is Transferred to the Function • Function Performs Required Tasks, and then Possibly Returns a Value • After Return from Function, Control Returns to the Statement Following the Function Call
Function Attributes • Function Name: Identifier Used to Call Function • Function Parameter(s) or Argument(s): Value(s) Passed into Function for Use by Function Code • Function Return Value: Value Returned by Function Back to Calling Function
Function Parameters (Arguments) • May Pass as Many Parameters as Necessary to Function • A Copy of the Value of the Parameter Is Passed to the Function • Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable • This Is Called Pass-by-Value
Declaring a Function • In .m File • Format : function resultVar = myFunction(parameter(s)) • Example : myFunction.m : function result = myFunction(x) result = x^2; result = result^2; Command Window : >> c = myFunction(2) c = 16
Another Function Example myFunction2.m : function result = myFunction2(x,y) result = x^2 + y; Command Window : >> myFunction2(2,3) ans = 7
Arrays • Array – Set of Values with One Name • MatLab Creation : arrayName = [ 1,3,5 ]; • Vector – One Dimensional Array • Matrix – Two Dimensional Array • Element – One Member (Value) in an Array • Offset or Subscript – location of an Element in and Array (in MatLab, starting with 1) • Row Vector - “Row” of Values • Column Vector - “Column” of Values
Creating Arrays – Row Vector >> rowV = [ 1, 3, 5 ] rowV = 1 3 5 >> rowV(2) ans = 3
Creating Arrays – Column Vector >> colV = [ 2; 4; 6 ] colV = 2 4 6 >> colV(3) ans = 6
Transpose (') Operator >> rowV' ans = 1 3 5 >> colV' ans = 2 4 6
Offsetting with Variable >> offset = 3 offset = 3 >> colV(offset) ans = 6
Array Example >> for k = 1:10 newRow(k) = k^2; end >> newRow newRow = 1 4 9 16 25 36 49 64 81 100
Colon Operator % [Start:Step:End] >> rowV = [10:5:50] rowV = 10 15 20 25 30 35 40 45 50 >> rowV = [10:5:49] rowV = 10 15 20 25 30 35 40 45 >> rowV = [10:15] rowV = 10 11 12 13 14 15
linspace() Function % linspace(Start, End, NumValues) >> rowV = linspace(10,50,5) rowV = 10 20 30 40 50
Adding Elements to Vectors % linspace(Start, End, NumValues) >> rowV = linspace(10,50,5) rowV = 10 20 30 40 50 >> rowV(6) = 5555 rowV = 10 20 30 40 50 5555 >> newRowV(4) = 1000 newRowV = 0 0 0 1000
Adding Vectors to Vectors >> rowV = [ 1, 3, 5]; >> rowV2 = [ 7, 9, 11, 13]; >> rowVadd = [rowV, rowV2] rowVadd = 1 3 5 7 9 11 13
Offset Errors >> rowVadd(8) Index exceeds matrix dimensions. >> rowVadd(0) Subscript indices must either be real positive integers or logicals. >> rowVadd(1.5) Subscript indices must either be real positive integers or logicals. >> rowVadd(8) = 77 rowVadd = 1 3 5 7 9 11 13 77