fakultaet is factorial!
This commit is contained in:
parent
16a9567058
commit
86ba1bc0fe
@ -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
|
@ -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)
|
@ -1 +0,0 @@
|
||||
printfaculty(5);
|
@ -1,3 +0,0 @@
|
||||
n = 5
|
||||
a = faculty(n);
|
||||
fprintf('Faculty of %i is: %i\n', n, x)
|
7
programming/exercises/myfactorial.m
Normal file
7
programming/exercises/myfactorial.m
Normal 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
|
@ -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
|
||||
|
@ -1 +1 @@
|
||||
plotsine(5.0, 2.0, 1.5, 0.001)
|
||||
plotsine(5.0, 2.0, 1.5)
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user