added solutions to function erecises

This commit is contained in:
Jan Benda 2016-11-19 12:04:08 +01:00
parent 778fde35fa
commit 1e0320ff8e
27 changed files with 288 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
p = boltzmann(voltage, a);
plot(voltage, p)
y = -1;

View File

@ -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

View File

@ -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)

View File

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

View File

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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
plotsine50()

View File

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

View File

@ -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));

View File

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

View File

@ -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');

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,7 @@
n = 2000;
hold on
for k = 1:10
[t, x] = randomwalk(n);
plot(t, x)
end
hold off

View File

@ -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

View File

@ -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

View File

@ -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');

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -2,7 +2,7 @@
\input{header}
\setcounter{maxexercise}{0} % show listings up to exercise maxexercise
\setcounter{maxexercise}{1000} % show listings up to exercise maxexercise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%