diff --git a/mr/Ub2/mr2.pdf b/mr/Ub2/mr2.pdf new file mode 100644 index 0000000..f236466 --- /dev/null +++ b/mr/Ub2/mr2.pdf Binary files differ diff --git a/mr/Ub2/mr2.tex b/mr/Ub2/mr2.tex index 55da6cb..a8837f9 100644 --- a/mr/Ub2/mr2.tex +++ b/mr/Ub2/mr2.tex @@ -83,15 +83,30 @@ \Aufgabe{}{6} \begin{enumerate}[(a)] - \item Forward kinematic velocity: calculation of the robots' current pose with knowledge about the velocity and position of the last pose \\ - inverse kinematic velocity: given a pose the velocity and position needed to reach this pose are calulated - \item $^Rv=\begin{pmatrix} - 0.2\frac{m}{s} \\ 0.5\frac{m}{s} - \end{pmatrix}$ \\$l=0.2m \\ - u_t= \begin{pmatrix} - ?\frac{m}{s} \\ ?\frac{m}{s} - \end{pmatrix}$ - + \item Forward kinematic velocity: calculation of the robots' current pose with knowledge about the velocity and position of the last pose \\ + inverse kinematic velocity: given a pose the velocity and position needed to reach this pose are calulated + \item $^R\mathbf{v}=\begin{pmatrix} + 0.2\frac{m}{s} \\ 1 \frac{1}{s} + \end{pmatrix}$, $l=0.2m$\\ + \begin{align} + v_l &= \frac{2\cdot 0.2-0.2}{2}&=0.1\\ + v_r &= \frac{2\cdot 0.2+0.2}{2}&=0.3 + \end{align} + $\Rightarrow \mathbf{u}_t=\left(0.1\frac{m}{s}, 0.3\frac{m}{s}\right)^T$ + \item \begin{align} + v &= \frac{0.3+0.5}{2}\frac{m}{s} &=0.4\frac{m}{s}\\ + \omega &= \frac{0.5-0.3}{0.2}\frac{1}{s}&=1\frac{1}{s} + \end{align} + $\Rightarrow \mathbf{v}_t=\left(0.4\frac{m}{s},1\frac{1}{s}\right)$ + \item \begin{align} + \frac{1}{2}v_r+\frac{1}{2}v_l &=v\\ + \frac{1}{l}v_r -\frac{1}{l}v_l &=\omega + \end{align}$\Leftrightarrow$\begin{align} + v_r+v_l &= 2v\\ + v_r-v_l &=l\omega\\ + \end{align} + $\Rightarrow 2v_r =2v+l\omega\Leftrightarrow v_r=\frac{2v+l\omega}{2}$\\ + $\Rightarrow \frac{2v+l\omega}{2}+v_l =2v \Leftrightarrow v_l=2v-\frac{2v+l\omega}{2}=\frac{2v-l\omega}{2}$\qed \end{enumerate} \Aufgabe{}{8} \Aufgabe{}{6} diff --git a/mr/tutorial.m b/mr/tutorial.m new file mode 100644 index 0000000..bd1ca71 --- /dev/null +++ b/mr/tutorial.m @@ -0,0 +1,269 @@ +% MATLAB Tutorial +% =============== +% Kurze Einführung in Funktionen relevant zu unserer Vorlesung +% Erstellt von Sebastian Scherer sebastian.scherer@uni-tuebingen.de +% CC BY-SA 3.0 ( http://creativecommons.org/licenses/by-sa/3.0/ ) + +% Allgemeines: +% ============ +% MATLAB ist eine +% 1) einfache Skriptsprache zur schnellen Manipulation von Matrizen, +% 2) vollständige IDE, +% 3) Sammlung etlicher wichtige mathematischer Bibliotheken + Toolboxen. +% +% Kommerzielles (teures!) Produkt, für Studenten und Mitarbeiter kostenlos. +% +% Download im CampusSoftware-Portal des ZDV: +% https://services.zdv.uni-tuebingen.de/CampusSoftware/ +% +% Interessante freie Alternativen: +% +% - GNU Octave: +% * Weitgehend kompatibel zu MATLAB ohne Toolboxen +% * Keine symbolischen Berechnungen +% +% - (wx)Maxima: +% * Freies Computer Algebra System +% +% - Python: +% * NumPy/SciPy für numerische Berechnungen +% http://wiki.scipy.org/NumPy_for_Matlab_Users +% * matplotlib für plots +% http://matplotlib.org/ +% * SymPy für symbolische Berechnungen +% http://www.sympy.org/en/index.html + +close all; % evtl. geöffnete plot-Fenster schließen +clear all; % Daten im Workspace löschen + +% Einfaches Rechnen: +% ================== +% Taschenrecher-Ersatz: +% Setzt man am Ende einer Zeile kein Semikolon, wird das Ergebnis +% direkt ausgegeben: + +5+3 %#ok<*NOPTS> % Warne nicht vor fehlenden Semikolons + +3^2 + +sqrt(9) + +cos(pi) + +1/inf + +% Variablen und Funktionen: +% ========================= +% Variablen werden bei der ersten Zuweisung deklariert: + +a = 5 + +b = 2 + a + +% Das Ergebnis der letzten Operation kann aus der Variablen 'ans' +% ausgelesen werden (aber bitte nicht in Skripten verwenden): + +ans + +% Funktionen können "anonym" deklariert und anschließend verwendet werden: + +parabel = @(x) 5*x.^2 + 2*x + 1 +parabel(5) + +% Für komplexere Funktionen empfiehlt es sich allerdings, diese in eine +% extra Datei auszulagern (s. beispielFunktion.m). +% Matlab durchsucht das aktuelle Verzeichnis + seinen Suchpfad +% (konfigurierbar). + +beispielFunktion(5) + +% Lineare Algebra: +% ================ +% Matrizen oder Vektoren werden folgendermaßen eingegeben: Ein Semikolon +% markiert das Ende einer Zeile, zwischen Elementen einer Zeile kann man +% ein Komma setzen (optional). + +M = [1 2; 3 4] +v = [1; 2] + +% Es gibt diverse Abkürzungen zur Erzeugung einfacher Matrizen: + +I4 = eye(4) % Identität +Z = zeros(4,4) +a = ones(4,1) +D = diag([1, 2, 3]) + +% Einer der hilfreichsten Operatoren ist der Doppelpunkt-Operator: + +vi = 1:10 +vi = 1:0.1:10 + +% Passende Dimensionen vorausgesetzt, können Vektoren und Matrizen einfach +% konkateniert werden: + +B = [I4 Z a] +B2 = [I4; Z] + +% Matrizen und Vektoren werden mit "*" multipliziert: + +M*v +M*M +5*M + +% Elementweise Operationen werden durch einen vorangestellten Punkt +% ausgedrückt: +M.*M +M.^5 + +% "'" transponiert Matrizen: + +M' +v' + +% Zugriff auf einzelne Einträge geht über runde Klammern. +% Achtung: MATLAB verwendet 1-basierte Zählweise + +M(1,2) +v(1) +v(end) + +% Teilmatrizen bzw. Zeilen und Spalten werden über ":" selektiert: + +M(:,1) +M(1,1:2) +v(2:end) + +% Übrigens: Die meisten eingebauten Funktionen akzeptieren sowohl Skalare +% als auch Matrizen als Eingabe und werten Matrizen elementweise aus: + +sin(M) + +% Außerdem praktisch: Summieren über eine Dimension: + +sum(M,1) +sum(M,2) + +% Beispiel Rotation in 2D um 180° als homogene Transformationsmatrix: + +rot = @(alpha) [cos(alpha), -sin(alpha), 0; sin(alpha), cos(alpha), 0; 0, 0, 1] +rot(180) +rot(pi) + +% Plotten von Daten: +% ================== +% Gewünschten Wertebereich für x-Achse auswählen: + +x = 0:0.01:2*pi; + +% Zugheörige y-Werte berechnen und plotten: + +plot(x, sin(x)); + +% Mehrere Kurven: + +plot(x, sin(x), x, cos(x)); +legend('sin', 'cos') + +% 3D: + +x = 0:0.01:10*pi; +plot3(sin(x),cos(x),x) + +% Für mehr Details siehe Hilfe! + +% Hilfreiche Funktionen beim Umgang mit Wahrscheinlichkeiten +% ========================================================== + +% Zufallsvariablen erzeugen: +randn(10, 1) % normalverteilt N(0, 1) +rand(10, 1) % gleichverteilt zwischen 0 und 1 + +r2 = mvnrnd([0; 0], eye(2), 20) % n-D normalverteilt mit geg. mu, Sigma + +% Mean und Sample Covariance Matrix: +mu = mean(r2) +S = cov(r2) + +x = -5:0.01:5; + +% Wahrscheinlichkeitsdichte und kumulative Verteilungsfunktion: +plot(x, normpdf(x, 0, 1), x, normcdf(x, 0, 1)); + +% Wahrscheinlichkeitsdichte im mehrdimensionalen Fall: +mvnpdf(r2(1,:), mu, S) + + +% Symbolisches Rechnen +% ==================== +% MATLAB enthält ein komplettes Computeralgebrasystem: Das frühere MuPAD. + +% Symbolische Variablen werden mit dem Befehl syms deklariert. +% Falls sie nicht weiter spezifiziert werden, geht MATLAB von komplexen +% Zahlen aus. Wir rechnen meist mit reellen Zahlen. + +syms x y z real + +x + y + +% Plotten von Symbolischen Funktionen: +% ==================================== + +syms x y z real +parabel(x) +ezplot(parabel, [-1,1]) + +% Etwas komplexer: +l = (x^2 + y^2)^2 - 20*(x^2 - y^2) +ezplot( l == 0 ) + +% Vereinfachen von Termen: +% ======================== + +term = sin(x)*cos(y) + sin(y)*cos(x) +terms = simplify(term) + +% Manchmal gibt es unterschiedliche Möglichkeiten, einen Term zu +% "vereinfachen" + +simple(10*(x^2 + y*(20+z))) + +% Ableiten und integrieren: +% ========================= + +% Ableitung nach x: + +d = diff(parabel(x), x) + +% Unbestimmtes Integral: ("Stammfunktion") + +s = int(parabel(x), x) + +% Schönere Ausgabe: +% ================= + +pretty(rot(x)) +latex(rot(x)) + +% Symbolische Berechnungen mit Matrizen: +% ====================================== + +syms px py pz x real +p = [px; py; pz] +R = rot(x) +R*p +diff(R*p, x) + +% Jacobi-Matrix: +d = sqrt(p'*p) +J = jacobian(d, p) + +% Sonstiges: +% ========== + +solve(parabel(x) == 1, x) + +limit(1/x, x, 0) +limit(1/x, x, 0, 'right') +limit(1/x, x, 0, 'left') + +limit(1/x, x, Inf, 'left') diff --git a/mr/tutorial.py b/mr/tutorial.py new file mode 100755 index 0000000..dc92733 --- /dev/null +++ b/mr/tutorial.py @@ -0,0 +1,125 @@ +#!/usr/bin/python + +# inspired by MATLAB-Tutorial by +# Sebastian Scherer sebastian.scherer@uni-tuebingen.de +# adapted for python by +# Jan-Peter Hohloch jan-peter.hohloch@uni-tuebingen.de +# CC BY-SA 3.0 ( http://creativecommons.org/licenses/by-sa/3.0/ ) + +# this is only for getting an impression, for deeper understanding +# and more extensive use see the python references u can find in the web + +# in UNIX: 'chmod +x tutorial.py', then executable './tutorial.py' +# alternative 'python tutorial.py' +# there are IDEs, e.g. Spyder +# others see: +# https://wiki.python.org/moin/IntegratedDevelopmentEnvironments + +# 'import' to include libraries +# as allows to define aliases +import numpy as np + +# Calculator +# ========== +#(best in a python-shell 'python' and use there, +# in the shell there is also no need for the print command) +print 5+3 +print 3**2 # ** is 3^2 +print np.sqrt(2) #sqrt is from numpy library +print 1/np.sqrt(2) # Vorwahl von TU + +print np.cos(np.pi) #numpy contains constants +print 1/np.inf +print + +# Variables and functions +# ======================== +# variables are used dynamically +a=3 +b=1/np.sqrt(2) +print type(a) #int +print type(b) #float + +# functions can be defined as lambda-abstraction +parabel= lambda (x) : 5*x**2+2*x+1 +print parabel(5) +# alternatively they can be defined like this +def collatz(a): + if np.mod(a,2) == 0: + return a/2 + else: + return 3*a+1 +#IMPORTANT: there are no braces, indentation decides where the function ends! +print collatz(5) +print + +# Linear algebra +# ============== +# there are matrices in numpy +M=np.matrix('1,2;3,4') +# but often arrays are sufficient +N=np.array([[1,2],[3,4]]) + +print M +print N +## transpose +Mt = M.T +Nt = N.T +# be careful: operators might describe different operations on +# matrices and array +## matrix multiplication or element-wise multiplication +print Mt*M # matrix multiplication +print Nt*N # element-wise(!!!) +## multiplication with scalar +print 5*M +print 5*N +## concatenation with vstack and hstack (only one argument, so use tuples) +## dimensions must match +print np.hstack((M,Mt)) +print np.vstack((N,Nt)) +# ... lots of other operations +# (see http://docs.scipy.org/doc/numpy/reference/index.html) +print + +# Built in matrices +I=np.identity(3) +Z=np.zeros((3,3)) +print I +print Z +print np.ones((3,3)) + +# many functions are also applicable to matrices +print np.sin(I) + +# example from robotics +rot = lambda phi : np.matrix([[np.cos(phi),-np.sin(phi),0],[np.sin(phi),np.cos(phi),0],[0,0,1]]) +print rot(np.pi) + +# Access via index +# ================ +print I[0,0] +print I[0:2,1] # 0 to 2, 2 excluded +print I[:,-1] # begin to end, last row(-1) + +a=range(-10,10,2) +print a +print a[2:-4:2] # 3rd elem to 4th last in steps of 2 +print + +#for plotting you can use e.g. matplotlib + +# Distributions and probabilities +# =============================== +# Random variable +X=np.random.rand(1000) # 4 random values, uniformly distributed in [0,1) +Xn=np.random.randn(4) # 4 random values ~N(0,1) +Xu=np.random.uniform(-1,1,2) # 2 rand val, uniformly dist in [-1,1) +Xu=np.random.normal(42,2,3) # 3 rand val, normally dist: ~N(42,2) + +print np.mean(X) + +mu=np.array([-1,1]) +cov=np.array([[1,3],[3,9]]) +r2=np.random.multivariate_normal(mu, cov, 3) +print r2 +