fakultaet is factorial!

This commit is contained in:
Jan Benda 2016-11-22 09:26:50 +01:00
parent 16a9567058
commit 86ba1bc0fe
13 changed files with 22 additions and 42 deletions

View File

@ -1,8 +0,0 @@
function x = faculty(n)
% return the faculty of n
x = 1;
for i = 1:n
x = x * i;
end
% x = prod(1:n) % this is a one line alternative to the for loop!
end

View File

@ -1,6 +0,0 @@
n = 5;
x = 1;
for i = 1:n
x = x * i;
end
fprintf('Faculty of %i is: %i\n', n, x)

View File

@ -1 +0,0 @@
printfaculty(5);

View File

@ -1,3 +0,0 @@
n = 5
a = faculty(n);
fprintf('Faculty of %i is: %i\n', n, x)

View File

@ -0,0 +1,7 @@
function x = mufactorial(n)
% return the factorial of n
x = 1;
for i = 1:n
x = x * i;
end
end

View File

@ -1,9 +1,9 @@
function plotsine(freq, ampl, duration, step)
function plotsine(freq, ampl, duration)
% plot a sine wave
% freq: frequency of the sinewave in Hertz
% ampl: amplitude of the sinewave
% duration: duration of the sinewave in seconds
% step: stepsize for plotting in seconds
step = 0.01/freq;
time = 0:step:duration;
sine = ampl*sin(2*pi*freq*time);
if duration <= 1.0

View File

@ -1 +1 @@
plotsine(5.0, 2.0, 1.5, 0.001)
plotsine(5.0, 2.0, 1.5)

View File

@ -1,6 +1,6 @@
freq = 5.0;
ampl = 2.0;
[time, sine] = sinewave(freq, ampl, 1.5, 0.001);
[time, sine] = sinewave(freq, ampl, 1.5);
if duration <= 1.0
plot(1000.0*time, sine);

View File

@ -1,4 +1,4 @@
freq = 5.0;
ampl = 2.0;
[time, sine] = sinewave(freq, ampl, 1.5, 0.001);
[time, sine] = sinewave(freq, ampl, 1.5);
plotsinewave(time, sine);

View File

@ -1,8 +0,0 @@
function printfaculty(n)
% compute the faculty of n and print it
x = 1;
for i = 1:n
x = x * i;
end
fprintf('Faculty of %i is: %i\n', n, x)
end

View File

@ -9,17 +9,16 @@ function positions = randomwalkthresh(p, thresh)
% positions: vector with positions of the random walker
positions = [0.0];
% positions = 0.0;
% positions = zeros(1, 1);
i = 2;
while true
while abs(positions(i-1)) < thresh
r = rand(1);
if r < p
positions(i) = positions(i-1) + 1;
else
positions(i) = positions(i-1) - 1;
end
if abs(positions(i)) > thresh
break
end
i = i + 1;
end
end

View File

@ -73,20 +73,20 @@ also als zip-Archiv auf ILIAS hochladen. Das Archiv sollte nach dem Muster:
\part Version 1: berechnet die Fakult\"at von 5 und gib das
Resultat auf dem Bildschirm aus.
\begin{solution}
\lstinputlisting{facultyscripta.m}
\lstinputlisting{factorialscripta.m}
\end{solution}
\part Version 2: Wie 1 aber die Funktion \"ubernimmt als Argument
die Zahl, von der die Fakult\"at berechnet werden soll.
\begin{solution}
\lstinputlisting{printfaculty.m}
\lstinputlisting{facultyscriptb.m}
\lstinputlisting{printfactorial.m}
\lstinputlisting{factorialscriptb.m}
\end{solution}
\part Version 3: Wie 2 aber mit R\"uckgabe des berechneten Wertes.
\begin{solution}
\lstinputlisting{faculty.m}
\lstinputlisting{facultyscriptc.m}
\lstinputlisting{myfactorial.m}
\lstinputlisting{factorialscriptc.m}
\end{solution}
\end{parts}

View File

@ -1,12 +1,12 @@
function [time, sine] = sinewave(freq, ampl, duration, step)
function [time, sine] = sinewave(freq, ampl, duration)
% compute sine wave with time axis
% freq: frequency of the sinewave in Hertz
% ampl: amplitude of the sinewave
% duration: duration of the sinewave in seconds
% step: stepsize for plotting in seconds
% returns:
% time: vector of time points
% sine: corresponding vector with the sine wave
step = 0.01/freq;
time = 0:step:duration;
sine = ampl*sin(2*pi*freq*time);
end