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 % plot a sine wave
% freq: frequency of the sinewave in Hertz % freq: frequency of the sinewave in Hertz
% ampl: amplitude of the sinewave % ampl: amplitude of the sinewave
% duration: duration of the sinewave in seconds % duration: duration of the sinewave in seconds
% step: stepsize for plotting in seconds step = 0.01/freq;
time = 0:step:duration; time = 0:step:duration;
sine = ampl*sin(2*pi*freq*time); sine = ampl*sin(2*pi*freq*time);
if duration <= 1.0 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; freq = 5.0;
ampl = 2.0; ampl = 2.0;
[time, sine] = sinewave(freq, ampl, 1.5, 0.001); [time, sine] = sinewave(freq, ampl, 1.5);
if duration <= 1.0 if duration <= 1.0
plot(1000.0*time, sine); plot(1000.0*time, sine);

View File

@ -1,4 +1,4 @@
freq = 5.0; freq = 5.0;
ampl = 2.0; ampl = 2.0;
[time, sine] = sinewave(freq, ampl, 1.5, 0.001); [time, sine] = sinewave(freq, ampl, 1.5);
plotsinewave(time, sine); 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: vector with positions of the random walker
positions = [0.0]; positions = [0.0];
% positions = 0.0;
% positions = zeros(1, 1);
i = 2; i = 2;
while true while abs(positions(i-1)) < thresh
r = rand(1); r = rand(1);
if r < p if r < p
positions(i) = positions(i-1) + 1; positions(i) = positions(i-1) + 1;
else else
positions(i) = positions(i-1) - 1; positions(i) = positions(i-1) - 1;
end end
if abs(positions(i)) > thresh
break
end
i = i + 1; i = i + 1;
end end
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 \part Version 1: berechnet die Fakult\"at von 5 und gib das
Resultat auf dem Bildschirm aus. Resultat auf dem Bildschirm aus.
\begin{solution} \begin{solution}
\lstinputlisting{facultyscripta.m} \lstinputlisting{factorialscripta.m}
\end{solution} \end{solution}
\part Version 2: Wie 1 aber die Funktion \"ubernimmt als Argument \part Version 2: Wie 1 aber die Funktion \"ubernimmt als Argument
die Zahl, von der die Fakult\"at berechnet werden soll. die Zahl, von der die Fakult\"at berechnet werden soll.
\begin{solution} \begin{solution}
\lstinputlisting{printfaculty.m} \lstinputlisting{printfactorial.m}
\lstinputlisting{facultyscriptb.m} \lstinputlisting{factorialscriptb.m}
\end{solution} \end{solution}
\part Version 3: Wie 2 aber mit R\"uckgabe des berechneten Wertes. \part Version 3: Wie 2 aber mit R\"uckgabe des berechneten Wertes.
\begin{solution} \begin{solution}
\lstinputlisting{faculty.m} \lstinputlisting{myfactorial.m}
\lstinputlisting{facultyscriptc.m} \lstinputlisting{factorialscriptc.m}
\end{solution} \end{solution}
\end{parts} \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 % compute sine wave with time axis
% freq: frequency of the sinewave in Hertz % freq: frequency of the sinewave in Hertz
% ampl: amplitude of the sinewave % ampl: amplitude of the sinewave
% duration: duration of the sinewave in seconds % duration: duration of the sinewave in seconds
% step: stepsize for plotting in seconds
% returns: % returns:
% time: vector of time points % time: vector of time points
% sine: corresponding vector with the sine wave % sine: corresponding vector with the sine wave
step = 0.01/freq;
time = 0:step:duration; time = 0:step:duration;
sine = ampl*sin(2*pi*freq*time); sine = ampl*sin(2*pi*freq*time);
end end