Control flow done
This commit is contained in:
parent
e81498d095
commit
54a390edc3
@ -824,7 +824,189 @@
|
|||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
\end{frame}
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[plain]
|
||||||
|
\huge{4. Kontrollstrukturen}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{Schleifen}
|
||||||
|
Schleifen werden gebrauch um wiederholte Ausfuehrung desselben Codes zu vereinfachen.
|
||||||
|
\tiny
|
||||||
|
\begin{lstlisting}
|
||||||
|
>> x = 1; % Startwert fuer x
|
||||||
|
... % etwas sinnvolles mit x
|
||||||
|
>> x = x + 1;
|
||||||
|
...
|
||||||
|
>> x = x + 1;
|
||||||
|
...
|
||||||
|
>> x = x + 1;
|
||||||
|
...
|
||||||
|
>> x = x + 1;
|
||||||
|
...
|
||||||
|
>> x = x + 1;
|
||||||
|
>>
|
||||||
|
>> x
|
||||||
|
x =
|
||||||
|
6
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{for - Schleife}
|
||||||
|
\tiny
|
||||||
|
\begin{lstlisting}[label=loopListing2]
|
||||||
|
>> for x = 1:5
|
||||||
|
... % etwas sinnvolles mit x ...
|
||||||
|
end
|
||||||
|
>> disp(x);
|
||||||
|
5
|
||||||
|
>>
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{while - Schleife}
|
||||||
|
\tiny
|
||||||
|
\begin{lstlisting}
|
||||||
|
>> x = 1;
|
||||||
|
>> while x <= 5
|
||||||
|
... % etwas sinnvolles mit x ...
|
||||||
|
disp(x);
|
||||||
|
x = x + 1;
|
||||||
|
end
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
>> disp(x)
|
||||||
|
6
|
||||||
|
>>
|
||||||
|
>> while true %never ending loop!
|
||||||
|
disp(x);
|
||||||
|
x = x + 1;
|
||||||
|
end
|
||||||
|
1
|
||||||
|
2
|
||||||
|
...
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{if - Anweisung (bedingte Anweisung)}
|
||||||
|
|
||||||
|
Wird eingesetzt um bestimmte Code Abschnitte
|
||||||
|
nur unter bestimmten Umst\"anden auszuf\"uhren.
|
||||||
|
|
||||||
|
\tiny
|
||||||
|
\begin{lstlisting}[label=loopListing4]
|
||||||
|
>> x = rand(1); % eine einzelne Zufallszahl
|
||||||
|
>> if x < 0.5
|
||||||
|
disp('x is less than 0.5');
|
||||||
|
end
|
||||||
|
>>
|
||||||
|
>> if x < 0.5
|
||||||
|
disp('x is less than 0.5!');
|
||||||
|
else
|
||||||
|
disp('x is greater than or equal to 0.5!')
|
||||||
|
end
|
||||||
|
>>
|
||||||
|
>> if x < 0.5
|
||||||
|
disp('x is less than 0.5!');
|
||||||
|
elseif x < 0.75
|
||||||
|
disp('x is greater than 0.5 but less than 0.75!');
|
||||||
|
else
|
||||||
|
disp('x is greater than or equal to 0.75!')
|
||||||
|
end
|
||||||
|
>>
|
||||||
|
>> if x < 0.75
|
||||||
|
disp('x is less than 0.75!');
|
||||||
|
elseif x < 0.5
|
||||||
|
disp('x is less than 0.5!');
|
||||||
|
else
|
||||||
|
disp('x is greater than or equal to 0.75!')
|
||||||
|
end
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{break unnd continue}
|
||||||
|
\vspace{-0.5cm}
|
||||||
|
\tiny
|
||||||
|
\begin{lstlisting}
|
||||||
|
>> for x = 1:5 % using continue
|
||||||
|
if(x > 2 & x < 5)
|
||||||
|
continue;
|
||||||
|
end
|
||||||
|
disp(x);
|
||||||
|
end
|
||||||
|
1
|
||||||
|
2
|
||||||
|
5
|
||||||
|
>>
|
||||||
|
>> for x = 1:5000 % using break
|
||||||
|
if(x > 5)
|
||||||
|
break;
|
||||||
|
end
|
||||||
|
disp(x);
|
||||||
|
end
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
>>
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{\"Ubungen}
|
||||||
|
\begin{enumerate}
|
||||||
|
\item Erzeuge einen Vektor 'x' mit z.B. 50 Zufallszahlen im Bereich 0 - 10.
|
||||||
|
\begin{enumerate}
|
||||||
|
\item Benutze eine Schleife um das arithmetische Mittel zu berechnen. Der Mittelwert ist definiert als:
|
||||||
|
$\overline{x}=\frac{1}{n}\sum\limits_{i=0}^{n}x_i $.
|
||||||
|
\item Benutze eine Schleife um die Standardabweichung zu bestimmen:
|
||||||
|
$\sigma=\sqrt{\frac{1}{n}\sum\limits_{i=0}^{n}(x_i-\overline{x})^2}$).
|
||||||
|
\item Suche in der MATLAB Hilfe nach Funktionen, die das fuer dich tuen :-).
|
||||||
|
\end{enumerate}
|
||||||
|
\item Erzeuge eine 5 x 5 x 5 Matrix mit Zufallszahlen.
|
||||||
|
\begin{enumerate}
|
||||||
|
\item Benutze eine for Schleife um nacheinander die Elemente jedes ``Blattes'' einzeln auszugeben.
|
||||||
|
\item Das gleich mit einer while-Schleife.
|
||||||
|
\end{enumerate}
|
||||||
|
\end{enumerate}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
\begin{frame}[fragile]
|
||||||
|
\frametitle{Kontrollstrukturen}
|
||||||
|
\framesubtitle{\"Ubungen}
|
||||||
|
\vspace{-0.5cm}
|
||||||
|
\begin{enumerate}\setcounter{enumi}{2}
|
||||||
|
\item Erstelle 'x' einen Vektor mit 10 Zufallszahlen im Bereich 0:10.
|
||||||
|
\begin{enumerate}
|
||||||
|
\item Benutze eine for-Schleife um all die Elemente zu loeschen,
|
||||||
|
die (\verb+x(index) = [];+) kleiner als 5 sind.
|
||||||
|
\item Loesche alle Elemente die kleiner als 5 und groesser als 2 sind.
|
||||||
|
\item Kann man das gleiche auch ohne eine Schleife erledigen?
|
||||||
|
\end{enumerate}
|
||||||
|
\item Teste den Zufallsgenerator! Dazu zaehle die Anzahl der
|
||||||
|
Elemente, die durch folgende Grenzen getrennt werden [0.2 0.4 0.6
|
||||||
|
0.8 1.0]. Speichere die Ergebnisse in einem passenden
|
||||||
|
Vektor. Nutze eine Schleife um 1000 Zufallszahlen mit
|
||||||
|
\verb+rand()+ (siehe Hilfe) zu ziehen. Was waere die Erwartung,
|
||||||
|
was kommt heraus?
|
||||||
|
\end{enumerate}
|
||||||
|
\end{frame}
|
||||||
|
|
||||||
|
|
||||||
\end{document}
|
\end{document}
|
||||||
|
Reference in New Issue
Block a user