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.
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.
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
function y=funk(n)
if n==0
else
y=1;
for j=n:-1:2
y=y*j;
end
end
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
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
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.
[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.
[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