CS 005 Introduction to Programming Matlab Eamonn Keogh
CS 005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs. ucr. edu
Displaying Text
(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.
Displaying Text EDU>> disp('Hello World') Hello World EDU>> Correct, the phase ‘Hello World’ is in single quotes, it is a literal string 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) EDU>> disp(Hello World) ? ? ? disp(Hello World) | Error: Unexpected MATLAB expression.
Displaying Variables EDU>> Age = 27; EDU>> disp(Age) 27 EDU>> 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.
Displaying Variables EDU>> Age = 27; EDU>> disp(Age) 27 EDU>> disp('Age') Age EDU>> 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’
Displaying Variables EDU>> disp(' Age EDU>> disp('Age') Age EDU>> Age') Note that the disp command respects white space
Displaying Variables EDU>> disp(Age + 17) 44 EDU>> We can ask disp to display the result of any valid expression
Using Built-In Functions EDU>> sqrt(9) ans = 3 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
Using Built-In Functions EDU>> sqrt(9) ans = 3 9 3 We sometimes talk about a black box view of a function
Using Built-In Functions EDU>> His. Age = 25 EDU>> sqrt(His. Age) ans = 5 EDU>> We can use variables as input arguments
Using Built-In Functions EDU>> sqrt(His. Age - 5) ans = 4. 4721 EDU>> We can use expressions as input arguments
Using Built-In Functions EDU>> His. Age = 81 EDU>> sqrt(His. Age) ) ans = 3 We can use expressions as input arguments Since function calls are legal parts of an expression, we can have function calls inside function calls
Using Built-In Functions EDU>> Fish = 56; EDU>> His. Age = sqrt(Fish) His. Age = 7. 4833 EDU>> We can assign the result of function call to a variable
Matlab has many built-in functions we can use. Later we will learn to write our own functions >> bob = sqrt(9) bob = 3 >> bob = abs(-9) bob = 9 >> bob = log(9) bob = 2. 172 >> bob = round(9. 332) + sqrt(3 + 6) bob = 12 >> bob = cos(0) bob = 1 Matlab has hundreds of built-in functions
Using Built-In Functions Some functions take more than one parameter EDU>> max(3, 4) ans = 4 3 4 4 For example, the max function takes two But how could we find the maximum of three numbers? (next slide)
Using Built-In Functions How can we find the maximum of three numbers? EDU>> max(3, max(1, 4)) ans = 4 1 3 4 4 4 We will later see better ways to do this
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 = 38. 9711 >> >> area = 876. 8507 >> s h Quiz! Finish the code below >> >> >> a = 3; b = 2; pi = 3. 14; area =. . . circ =. . . b a
Up to this point we have typed in our “programs” line by line. Now we will learn to write scripts. >> >> side = 45; height = (sqrt(3) * side) / 2; area = (sqrt(3) * side^2) / 4; disp(height) 38. 9711 >> >> disp(area) 876. 8507 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 We can run (invoke) our program simply by typing the name of the file (without the ‘. m’ extension)
At the command line I typed edit and hit <enter> This caused a text editor to appear….
Into the text editor, I typed all the commands for my triangle program The empty lines are just for clarity. .
I clicked save as , and that opened a dialogue box…. I named my script file as triangle_stuff. m Must end in. m
Now I can run (invoke, call) my script by typing its name…. . Note the file name
We can generalize our program with the input function
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.
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);
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; area = (sqrt(3) * side^2) / 4; % Perform calculation for height. % 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.
Variables • Don’t name your variables the same as functions – min, max, sqrt, cos, sin, tan, 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
MATLAB Help • Ways to get help in MATLAB – help function name – Provides basic output text • Type helpwin on command line • Look under the help menu on the desktop
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 • lookfor keyword command will also find functions that have the keyword in them • doc function_name brings up the full documentation for the function
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 = 38. 9711 >> >> area = 876. 8507 >> s h Quiz! Finish the code below >> >> >> a = 3; b = 2; pi = 3. 14; area =. . . circ =. . . b a
- Slides: 32