1 / 27

A bit more about variables

A bit more about variables. >> var1 = [ 1 2 3] var1 = 1 2 3 >> var2 = var1 var2 = 1 2 3 >> var1 = [4 5 6] var1 = 4 5 6 >> var2 var2 = 1 2 3. Strings – vectors of characters. >> var3 = 'Mostly Harmless' var3 = Mostly Harmless

kamuzu
Télécharger la présentation

A bit more about variables

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. A bit more about variables >> var1 = [ 1 2 3] var1 = 1 2 3 >> var2 = var1 var2 = 1 2 3 >> var1 = [4 5 6] var1 = 4 5 6 >> var2 var2 = 1 2 3

  2. Strings – vectors of characters >> var3 = 'Mostly Harmless' var3 = Mostly Harmless >> var3(2:9) ans = ostly Ha

  3. Control Structures • Sequence • Repetition • Selection

  4. Sequence Pop1=23 birthRate=0.2 deathRate=0.1 birth=Pop1 * birthRate death=Pop1 * deathRate change=birth - death Pop2=Pop1 + change (The commands are executed one after the other)

  5. Repetitions

  6. Reminder function pop = popDynam(popSize1, birthRate, deathRate) birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop = [popSize2 birth death]; end Please note the indentation.

  7. >> popDynam(23,0.2,0.1) ans = 25.3000 4.6000 2.3000 >>

  8. An Alternative approach is to encapsulate the three parameters in a single element – a vector. popDynam([23 0.2 0.1])

  9. function pop=popDynam(popParam) popSize1 = popParam(1); birthRate = popParam(2); deathRate = popParam(3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop = [popSize2 birth death]; end

  10. >> popDynam([23 0.2 0.1]) ans = 25.3000 4.6000 2.3000 >>

  11. The vector [23 0.2 0.1] represents the parameters of a population. How wouldwerepresent the parameters of three populations?

  12. popParam = [23 0.2 0.1; 38 0.25 0.05; 17 0.5 0.4] popParam = 23.0000 0.2000 0.1000 38.0000 0.2500 0.0500 17.0000 0.5000 0.4000 >> popParam(:,1) population size popParam(:,2) birth rate popParam(:,3) death rate

  13. function pop=popDynam(popParam) foriPop = 1:3 popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end end

  14. What is the output of this function? function pop=popDynam(popParam) foriPop = [1 2 3] popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end end

  15. What is the output of this function? function pop=popDynam(popParam) foriPop = [1 3] popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end end

  16. But the input matrix may be larger…. function pop=popDynam(popParam) foriPop = 1:size(popParam,1) popSize1 = popParam(iPop,1); birthRate = popParam(iPop,2); deathRate = popParam(iPop,3); birth = popSize1 * birthRate; death = popSize1 * deathRate; change = birth - death; popSize2 = popSize1 + change; pop(iPop,:) = [popSize2 birth death]; end end

  17. Selection • if (Pop1>60) • deathRate=0.15 • birthRate=0.12 • end

  18. If-else-end • if (Pop1>60) • deathRate=0.15 • birthRate=0.12 • else • deathRate=0.1 • birthRate=0.2 • end

  19. If-elseif-end • if (Pop1>60) • deathRate=0.15 • birthRate=0.12 • elseif ((Pop1>40) & (Pop1<61)) • deathRate=0.13 • birthRate=0.18 • else • deathRate=0.1 • birthRate=0.2 • end and

  20. Relational operators < > <= >= == (equals) ~= (does not equal) Logical operators & (and) ~ (not) | (or) e.g.,: if ((A>B) | (B~=C))

  21. Logical expressions cab be interpreted >> 10 > 1 ans = 1 >> 10 > 100 ans = 0

  22. Logical expressions cab be applied to vectors and matrices >> popParam popParam = 23.0000 0.2000 0.1000 38.0000 0.2500 0.0500 17.0000 0.5000 0.4000 >> popParam(:,1) > 30 ans = 0 1 0

  23. The “find” command >> popParam popParam = 23.0000 0.2000 0.1000 38.0000 0.2500 0.0500 17.0000 0.5000 0.4000 >> find( popParam(:,1) > 30) ans = 2

  24. Repetition with logical condition >> i = 10; >> while (i > 0) disp(1/i); i = i - 1; end 0.1000 0.1111 0.1250 0.1429 0.1667 0.2000 0.2500 0.3333 0.5000 1 >> commands output

  25. A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end commands

  26. A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end 0.1000 0.1111 0.1250 0.1429 0.1667 0.2000 0.2500 0.3333 0.5000 1 Inf -1 -0.5000 -0.3333 commands

  27. >> i = 10; >> while (i < 11) disp(1/i); if (isinf(1/i)) error('Bad number'); end i = i - 1; end 0.1000 0.1111 0.1250 0.1429 0.1667 0.2000 0.2500 0.3333 0.5000 1 Inf ??? Bad number

More Related