A bit more about variables
description
Transcript of A bit more about variables
![Page 1: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/1.jpg)
A bit more about variables>> var1 = [ 1 2 3]var1 = 1 2 3
>> var2 = var1var2 = 1 2 3
>> var1 = [4 5 6]var1 = 4 5 6
>> var2var2 = 1 2 3
![Page 2: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/2.jpg)
>> var3 = 'Mostly Harmless'
var3 =
Mostly Harmless
>> var3(2:9)
ans =
ostly Ha
Strings – vectors of characters
![Page 3: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/3.jpg)
Control Structures•Sequence
•Repetition
•Selection
![Page 4: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/4.jpg)
SequencePop1=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)
![Page 5: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/5.jpg)
Repetitions
![Page 6: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/6.jpg)
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.
![Page 7: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/7.jpg)
>> popDynam(23,0.2,0.1)
ans =
25.3000 4.6000 2.3000
>>
![Page 8: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/8.jpg)
popDynam([23 0.2 0.1])
An Alternative approach is to encapsulate the three parameters in a single element – a vector.
![Page 9: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/9.jpg)
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
![Page 10: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/10.jpg)
>> popDynam([23 0.2 0.1])
ans =
25.3000 4.6000 2.3000
>>
![Page 11: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/11.jpg)
The vector [23 0.2 0.1] represents the parameters
of a population.
How would we represent the parameters of three
populations?
![Page 12: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/12.jpg)
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 sizepopParam(:,2) birth ratepopParam(:,3) death rate
![Page 13: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/13.jpg)
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];
endend
![Page 14: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/14.jpg)
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];
endend
What is the output of this function?
![Page 15: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/15.jpg)
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];
endend
What is the output of this function?
![Page 16: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/16.jpg)
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];
endend
But the input matrix may be larger….
![Page 17: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/17.jpg)
Selectionif (Pop1>60)
deathRate=0.15
birthRate=0.12
end
![Page 18: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/18.jpg)
If-else-end
if (Pop1>60)deathRate=0.15birthRate=0.12
elsedeathRate=0.1birthRate=0.2
end
![Page 19: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/19.jpg)
If-elseif-endif (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
![Page 20: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/20.jpg)
Relational operators
<
>
<=
>=
== (equals)
~= (does not equal)
Logical operators
& (and)~ (not)| (or)
e.g.,:if ((A>B) | (B~=C))
![Page 21: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/21.jpg)
Logical expressions cab be interpreted
>> 10 > 1
ans =
1
>> 10 > 100
ans =
0
![Page 22: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/22.jpg)
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) > 30ans = 0 1 0
![Page 23: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/23.jpg)
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
![Page 24: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/24.jpg)
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
![Page 25: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/25.jpg)
A bug>> i = 10;>> while (i < 11)
disp(1/i);i = i - 1;
end
commands
![Page 26: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/26.jpg)
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
![Page 27: A bit more about variables](https://reader036.fdocuments.in/reader036/viewer/2022062500/56815706550346895dc4abda/html5/thumbnails/27.jpg)
>> i = 10;>> while (i < 11)
disp(1/i);if (isinf(1/i))
error('Bad number');endi = 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