A bit more about variables >> var1 = [ 1 2 3] var1 = >> var2 = var1 var2 = >> var1 = [4 5 6] var1 = >> var2 var2 = 1 2 3
>> var3 = 'Mostly Harmless' var3 = Mostly Harmless >> var3(2:9) ans = ostly Ha Strings – vectors of characters
Control Structures Sequence Repetition Selection
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)
Repetitions
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.
>> popDynam(23,0.2,0.1) ans = >>
popDynam([ ]) An Alternative approach is to encapsulate the three parameters in a single element – a vector.
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
>> popDynam([ ]) ans = >>
The vector [ ] represents the parameters of a population. How would we represent the parameters of three populations?
popParam = [ ; ; ] popParam = >> popParam(:,1 ) population size popParam(:,2) birth rate popParam(:,3) death rate
function pop=popDynam(popParam) for iPop = 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
function pop=popDynam(popParam) for iPop = [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 What is the output of this function?
function pop=popDynam(popParam) for iPop = [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 What is the output of this function?
function pop=popDynam(popParam) for iPop = 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 But the input matrix may be larger….
Selection if (Pop1>60) deathRate=0.15 birthRate=0.12 end
If-else-end if (Pop1>60) deathRate=0.15 birthRate=0.12 else deathRate=0.1 birthRate=0.2 end
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
Relational operators < > <= >= == (equals) ~= (does not equal) Logical operators & (and) ~ (not) | (or) e.g.,: if ((A>B) | (B~=C))
Logical expressions cab be interpreted >> 10 > 1 ans = 1 >> 10 > 100 ans = 0
Logical expressions cab be applied to vectors and matrices >> popParam popParam = >> popParam(:,1) > 30 ans = 0 1 0
The “find” command >> popParam popParam = >> find( popParam(:,1) > 30) ans = 2
Repetition with logical condition >> i = 10; >> while (i > 0) disp(1/i); i = i - 1; end >> commands output
A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end commands
A bug >> i = 10; >> while (i < 11) disp(1/i); i = i - 1; end Inf commands
>> i = 10; >> while (i < 11) disp(1/i); if (isinf(1/i)) error('Bad number'); end i = i - 1; end Inf ??? Bad number