Mathematics,
Chalmers University of Technology
and
Göteborg University
#### Matlab exercise 2

(written by Kenneth Eriksson, modified by Stig Larsson)
Home,
Matlab exercise 1,
Matlab exercise 2,
Matlab exercise 3.

False (=0) / True (=1):

>> 2<3

>> 2>3

>> -3<2

>> 1+1 = = 2

>> 2 = = 3

>> 2 ~= 3

==================

Conditional statements:

>> i=1;

>> if i = = 1 disp('i is one'); end

>> i=2

>> if i = = 1 disp('i is one'); end

>> if i = = 1 disp('i is one'); else disp('i is not one'); end

>> help if

======================

Script files:

>> edit

(type disp('hi') and save file as hello.m)

>> hello

Replace disp('hi') by code that sorts a list/vector v=[a b],

that is, gives v=[a b] if a < b and v=[b a] if b < a, for example

`
if v(1) < v(2)`

v=v;

elseif v(2) < v(1)

v=[v(2) v(1)];

end

v

or more elegantly

`
if v(2) < v(1)`

v=[v(2) v(1)];

end

v

and save as MyFirstSort.m. Test the script file with

>> v=[2 1]

>> MyFirstSort

>> MyFirstSort

What happens in the two code versions if v(1)=v(2)?

===================

Loops, for and while:

Write code that makes the computer count to a given number N, for
example.

`
i=0;`

while i < N

i=i+1;

disp(i)

end

Save as CountToN.m. Test with

>> N=10;

>> CountToN

Alternative code construction

`
for i=1:N`

disp(i)

end

Save as CountToN2.m and test with

>> CountToN2

Exercises:

1. a) Get the computer to count only the odd numbers up to N. Hint:
elegant to use the for loop with i=1:2:N.

b) Get the computer to count backwards from N down to 1, first with
a "for loop" (hint: elegant to just replace i=1:N by i=N:-1:1),
then with a "while loop".

2. Write code that for a given number N tests whether 1+2+..+N = N^2/2
by computing 1+2+..+N and N^2/2, and display their difference and
ratio. What is the ratio for N=1000? Explain why two numbers with a
considerable difference can have a ratio very close to one!

3. Given natural numbers m and n, have matlab find p and r with 0<=r < n
such that m=pn+r. Try the following code

`
p=0;`

r=m;

while r>=n

p=p+1;

r=m-p*n;

end

disp(['m=' num2str(p) 'n+' num2str(r)])

4. Check if a turtle starting at 0 ever reaches 1. Consider and test
the following code and explain the outcome!

`
pos=0;`

while pos<1

step=(1-pos)/2;

pos=pos+step;

disp(pos)

end

5. How would the following code work?

`
while 1<2`

disp('this will go on forever')

end

Try it! If you run into problem you can always press Ctrl-C

6. Try and then seek to understand the following code?

`
while 1`

disp('What is 3+4?')

answer=input('');

if answer == 7

disp('Correct')

return

else

disp('Wrong, but you get another chance.')

end

end

>> help input

============================

Computer arithmetic:

>> eps

(a very small positive number in the computer. The smallest?)

>> eps/2

>> eps/100000000000000

>> help eps

>> 1+eps = = 1

>> 1+eps/2 = = 1

>> a=10

>> a=a^2

(repeat, first until matlab changes to number representation

of the form d.dddde+ddd, and make sure you can interprete this,

then until matlab gets exhausted ..)

>> Inf=Inf+1

>> Inf*2

>> Inf/2

>> 1/eps

>> 1/0

>> -1/0

(seems as if 0 is somewhat
positive after all, strange..)

>> 0/0

(Not a Number, without any reasonable interpretation..)

>> Inf-Inf

>> Inf/Inf

>> 0*Inf

Exercises:

1. Seek the largest number less than Inf, according to matlab.

========================

Composite conditions:

>> 0<=1

>> 1<=1

>> 1<2 | 1>2

>> 1<2 & 1>2

>> ~(1<2)

Last modified: Sat Sep 8 18:21:14 MET DST 2001