1 / 60

Day 1 review

This text introduces the concepts of hardware, software, vectors, and matrices in Matlab. It covers topics such as script files, flow control, and creating variables using different commands.

jdotson
Télécharger la présentation

Day 1 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. Day 1 review • What is hardware. • What is software. • What is a vector. • What is a matrix. • Let's fire up Matlab.

  2. Day 2 plan • What is a script file. • Flow control(1): for loops. • Logic (tomorrow) • Let's fire up Matlab.

  3. What is a script file? Files of the type “.m” are simply a collection of orderly command- line instructions (scripting language) type: > edit moo.m and save it in your matlab directory.

  4. script files Inside moo.m type the following sequence of commands: >str='this is my first program'; Save your file, close it and on the command line, execute it by typing: >moo

  5. script files What happened? Why? Allow your program to show you its output and run it again.

  6. script files Change your program to this: str='this is my first program'; disp(str); Run moo Differences?

  7. clear all

  8. script files Erase everything on moo and type: a=[… 1 2 3 6 4 5 6 7.811]; Save moo and execute it. CHECK WORKSPACE

  9. script files Two types of m files: - programs (aka scripts) don't take inputs. don't produce outputs. but leave variables in workspace. - functions (Day 4). can take input (passing variables) can return outputs. variables are internal.

  10. script files (1) Write a program that creates a variable named u that has 10 rows, and one column using the function zeros

  11. script files (2) Add code that creates a variable v that has all the elements from 1 to 10, using the command: X = startN:Increment:endN

  12. script files (2) Finally, create a variable w that has zeros in its first column and the numbers 1 through 10 in its second column using the “concatenate” command [ ] concatenatedAB= [A B]; or concatenatedAB= [A;B]; You will also need the function transpose: ' example: if A=[1 2 3] B=A' produces B=[ 1 2 3]

  13. Flow control p.12 • REPEATING ORDERS: • 'FOR' loops • If you want to do something a fixed number of times: • you say “FOR the next ten times do this.”

  14. script files FOR EXAMPLE: UofI.m • WRITE A SCRIPT TO CALCULATE THE FIRST 10 numbers in the sequence:u(n) = u(n-1) x (n+1)with u(1) = 1 • What we want is a vector U with 10 rows, U(i) = u(i)

  15. script files This is a “list” the “FOR” command will run through… Any list will do! FOR syntax: • for n=first:increment:final . . . . . . end --> USE THE COMMAND “zeros” to get your variable U started. REMEMBER! Type n=1:10:100 on your command prompt What happened? And k=1212:-pi:0

  16. script files A NOTE ABOUT HELP The first continuous lines of commented text atop of your script are read as the “help” file for your program. Etiquette dictates that you specify at least: What the program does and the “how to” Who wrote it When it was last updated Quirks & bugs GO AHEAD AND INCLUDE HELP FOR UofI

  17. UofI.m % this is my first program, it does ... % created on august 4, 2005 by Alejo. u=zeros(10,1); u(1)=1; for i=2:10 u(i,1) = u(i-1,1) .*(i+1); %INDENT end; u .

  18. UofI.m Issue of first and last CASE!! In your mind, testing these two cases is critical!!!!

  19. UofI.m • It would be nice to add the index to U. So each row show index i and U(i). • Go ahead…

  20. UofI.m % this is my first program % created on august 4, 2005 U=zeros(10,2); U(1,1)=1; U(1,2)=1; for n=2:10 U(n,1) = U(n-1,1) .*(n+1); U(n,2) = n; end; U

  21. UofI.m It would be even nicer, if we asked the user the first value of the series. Use 'input'

  22. UofI.m Syntax: VAR = input('your text here'); (EVALUATED INPUT) For entering strings of charactersVAR = input('Your text here','s'); (NOT EVALUATED INPUT)

  23. UofI.m % this is my first program % created on august 4, 2005 U=zeros(10,2); U(1,1)=input('first value? '); U(1,2)=1; for n=2:10 U(n,1) = U(n-1,1) .*(n+1); U(n,2) = n; end; U

  24. UofI.m RUN UofI and as input put: 1220-1219 That's why it is evaluated input.

  25. Embedding for loops Multiplication table example. Write a 10 x 10 matrix such that each element is the product of the row and column number multable(i,j) = i*j;

  26. Multiplication table For i=1:10 for j=1:10 Multable(i,j)=i*j;end end

  27. A second look at for What is the variable: myname= 'ALEJO'; Answer: A list of 5 characters! Try switching the case of myname by adding 32 to each ascii code… use function “double”

  28. Check the Help of double What does it mean??? Well, a “character” is not a double, so double transforms it into its “number equivalent”, which is its ASCII code. So, asciinumberofa=double('a')

  29. How do we get the letter back? Try “edit char” so, now type: backtoa=char(asciinumberofa)

  30. A second look at “for” What is the variable: myname= 'ALEJO'; Answer: A list of 5 characters! Try switching the case of myname by adding 32 to each ascii code…

  31. A second look at for Try switching the case of myname by adding 32 to each ascii code… (edit moo2.m) myname='ALEJO'; counter =1; for l=myname lowname(counter)=double(l)+32; counter = counter +1; end; lowname=char(lowname) Did the size of my name mattered??? Try running the program with: myname='Alejo'

  32. Without the “for” Try switching the case of myname by adding 32 to each ascii code… (edit moo2.m) myname='ALEJO'; asciiname=double(myname); newname=asciiname+32; lowname=char(newname); We've VECTORIZED the For Loop!An order of magnitude faster. Or EMBEDDING:lowname=char(double(myname)+32);

  33. One more example: WRITE Moo3.m in which you add all the elements of a two dimensional matrix entered by the user. USING: “input” for loop “size” (help size)

  34. Moo3.m matrix=input('enter two dimensional matrix '); s=size(matrix); tot=0; for i=1:s(1) for j=1:s(2) tot=matrix(i,j)+tot end end WORK ON A PIECE OF PAPER THE STEPS OF THIS FOR LOOP.

  35. Moo3.m matrix=input('enter two dimensional matrix '); s=size(matrix); tot=0; what is sum(matrix)? %help sum so what is tot=sum(sum(matrix));

  36. Multiplication table Can you do it with two embedded loops now?

  37. Multiplication table For i=1:10 for j=1:10 Multable(i,j)=i*j;end end

  38. LOGIC p.8 AND: & OR: | Complement: ~ Exclusive OR: xor

  39. LOGIC p.8 What is TRUE in Matlab? NON ZERO ELEMENTS. TRY: a=[0 1 1 0 1]; b=[1 1 0 0 234009]; c = a&b d = a|b e = ~a f = xor(a,b)

  40. LOGIC p.8 Question (without typing): Mylove=100009999; Yourlove=0; Ourlove = xor(Mylove,Yourlove); Whose love is true?

  41. LOGIC p.8 BRUSH UP ON YOUR LOGIC!! --> YOU WILL MAKE LOCIGAL MISTAKES and you'll think your computer has gone crazy. … humbling experience -->TEST AND RETEST LOGICAL STATEMENTS. -->DO NOT MAKE CONVOLUTATED logical statements in one swoop:such as “if (this is true) or (that is wrong while x is also true) but not y”.

  42. IF IF something is true do {list of commands} Else { list of commands2} end

  43. IF syntax if (conditional statement) … elseif (conditional statement) … else … end Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…

  44. IF syntax So… if (Mylove) Mylove=0; else Yourlove=0; end; Whose love is true?

  45. IF syntax So… if (Mylove == true) % == is not = Mylove=0; else yourlove=0; end; Whose love is true? WHY????%help true

  46. IF exercise Ask user for password. If password is less than 6 characters long, return "invalid password"

  47. IF exercise Algorithm steps: 1. ask for user input (password) 2. check size of password 3. if size is too small, reject password

  48. IF exercise > pwd = input('Please enter password: '); pwdsize = length(pwd); %check size of pwdsize if (pwdsize < 6) 'password invalid' else 'valid password' end

  49. SWITCH: a more convenient if If you know in advance SEVERAL OUTCOMES ARE POSSIBLE, use SWITCH. For example: ask user to choose a number from 1 to 10, each number prints an associated joke.

  50. SWITCH: syntax Something like: choice = input('number from 1 to 10? ') switch choice case 1 … case 2 … … otherwise … end. Choice is the name of the variable that can take the values listed after the “case” word.

More Related