1 / 19

Announcements

Announcements. 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

Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements

  2. 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)

  3. Functions Command Window or Script File M File with Function displayHi.m function z = displayHi() disp('Hi'); … displayHi(); … displayHi(); … … displayHi();

  4. 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

  5. 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

  6. 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

  7. 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

  8. Another Function Example myFunction2.m : function result = myFunction2(x,y) result = x^2 + y; Command Window : >> myFunction2(2,3) ans = 7

  9. 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

  10. Creating Arrays – Row Vector >> rowV = [ 1, 3, 5 ] rowV = 1 3 5 >> rowV(2) ans = 3

  11. Creating Arrays – Column Vector >> colV = [ 2; 4; 6 ] colV = 2 4 6 >> colV(3) ans = 6

  12. Transpose (') Operator >> rowV' ans = 1 3 5 >> colV' ans = 2 4 6

  13. Offsetting with Variable >> offset = 3 offset = 3 >> colV(offset) ans = 6

  14. Array Example >> for k = 1:10 newRow(k) = k^2; end >> newRow newRow = 1 4 9 16 25 36 49 64 81 100

  15. 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

  16. linspace() Function % linspace(Start, End, NumValues) >> rowV = linspace(10,50,5) rowV = 10 20 30 40 50

  17. 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

  18. Adding Vectors to Vectors >> rowV = [ 1, 3, 5]; >> rowV2 = [ 7, 9, 11, 13]; >> rowVadd = [rowV, rowV2] rowVadd = 1 3 5 7 9 11 13

  19. 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

More Related