Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS005 Introduction to Programming

Similar presentations


Presentation on theme: "CS005 Introduction to Programming"— Presentation transcript:

1 CS005 Introduction to Programming
Matlab Eamonn Keogh

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 EDU>> disp('Hello World') Hello World
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 EDU>> 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. EDU>> Age = 27; EDU>> EDU>> disp(Age) 27

6 Displaying Variables EDU>> Age = 27; 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’ EDU>> Age = 27; EDU>> EDU>> disp(Age) 27 EDU>> disp('Age') Age

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

8 Displaying Variables EDU>> disp(Age + 17) 44 EDU>>
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 >> bob = round(9.332) + sqrt(3 + 6) 12 >> bob = sqrt(9)
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) 12 >> bob = sqrt(9) bob = 3 >> bob = log(9) 2.172 >> bob = cos(0) 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 4 3

18 Suppose we have an equilateral triangle, and we measure one side, finding it to be 45 millimeters long... s h >> side = 45; >> >> height = (sqrt(3) * side) / 2; >> area = (sqrt(3) * side^2) / 4; >> height height = >> area area = a b 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
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) >> disp(area) We can run (invoke) our program simply by typing the name of the file (without the ‘.m’ extension)

20 This caused a text editor to appear….
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 As before, we won’t always bother showing the window.
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

29 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

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

31 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

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


Download ppt "CS005 Introduction to Programming"

Similar presentations


Ads by Google