diff --git a/pointprocesses/lecture/pointprocessscetchA.eps b/pointprocesses/lecture/pointprocessscetchA.eps index 6159193..1dfe6f5 100644 --- a/pointprocesses/lecture/pointprocessscetchA.eps +++ b/pointprocesses/lecture/pointprocessscetchA.eps @@ -1,7 +1,7 @@ %!PS-Adobe-2.0 EPSF-2.0 %%Title: pointprocessscetchA.tex %%Creator: gnuplot 4.6 patchlevel 4 -%%CreationDate: Fri Sep 30 10:14:40 2016 +%%CreationDate: Sat Nov 19 10:17:42 2016 %%DocumentFonts: %%BoundingBox: 50 50 373 135 %%EndComments @@ -430,10 +430,10 @@ SDict begin [ /Title (pointprocessscetchA.tex) /Subject (gnuplot plot) /Creator (gnuplot 4.6 patchlevel 4) - /Author (grewe) + /Author (jan) % /Producer (gnuplot) % /Keywords () - /CreationDate (Fri Sep 30 10:14:40 2016) + /CreationDate (Sat Nov 19 10:17:42 2016) /DOCINFO pdfmark end } ifelse diff --git a/pointprocesses/lecture/pointprocessscetchA.pdf b/pointprocesses/lecture/pointprocessscetchA.pdf index eb57b5b..b532798 100644 Binary files a/pointprocesses/lecture/pointprocessscetchA.pdf and b/pointprocesses/lecture/pointprocessscetchA.pdf differ diff --git a/pointprocesses/lecture/pointprocessscetchB.eps b/pointprocesses/lecture/pointprocessscetchB.eps index edfe69a..1856ba8 100644 --- a/pointprocesses/lecture/pointprocessscetchB.eps +++ b/pointprocesses/lecture/pointprocessscetchB.eps @@ -1,7 +1,7 @@ %!PS-Adobe-2.0 EPSF-2.0 %%Title: pointprocessscetchB.tex %%Creator: gnuplot 4.6 patchlevel 4 -%%CreationDate: Fri Sep 30 10:14:40 2016 +%%CreationDate: Sat Nov 19 10:17:43 2016 %%DocumentFonts: %%BoundingBox: 50 50 373 237 %%EndComments @@ -430,10 +430,10 @@ SDict begin [ /Title (pointprocessscetchB.tex) /Subject (gnuplot plot) /Creator (gnuplot 4.6 patchlevel 4) - /Author (grewe) + /Author (jan) % /Producer (gnuplot) % /Keywords () - /CreationDate (Fri Sep 30 10:14:40 2016) + /CreationDate (Sat Nov 19 10:17:43 2016) /DOCINFO pdfmark end } ifelse diff --git a/pointprocesses/lecture/pointprocessscetchB.pdf b/pointprocesses/lecture/pointprocessscetchB.pdf index 8a506c9..6e4e783 100644 Binary files a/pointprocesses/lecture/pointprocessscetchB.pdf and b/pointprocesses/lecture/pointprocessscetchB.pdf differ diff --git a/programming/code/boltzmann.m b/programming/code/boltzmann.m new file mode 100644 index 0000000..f46d0fb --- /dev/null +++ b/programming/code/boltzmann.m @@ -0,0 +1,8 @@ +function y = boltzmann( x, k ) +% computes the boltzmann function +% x: scalar or vector of x values +% k: slope parameter of boltzman function +% returns y-values of boltzmann function +y = 1./(1+exp(-k*x)); +end + diff --git a/programming/code/plotboltzmann.m b/programming/code/plotboltzmann.m new file mode 100644 index 0000000..5fb919c --- /dev/null +++ b/programming/code/plotboltzmann.m @@ -0,0 +1,3 @@ +p = boltzmann(voltage, a); +plot(voltage, p) +y = -1; diff --git a/programming/exercises/faculty.m b/programming/exercises/faculty.m new file mode 100644 index 0000000..41992ac --- /dev/null +++ b/programming/exercises/faculty.m @@ -0,0 +1,8 @@ +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 \ No newline at end of file diff --git a/programming/exercises/facultyscripta.m b/programming/exercises/facultyscripta.m new file mode 100644 index 0000000..be0ed55 --- /dev/null +++ b/programming/exercises/facultyscripta.m @@ -0,0 +1,6 @@ +n = 5; +x = 1; +for i = 1:n + x = x * i; +end +fprintf('Faculty of %i is: %i\n', n, x) diff --git a/programming/exercises/facultyscriptb.m b/programming/exercises/facultyscriptb.m new file mode 100644 index 0000000..8488431 --- /dev/null +++ b/programming/exercises/facultyscriptb.m @@ -0,0 +1 @@ +printfaculty(5); diff --git a/programming/exercises/facultyscriptc.m b/programming/exercises/facultyscriptc.m new file mode 100644 index 0000000..0804287 --- /dev/null +++ b/programming/exercises/facultyscriptc.m @@ -0,0 +1,3 @@ +n = 5 +a = faculty(n); +fprintf('Faculty of %i is: %i\n', n, x) diff --git a/programming/exercises/plotsine.m b/programming/exercises/plotsine.m new file mode 100644 index 0000000..18f562e --- /dev/null +++ b/programming/exercises/plotsine.m @@ -0,0 +1,19 @@ +function plotsine(freq, ampl, duration, step) +% 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 +time = 0:step:duration; +sine = ampl*sin(2*pi*freq*time); +if duration <= 1.0 + plot(1000.0*time, sine); + xlabel('Time [ms]'); +else + plot(time, sine); + xlabel('Time [s]'); +end +ylim([-1.2*ampl 1.2*ampl]); +ylabel('Sinewave'); +title(sprintf('Frequency %g Hz', freq)); +end diff --git a/programming/exercises/plotsine50.m b/programming/exercises/plotsine50.m new file mode 100644 index 0000000..19e74ae --- /dev/null +++ b/programming/exercises/plotsine50.m @@ -0,0 +1,9 @@ +function plotsine50() +% plot a sine wave of 50Hz +time = 0:0.0001:0.2; +sine = sin(2*pi*50.0*time); +plot(1000.0*time, sine); +xlabel('Time [ms]'); +ylim([-1.2 1.2]); +ylabel('Sinewave'); +end diff --git a/programming/exercises/plotsinea.m b/programming/exercises/plotsinea.m new file mode 100644 index 0000000..0c64eea --- /dev/null +++ b/programming/exercises/plotsinea.m @@ -0,0 +1 @@ +plotsine50() diff --git a/programming/exercises/plotsineb.m b/programming/exercises/plotsineb.m new file mode 100644 index 0000000..b7cb2b8 --- /dev/null +++ b/programming/exercises/plotsineb.m @@ -0,0 +1 @@ +plotsine(5.0, 2.0, 1.5, 0.001) diff --git a/programming/exercises/plotsinec.m b/programming/exercises/plotsinec.m new file mode 100644 index 0000000..6e4bd8c --- /dev/null +++ b/programming/exercises/plotsinec.m @@ -0,0 +1,14 @@ +freq = 5.0; +ampl = 2.0; +[time, sine] = sinewave(freq, ampl, 1.5, 0.001); + +if duration <= 1.0 + plot(1000.0*time, sine); + xlabel('Time [ms]'); +else + plot(time, sine); + xlabel('Time [s]'); +end +ylim([-1.2*ampl 1.2*ampl]); +ylabel('Sinewave'); +title(sprintf('Frequency %g Hz', freq)); diff --git a/programming/exercises/plotsined.m b/programming/exercises/plotsined.m new file mode 100644 index 0000000..d6bf5d8 --- /dev/null +++ b/programming/exercises/plotsined.m @@ -0,0 +1,4 @@ +freq = 5.0; +ampl = 2.0; +[time, sine] = sinewave(freq, ampl, 1.5, 0.001); +plotsinewave(time, sine); diff --git a/programming/exercises/plotsinewave.m b/programming/exercises/plotsinewave.m new file mode 100644 index 0000000..f2960da --- /dev/null +++ b/programming/exercises/plotsinewave.m @@ -0,0 +1,13 @@ +function plotsinewave(time, sine) +% plot precomputed sinewave +% time: vector with timepoints +% sine: corresponding vector with sinewave +if time(end)-time(1) <= 1.0 + plot(1000.0*time, sine); + xlabel('Time [ms]'); +else + plot(time, sine); + xlabel('Time [s]'); +end +ylim([1.2*min(sine) 1.2*max(sine)]); +ylabel('Sinewave'); diff --git a/programming/exercises/printfaculty.m b/programming/exercises/printfaculty.m new file mode 100644 index 0000000..bb3fa1f --- /dev/null +++ b/programming/exercises/printfaculty.m @@ -0,0 +1,8 @@ +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 \ No newline at end of file diff --git a/programming/exercises/randomwalk.m b/programming/exercises/randomwalk.m new file mode 100644 index 0000000..5bde73d --- /dev/null +++ b/programming/exercises/randomwalk.m @@ -0,0 +1,13 @@ +function [time, position] = randomwalk(numbersteps) +% 1-D random walk for numbersteps time steps +time = 1:numbersteps; +position = zeros(numbersteps, 1); +for i = time(2:end) + r = rand(1); + if r > 0.5 + position(i) = position(i-1) + 1; + else + position(i) = position(i-1) - 1; + end +end +end diff --git a/programming/exercises/randomwalkscript.m b/programming/exercises/randomwalkscript.m new file mode 100644 index 0000000..15cff7c --- /dev/null +++ b/programming/exercises/randomwalkscript.m @@ -0,0 +1,7 @@ +n = 2000; +hold on +for k = 1:10 + [t, x] = randomwalk(n); + plot(t, x) +end +hold off diff --git a/programming/exercises/randomwalkscriptb.m b/programming/exercises/randomwalkscriptb.m new file mode 100644 index 0000000..d124e0f --- /dev/null +++ b/programming/exercises/randomwalkscriptb.m @@ -0,0 +1,8 @@ +p = 0.5; +thresh = 50.0; +hold on +for k = 1:30 + x = randomwalkthresh(p, thresh); + plot(x) +end +hold off diff --git a/programming/exercises/randomwalkscriptc.m b/programming/exercises/randomwalkscriptc.m new file mode 100644 index 0000000..7f85de4 --- /dev/null +++ b/programming/exercises/randomwalkscriptc.m @@ -0,0 +1,20 @@ +thresh = 50.0; +probs = [0.5 0.52 0.55 0.6]; +maxt = 0; +for sp = 1:4 + p = probs(sp); + subplot(2, 2, sp); + hold on + for k = 1:30 + x = randomwalkthresh(p, thresh); + if maxt < length(x) + maxt = length(x); + end + plot(x) + end + hold off + title(sprintf('p=%g', p)) + xlabel('Time') + xlim([0 maxt]) + ylabel('Position') +end diff --git a/programming/exercises/randomwalkscriptd.m b/programming/exercises/randomwalkscriptd.m new file mode 100644 index 0000000..5cb49bc --- /dev/null +++ b/programming/exercises/randomwalkscriptd.m @@ -0,0 +1,23 @@ +thresh = 50.0; +probs = 0.5:0.01:1.0; +reps = 1000; +meancount = zeros(length(probs), 1); +stdcount = zeros(length(probs), 1); +for sp = 1:length(probs) + p = probs(sp); + positions = zeros(reps, 1); + for k = 1:reps + x = randomwalkthresh(p, thresh); + positions(k) = length(x); + end + meancount(sp) = mean(positions); + stdcount(sp) = std(positions); +end +semilogy(probs, meancount, 'displayname', 'mean'); +hold on; +semilogy(probs, stdcount, 'displayname', 'std'); +hold off; +xlabel('p'); +xlim([0.5 1.0]) +ylabel('number of steps'); +legend('show'); \ No newline at end of file diff --git a/programming/exercises/randomwalkthresh.m b/programming/exercises/randomwalkthresh.m new file mode 100644 index 0000000..15af9e5 --- /dev/null +++ b/programming/exercises/randomwalkthresh.m @@ -0,0 +1,25 @@ +function positions = randomwalkthresh(p, thresh) +% computes a single random walk +% +% Arguments: +% p: the probability for an upward step +% thresh: compute the random walk until abs(pos) is larger than thresh +% +% Returns: +% positions: vector with positions of the random walker + +positions = [0.0]; +i = 2; +while true + 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 diff --git a/programming/exercises/scripts_functions.tex b/programming/exercises/scripts_functions.tex index d76be49..22975d3 100644 --- a/programming/exercises/scripts_functions.tex +++ b/programming/exercises/scripts_functions.tex @@ -1,7 +1,9 @@ -\documentclass[12pt,a4paper,pdftex]{exam} +%\documentclass[12pt,a4paper,pdftex]{exam} +\documentclass[answers,12pt,a4paper,pdftex]{exam} \usepackage[german]{babel} \usepackage{natbib} +\usepackage{xcolor} \usepackage{graphicx} \usepackage[small]{caption} \usepackage{sidecap} @@ -24,6 +26,27 @@ \setlength{\parskip}{0.3cm} \renewcommand{\baselinestretch}{1.15} + +%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\usepackage{listings} +\lstset{ + language=Matlab, + basicstyle=\ttfamily\footnotesize, + numbers=left, + numberstyle=\tiny, + title=\lstname, + showstringspaces=false, + commentstyle=\itshape\color{darkgray}, + breaklines=true, + breakautoindent=true, + columns=flexible, + frame=single, + xleftmargin=1em, + xrightmargin=1em, + aboveskip=10pt +} + + \newcommand{\code}[1]{\texttt{#1}} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -49,19 +72,52 @@ also als zip-Archiv auf ILIAS hochladen. Das Archiv sollte nach dem Muster: \begin{parts} \part Version 1: berechnet die Fakult\"at von 5 und gib das Resultat auf dem Bildschirm aus. + \begin{solution} + \lstinputlisting{facultyscripta.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} + \end{solution} + \part Version 3: Wie 2 aber mit R\"uckgabe des berechneten Wertes. + \begin{solution} + \lstinputlisting{faculty.m} + \lstinputlisting{facultyscriptc.m} + \end{solution} + \end{parts} \question Implementiere eine Funktion, die einen Sinus mit der Amplitude 1 und der Frequenz $f = $ 50\,Hz plottet ($sin(2\pi \cdot f \cdot t)$): + \begin{solution} + \lstinputlisting{plotsine50.m} + \lstinputlisting{plotsinea.m} + \end{solution} \begin{parts} \part Erweitere die Funktion sodass die L\"ange der Zeitachse, die Schrittweite, Amplitude, Frequenz als Argumente \"ubergeben werden k\"onnen. + \begin{solution} + \lstinputlisting{plotsine.m} + \lstinputlisting{plotsineb.m} + \end{solution} + \part Gib sowohl den Sinus als auch die Zeitachse zur\"uck. + \begin{solution} + \lstinputlisting{sinewave.m} + \lstinputlisting{plotsinec.m} + \end{solution} + + \part Extra plot Funktion. + \begin{solution} + \lstinputlisting{plotsinewave.m} + \lstinputlisting{plotsined.m} + \end{solution} \end{parts} %\question Schreibe eine Funktion, die bin\"are Datens\"atze @@ -80,15 +136,33 @@ also als zip-Archiv auf ILIAS hochladen. Das Archiv sollte nach dem Muster: \begin{parts} \part \"Uberlege Dir ein geeignetes ``Programmlayout'' aus Funktionen und Skripten. + \begin{solution} + One function that computes one realisation of a random walk. + Scripts for plotting and analysis. + \end{solution} + \part Implementiere die L\"osung. + \begin{solution} + \lstinputlisting{randomwalkthresh.m} + \lstinputlisting{randomwalkscriptb.m} + \end{solution} + \part Simuliere 30 Realisationen des random walk pro Wahrscheinlichkeit. + \part Es sollen die Positionen als Funktion der Schrittanzahl geplottet werden. Erstelle einen Plot mit den je 30 Wiederholungen pro Wahrscheinlichkeitsstufe. + \begin{solution} + \lstinputlisting{randomwalkscriptc.m} + \end{solution} + \part Wie entwickelt sich die mittlere ben\"otigte Schrittanzahl in Abh\"angigkeit der Wahrscheinlichkeit? Stelle die Mittelwerte und die Standardabweichungen graphisch dar. + \begin{solution} + \lstinputlisting{randomwalkscriptd.m} + \end{solution} \end{parts} %\question Modellierung des exponentiellen Wachstums einer isolierten diff --git a/programming/exercises/sinewave.m b/programming/exercises/sinewave.m new file mode 100644 index 0000000..42f9362 --- /dev/null +++ b/programming/exercises/sinewave.m @@ -0,0 +1,12 @@ +function [time, sine] = sinewave(freq, ampl, duration, step) +% 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 +time = 0:step:duration; +sine = ampl*sin(2*pi*freq*time); +end diff --git a/scientificcomputing-script.tex b/scientificcomputing-script.tex index 61df5a6..b8533c8 100644 --- a/scientificcomputing-script.tex +++ b/scientificcomputing-script.tex @@ -2,7 +2,7 @@ \input{header} -\setcounter{maxexercise}{0} % show listings up to exercise maxexercise +\setcounter{maxexercise}{1000} % show listings up to exercise maxexercise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%