1 / 17

Review

Review. Basic plotting command is plot(x,y) To plot a function we often use x=a:b:n, then y = f(x) Can customize using line specifiers: plot(x,y,’-g*’) Use subplot(m,n,i) for mxn array of plots. Can use fplot for functions, e.g. fplot('x^2 + 4', [-3 3])

vilmos
Download Presentation

Review

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. Review • Basic plotting command is plot(x,y) • To plot a function we often use x=a:b:n, then y = f(x) • Can customize using line specifiers: plot(x,y,’-g*’) • Use subplot(m,n,i) for mxn array of plots. • Can use fplot for functions, e.g. fplot('x^2 + 4', [-3 3]) • Multiple plots #1: plot(x,y,'-b',x,yd,'-r',x,ydd,‘-k') • Multiple plots #2: • plot(x,y,'-b') → hold on → plot(x,yd,'-r') → hold off • Formatting: • xlabel, ylabel, title, axis([xmin xmax ymin ymax]), text, legend • Save to EPS or PDF, e.g. print(‘filename.pdf’,’-dpdf’)

  2. MATLAB Function Files • A MATLAB function file (called an M-file) is a text (plain ASCII) file that contains a MATLAB function and, optionally, comments. • The file is saved with the function name and the usual MATLAB script file extension, ".m". • A MATLAB function may be called from the command line or from any other M-file. • When the function is called in MATLAB, the file is accessed, the function is executed, and control is returned to the MATLAB workspace.

  3. MATLAB Function Files • The syntax for a MATLAB function definition is: function [val1, … , valn] = myfunc (arg1, … , argk) where val1 through valn are the specified returned values from the function and arg1through argk are the values sent to the function. • Since variables are local in MATLAB, the function has its own memory locations for all of the variables and only the values (not their addresses) are passed between the MATLAB workspace and the function. • It is OK to use the same variable names in the returned value list as in the argument. The effect is to assign new values to those variables. As an example, the following slide shows a function that swaps two values.

  4. MATLAB Function files Here are some example function definitions: • function y=square(x) • function av=average(x1,x2,x3,x4,x5) • function printvalue(A) • function B=readvalue() • function [mean,sttdev]=analyse(tab) Functions are the building blocks of your own programs. You should always try and divide your programming task into separate functions, then design, code and test each one independently. It is common to design from the top down, but build from the bottom up.

  5. Example of a MATLAB Function File function [ a , b ] = swap ( a , b ) % The function swap receives two values, swaps them, % and returns the result. The syntax for the call is % [a, b] = swap (a, b) temp=a; a=b; b=temp; We then call the function from the matlab command line: >> x = 5 ; y = 6 ; [ x , y ] = swap ( x , y ) x = 6 y = 5

  6. MATLAB Function Files • Referring to the function, the comments immediately following the function definition statement are the "help" for the function. The MATLAB command: >>help swap %displays: The function swap receives two values, swaps them, and returns the result. The syntax for the call is [a, b] = swap (a, b)

  7. Exercises 1. Write a function called FtoC (ftoc.m) to convert Fahrenheit temperatures into Celsius. Formula is °C=(°F  -  32)  x  5/9 . Make sure the program has a help page. Test from the command window with: FtoC(96) help FtoC 2. Write a function (roll2dice.m) to roll 2 dice, returning two individual variables: d1 and d2. For example: [d1 d2] = roll2dice d1 = 4 d2= 3

  8. Relational operators Relational operators: == equal to ~= not equal to > greater than >= greater than or equal to < less than <= less than or equal to For example, • 3<4 • 3==4 • ‘A’ < ‘B’ (note strings are evaluated by ascii value) Beware of comparisons and rounding errors, e.g. • sin(pi) == 0 Can get around this with tolerance threshold, e.g. • abs(a-b) < 1.0e-14

  9. Logical operators Can have one or two operands, giving a logical result: & Logical AND | Logical OR xor Exclusive XOR ~ Logical NOT An operand is treated as true if it is any non-zero value and false if it is zero, thus ~5 is zero (and so is ~-1) and ~0 is one. Logic operators can also be used to compare two arrays (must be same size) or a scalar value with an array. For example, If a= and b= then a|b=

  10. Hierarchy revisited • Relational and logical operators are evaluated after arithmetic operators: • 1. Arithmetic operators as described before • 2. Relational operators (==, ~=, >, etc) left to right • 3. Logical operators as follows Short-circuit operators will not evaluate second operand unless necessary, e.g. 0 && f(x) will not evaluate f(x)

  11. Logical functions • The following functions test for condition and return 1 is the test is true, 0 otherwise • ischar(a) : test for character array • isempty(a) : test for empty array • isinf(a): true if a is infinite • e.g. isinf(1/0) • isnan(a): test for NaN (not a number) • isnumeric(a)

  12. Exercises For a=20, b=-2, c=0, d=1, evaluate • a greater than b ? • (a greater than b) and (c greater than d) ? • (a different to b) or (b different to c) ? • b equal to c ?

  13. Flow control • MATLAB has five flow control constructs: • • if statements • • switch statements • • for loops • • while loops • • break statements

  14. The if construct Has the the form: if control_expr_1 statement 1; statement 2; ... else statement 1; statement 2; ... else statement 1; statement 2; ... end Note that ‘end’ here is completely different from the index function ‘end’ For example, if A > B 'greater' elseif A < B ‘less' elseif A == B 'equal' else error('Unexpected situation') end

  15. If statement if (x < 10) disp(x); % only displays x when x < 10 end if ((0 < x) & (x < 100)) disp('OK'); % displays OK if x between 0 and 100 end You can build a chain of tests with ‘elseif’, as in: if (n <= 0) disp('n is negative or zero'); elseif (rem(n,2)==0) disp('n is even'); else disp('n is odd'); end

  16. If statements You can embed, or ‘nest’ statements, as in: if (a < b)     if (a < c)         disp(a);     else         disp(c);     end else     if (b < c)         disp(b);     else         disp(c);     end end The indenting of nested statements is not necessary but is recommended.

  17. Exercises 1. Write a function to evaluate for a given value x, giving appropriate error messages. 2. Write a function that classifies the given age according to the following scheme: Error < 0 <= Child < 13 <= Teenager < 18 <= Adult < 60 <= Senior < 120 <= Error For example: • ageclass(25) • Adult

More Related