Matematiska vetenskaper, Chalmers Tekniska Högskola och Göteborgs Universitet

ALA-A, Matlab programmering, 2005

[home, week 1, week 2, week 3, week 4, week 5, week 6, week 7]

Läraren börjar med att lösa uppgift 1 på tavlan och på datorn. Betona följande:

1. Programmering är PROBLEMLÖSNING. Kräver fantasi, kreativitet, försök och misslyckande (trial and error).

2. Börja med papper och penna. Skissa problemets lösning på papper. Rita figurer. Vilka variabler behövs? I vilken ordning ska olika saker beräknas? Räkna igenom ett enkelt fall med papper och penna.
Vilka inbyggda matlab-funktioner kan användas? Hur fungerar de? Använd Helpdesk och Help.
Kolla på program som du skrivit tidigare för att se hur man gör diverse saker.

3. Skriv en liten del av programmet. Provkör i matlab. Övertyga dig om att programmet är korrekt innan du fortsätter. Du måste FÖRSTÅ allt du gör och vad matlab gör innan du går vidare. Använd din fantasi och uppfinningsförmåga för att hitta på sätt att testa.

4. Hitta på enkla test-exempel som du kan räkna ut med papper och penna för att kolla att programmet är korrekt. Hitta sedan på svåra exempel som inte går att räkna ut med papper och penna.  Experimentera, variera problemet, förstå. 

5. Dokumentera uppgiften. Dvs skriv ned vad du gjort på papper, inklusive algoritmen och test-exempel, spara m-filen, eventuella figurer, och så vidare. Så att du kan läsa på detta inför tentamen.

6.  Det är inte meningen att du ska hinna alla matlabövningar på studiolektionerna.  Du har tillgång till Chalmers datorsalar 24 timmar per dag, 7 dagar per vecka.  Utnyttja detta, det kommer du att behöva.  Jag hoppas att du även har tillgång till Matlab eller Octave på egen pc. 

Hur man installerar Octave på sin PC.

Ett annat alternativ ar programmet COMSOL Script, som liknar Matlab. Vi kommer snart kunna dela ut CD med studentlicens for COMSOL Script.

Problems

1. sumdivisible7

Write a matlab function sum=sumdivisible7(N) which computes the sum of all numbers from 0 to N which are divisible by 7. Hint: the function rem(x,y) computes the remainder of x/y in integer division.

2.  Mystery function

Explain to your friends what the following function does (without trying it in matlab).
function v2 = albert(v1)
% albert(v1)
%
% Mystery function, explain what it does!
% v1 and v2 are vectors

% Make v2 an empty vector
v2 = [];
j = 1;
for i = 1:length(v1)
if v1(i) > 13
v2(j) = v1(i);
j = j + 1;
end
end

3. The human computer

Perform with pen and paper all calculations that matlab does after the following commands.

>> a=funk(4)
>> b=funk(0)

What is the function supposed to calculate? The file funk.m is:

function y=funk(n)

if n==0
    y=1;
else
    y=1;
    for j=n:-1:2
        y=y*j;
    end
end

4. sumdiv

Write a function z=sumdiv(x,y) that computes the sum of the quotients of the elements of two vectors. The mathematical formula is x1/y1+x2/y2+... The function should ignore (skip) those terms that are undefined (because of zero denominator or because one vector has more elements than the other).

Hint: Write the function first for the simpler situation that the vectors have the same number of elements and that yi is never zero. Then consider the general situation. The following functions might be useful: length, isfinite

5.  sumseries1

Write a function sum=sumseries1(N) which computes the sum of the N first terms in the sequence:

1, 1/2, (1/2)2, ..., (1/2)N

6.  sumseries2

Write a function [sum,N]=sumseries2(tol) which computes the sum of the numbers in the sequence:

1, 1/2, (1/2)2, ..., (1/2)n, ...,

and stops when the next term is less than tol (a tolerance).  It should output the sum and the number of terms used. Use a while-loop.

7.  sumseries3

Write a function [sum,N,error]=sumseries3(a,tol) which computes the sum of the numbers in the sequence:

1, a, a2, ..., an, ...,

and stops when the next term is less than tol (a tolerance).  It should output the sum, and the number of terms used, and the error=|sum-1/(1-a)|.  Test with various values for the number a.   How fast is the convergence for different values of a?  Read about it in AMBS page 173.

8.  sumseries4, decimal expansion, Cauchy sequence

Modify the previous program to [sums,N,error]=sumseries4(a,tol) so that the output sums is a column vector of all the partial sums that are computed. If you type

>> format long; sums

you will see a table of the decimal expansion of the sum. Can you see how the decimal expansion grows (how the decimals are fixed)? Do you see the Cauchy sequence? Read about it in AMBS ch 14.5, 14.6, 15.1.

/stig


Last modified: Wed Sep 4 11:08:20 MET DST 2002