1 / 32

CS005 Introduction to Programming Matlab

CS005 Introduction to Programming Matlab. Eamonn Keogh eamonn@cs.ucr.edu. Displaying Text. (sidebar). EDU>> disp (' Hello World ') Hello World EDU>>.

dpicard
Télécharger la présentation

CS005 Introduction to Programming Matlab

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. CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

  2. Displaying Text

  3. (sidebar) EDU>> disp('Hello World') Hello World EDU>> From now on, I am mostly going to show the Matlab commands without showing the command window (as above, left). However we all understand that the commands are type in the command window.

  4. Displaying Text Correct, the phase ‘Hello World’ is in single quotes, it is a literal string EDU>> disp('Hello World') Hello World EDU>> EDU>> disp(Hello World) ??? disp(Hello World) | Error: Unexpected MATLAB expression. Incorrect, the phase Hello World is not in quotes, matlab thinks it might be a variable (but it does not have a legal variable name)

  5. Displaying Variables We can display the value of a variable. This seem redundant, because we can just type the name of the variable itself. However we will see later that this is very useful. EDU>> Age = 27; EDU>> EDU>> disp(Age) 27 EDU>>

  6. Displaying Variables Note the difference here. In the first case, we are saying display the value of the variable Age, because there are no quotes. In this case, we are saying, display the literal string ‘Age’ EDU>> Age = 27; EDU>> EDU>> disp(Age) 27 EDU>> EDU>> disp('Age') Age EDU>>

  7. Displaying Variables Note that the disp command respects white space EDU>> disp('Age') Age EDU>> EDU>> disp('Age') Age EDU>>

  8. Displaying Variables We can ask disp to display the result of any valid expression EDU>> disp(Age + 17) 44 EDU>>

  9. Using Built-In Functions We can use some of Matlabs built-in functions to do calculations Here sqrt is a function 9 is a parameter argument input argument 3 was the returned value result Here we say that sqrt was called invoked EDU>> sqrt(9) ans = 3

  10. Using Built-In Functions We sometimes talk about a black box view of a function EDU>> sqrt(9) ans = 3 9 3

  11. Using Built-In Functions We can use variables as input arguments EDU>> HisAge = 25 HisAge = 25 EDU>> sqrt(HisAge) ans = 5 EDU>>

  12. Using Built-In Functions We can use expressions as input arguments EDU>> sqrt(HisAge - 5) ans = 4.4721 EDU>>

  13. Using Built-In Functions We can use expressions as input arguments Since function calls are legal parts of an expression, we can have function calls inside function calls EDU>> HisAge = 81 HisAge = 81 EDU>> sqrt( sqrt(HisAge) ) ans = 3

  14. Using Built-In Functions We can assign the result of function call to a variable EDU>> Fish = 56; EDU>> HisAge = sqrt(Fish) HisAge = 7.4833 EDU>>

  15. Matlab has many built-in functions we can use. Later we will learn to write our own functions >> bob = abs(-9) bob = 9 >> bob = round(9.332) + sqrt(3 + 6) bob = 12 >> bob = sqrt(9) bob = 3 >> bob = log(9) bob = 2.172 >> bob = cos(0) bob = 1 Matlab has hundreds of built-in functions

  16. Using Built-In Functions Some functions take more than one parameter For example, the max function takes two But how could we find the maximum of three numbers? (next slide) EDU>> max(3,4) ans = 4 3 4 4

  17. Using Built-In Functions How can we find the maximum of three numbers? We will later see better ways to do this EDU>> max(3,max(1,4)) ans = 4 1 4 3 4 4

  18. s h b a Suppose we have an equilateral triangle, and we measure one side, finding it to be 45 millimeters long... >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> >> area = (sqrt(3) * side^2) / 4; >> >> >> height height = 38.9711 >> >> area area = 876.8507 >> Quiz! Finish the code below >> a = 3; >> b = 2; >> pi = 3.14; >> area = ... >> circ = ...

  19. Up to this point we have typed in our “programs” line by line. Now we will learn to write scripts. Our program is typed into a special text editor that comes with Matlab. In this case the name of the file is triangle_stuff.m >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> >> area = (sqrt(3) * side^2) / 4; >> >> disp(height) 38.9711 >> >> disp(area) 876.8507 We can run (invoke) our program simply by typing the name of the file (without the ‘.m’ extension)

  20. At the command line I typed edit and hit <enter> This caused a text editor to appear….

  21. Into the text editor, I typed all the commands for my triangle program The empty lines are just for clarity..

  22. I clicked save as , and that opened a dialogue box…. I named my script file as triangle_stuff.m Must end in .m

  23. Now I can run (invoke, call) my script by typing its name….. Note the file name

  24. We can generalize our program with the input function

  25. side = input('How large is the side? '); When matlab encounters the input function, it display the association text, which is usually a prompt (How old are you etc) then it waits for a number to be typed, and <enter> to be hit. Once <enter> is hit, it assigns the value to the variable on the LHS, and continues to the next line.

  26. From now on we will assume that all code is being placed in a script file. As before, we won’t always bother showing the window. disp('Here is my triangle program'); side = input('How large is the side? '); height = (sqrt(3) * side) / 2; area = (sqrt(3) * side^2) / 4; disp(height); disp(area);

  27. It is important to comment all our programs…. % Written by Bertram Wilberforce Wooster, June 11, 1917. % This program prompts a user for a number corresponding to % the side of an equilateral triangle, then calculates the % area and height of the triangle. disp('Here is my triangle program'); % Prompt the user. side = input('How large is the side? '); % Input the value. %------------------Begin Calculations ------------------------------------ % Math formulas came from “Math and % the single girl” Page 23. height = (sqrt(3) * side) / 2; % Perform calculation for height. area = (sqrt(3) * side^2) / 4; % Perform calculation for area. %------------------Begin Display of Results------------------------------- disp(height); % Display the height. disp(area); % Display the area. We may omit comments in lecture notes for pedagogical reasons, but essentially every line of code you write should be commented.

  28. Variables • Don’t name your variables the same as functions • min, max, sqrt, cos, sin, tan, mean, median, etc • Funny things happen when you do this • MATLAB reserved words don’t work either • i, j, eps, nargin, end, pi, date, etc • i, j are reserved as complex numbers initially • Will work as counters in my experience so they can be redefined as real numbers

  29. MATLAB Help • Ways to get help in MATLAB • help function name • Provides basic text output • Type helpwin on command line • Look under the help menu on the desktop

  30. MATLAB Help • Can browse or search product help for a specific function or topic • MATLAB help has introductory help material, basic overviews of how to use functions, plot, program in MATLAB, example code, etc • lookforkeyword command will also find functions that have the keyword in them • doc function_name brings up the full documentation for the function

  31. s h b a Suppose we have an equilateral triangle, and we measure one side, finding it to be 45 millimeters long... >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> >> area = (sqrt(3) * side^2) / 4; >> >> >> height height = 38.9711 >> >> area area = 876.8507 >> Quiz! Finish the code below >> a = 3; >> b = 2; >> pi = 3.14; >> area = ... >> circ = ...

More Related