Computerübungen zur Beschleunigerphysik (Teil 1) schmidt ... · PASCAL / DELPHI, C...

Post on 18-Mar-2020

9 views 0 download

Transcript of Computerübungen zur Beschleunigerphysik (Teil 1) schmidt ... · PASCAL / DELPHI, C...

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

1

Einleitung

Computerübungen zur Beschleunigerphysik (Teil 1)

schmidt@delta.uni-dortmund.deschirmer@delta.uni-dortmund.de

shaukat.khan@tu-dortmund.de

MatLab-Rechner: CIP-POOL (Physik) Raum: P2-03-507Account muss für jeden Studenten eingerichtet werdenCIP-Pool Admins fragen!

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

2

Einleitung

Wissenschaftliches Rechnen (Hilfsmittel)Abacus:1100 v.Chr.

MBO Junior 1973

Curta Typ II1. Konzept 1935

Mathematische Tafeln

Rechenstab1. Version 1956

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

3

Einleitung

Wissenschaftliches Rechnen (Rechenzentrum)

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

4

Einleitung

Wissenschaftliches Rechnen (Taschenrechner)

Sharp PC1500

Casio FX100

TI571977-1982

HP 28CHP 200LX

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

5

Einleitung

Wissenschaftliches Rechnen (Personal Computer)

Commodore PET 1977-1982

Apple II 1977

Atari ST ab 1985

IBM PC ab 1981

Apple Macintosh ab 1984

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

6

Einleitung

LabVIEW:Beispiel mit RS232 und GPIB-Buchse

LEGO-Mindstorms:Microcontroller und RIS-Beispiel

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

7

Einleitung

Programmiersprachen

Maschinencode / Assembler s1: mov a1, 61hHochsprachen Prozedurale Sprachen

ALGOL, COBOLFORTRAN, BASICPASCAL / DELPHI, CListenorientierte SprachenLISP / PROLOGObjektorientierte SprachenC++, JAVA

technisch/wissenschaftliche MATHCADProgrammiersprachen MATLAB

SCILAB / OCTAVE

Computer Algebra Systeme MAPLE, MuPADMATHEMATICA

'Grafische' Programmiersprachen LABVIEW, HP-VEE, LEGO RIS

Anwendungsspezifische PERL, TCL/TK, PHYTHONInterpretersprachen

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

8

Einleitung

Was bietet MATLAB (MATrix LABoratory)?

➔ Integrierte Entwicklungsumgebung (Workbench) mit Texteditor, Debugger, Grafikeditor, Datei-Browser, Compiler, uvm

➔ (Programmierbarer) Taschenrechner (Interpreter)

➔ Programmierspracheerweiterbar durch eigene MatLab Scripte/Funktionenoder durch umfangreiche Toolboxen (z.B. Signalverarbeitung, Reglungstechnik, Optimierung, Statistik, usw.)

➔ Leistungsfähige 2/3D-Grafikfunktionen

➔ Objektorientierte Programmierung von GUIs (Grafical User Interfaces)für die Bedienung komplexer Software

➔ SIMULINK ist eine MATLAB-Toolbox zur Simulation regelungstechnischer Problemstellungen

➔ Steuerung/Überwachnung von Geräten (Netzgeräte, Oszilloskope, DELTA)Messwerterfassung und Auswertung

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

9

Einleitung

% Define initial conditions.t0 = 0;tfinal = 15;y0 = [20 20]';% Simulate the differential equation.tfinal = tfinal*(1+eps);

[t,y] = ode23('lotka',[t0 tfinal],y0);

function yp = lotka(t,y)yp=diag([1-.01*y(2),-1+.02*y(1)]*y;%LOTKA DGL: Lotka-Volterra predator-prey model.

Beispiel:Vergleich zwischen MatLab und 'C'Numerische Integration einer DGLhier: Runge-Kutta-Verfahren

Aus Numerical Recipes in C

subplot(1,2,1)plot(t,y)title('Time history')

subplot(1,2,2)plot(y(:,1),y(:,2))title('Phase plane plot')

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

10

Einleitung

MATLAB (Version 2008a)

The MathWorks (Natick/MA/USA)Studentenversion: ab 45€ (Version 2007)

SCILAB (Version 5.0.2)

Digiteo research network kostenloses Download

GNU OCTAVE (Version 3.03)

kostenloses Download (steht ebenfalls im CIP-Pool zur Verfügung)

http://www.mathworks.de/

http://www.scilab.org/

http://www.gnu.org/software/octave

http://www.mathworks.de/matlabcentral/fileexchange/loadCategory.do

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

11

Einleitung

MATLAB als Taschenrechner:Rechenoperationen (+,-,/,*,^)

>> 3-2 3-2; Semikolon unterdrückt die Ausgabeans = 1

Funktionen (z.B. sqrt, sin, abs, log, usw.)

>> sqrt(25)ans = 5

Konstanten

>> pians = 3.1416

Allgemeines:Hilfe

>> help HELP topics ...>> help sqrt SQRT Square root SQRT(X) is the square root of ...

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

12

Einleitung

Löschen von Variablen

>> help clearCLEAR Clear variables and functions from memory.

CLEAR removes all variables from the workspace. CLEAR VARIABLES does the same thing. CLEAR GLOBAL removes all global variables. CLEAR FUNCTIONS removes all compiled M- and MEX-functions.

CLEAR ALL

>> clear v1

>> clear *

>> clear all

>> clc löscht all Einträge im Command Window

>> format compact Leerzeilen bei Ausgabe vermeiden>> format short 1.6666>> format long 1.6666666666666666

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

13

Einleitung

Wertzuweisungen

>> a=1a = 1

>> a=a+1a = 2

Name = Datentyp (Wert)

>> x=int32(9.6)x = 10

>> y=3.5+2.1iy = 3.5000 + 2.0000i

>> wort='quatsch'wort = quatsch

Datentypen:int32 4 Bytedouble 8 Bytechar 2 Byte ...

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

14

Einleitung

Vektoren und Matrizen

>> v1=[1 2 3]v1 = 1 2 3

>> v2=[1; 2; 3]v2 = 1 2 3

>> v2' Spaltenvektor in Zeilenvektor umwandelnans = 1 2 3

>> len = length(v1) Länge eines Vektorslen=3

>> mat=[1, 2; 3, 4]mat= 1 2 3 4

>> Z = mat(i,:) Zeile i auswählen>> S = mat(:,s) Spalte s auswählen

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

15

Einleitung

Liste der benutzten Variablen

>> whoa mat v1 v2 wort x y

>> whosName Size Bytes Class

a 1x1 8 double arraymat 2x2 32 double arrayv1 1x3 24 double arrayv2 3x1 24 double arraywort 1x7 14 char arrayx 1x1 4 int32 array y 1x1 16 double array (complex)

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

16

Einleitung

Verzeichnisse und m-Dateien

>> pwdans = C:\My Documents

>> dirMy MusicMy PicturesMy Videosmatfiles

>> lsMy MusicMy PicturesMy Videosmatfiles

>> cd 'My Music'>> cd ..>> cd matfiles>> helloworldtext =Hello World

Pfad im Menu unter File --> Set Path hinzufügenC:\My documents\matfiles\helloworld.m

text = 'Hello World'

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

17

Einleitung

Rechenoperationen

>> e1=a+ae1 = 4

>> e2=a*e1e2 = 8

>> e3=e1–e2e3 = –4

>> e4=e2/e1e4 = 2

>> e5=e2^2e5 = 64

>> e6=v1*v2 Inneres Produkte6 = 14

>> e7=v1'*v2'e7 = 1 2 3 2 4 6 3 6 9

v1=Zeilenvektor; v2=Spaltenvektordaher:

>> e8=v1*v1 oder v2*v2??? Error using ==> mtimesInner matrix dimensions must agree.

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

18

Einleitung

Ausgabeformate

>> cd matfiles>> helloworldHello World

fprintf wie printf bei der Programmiersprache 'C'

>> fprintf('x = %d \n', -5) %d dezimal mit Vorzeichen x = -5>> fprintf('x = %f \n', 5) %f floatx = 5.000000>> fprintf('x = %e \n', 5) %e Exponentialdarstellungx = 5.000000e+000>> fprintf('x = %4.2f \n', 5) %f float mit 2 Nachkommastellenx = 5.00>> fprintf('x = %g \n', 5)x = 5>> fprintf('x = %g \n', 0.0000005) %e nur wenn Exp. <-4x = 5e-007

>> text = 'aua'>> fprintf('%s \n', text) %s char Zeichenketteaua

C:\My documents\matfiles\helloworld.m

text = 'Hello World';disp(text)oderfprintf('Hello World \n');

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

19

Einleitung

Eingabe

>> cd matfiles>> wurzelBitte Wert eingeben: 5Danke, die Wurzel ist 2.236068

Datei schreiben

>> fid = fopen('Wichtige_Daten.dat', 'w');>> fprintf(fid, '%f \n',pi);>> fclose(fid);

>> save –ascii Unwichtige_Daten.dat v1

Datei lesen

>> fid = fopen('Wichtige_Daten.dat', 'r');>> wert = fscanf(fid,'%f');>> fclose(fid)

>> load –ascii Unwichtige_Daten.dat LOAD Load workspace variables from disk.

>> Unwichtige_DatenUnwichtige_Daten = 1 2 3

C:\My documents\matfiles\wurzel.m

wert=input('Bitte Wert eingeben: ');antwort=sqrt(wert);fprintf('Danke, die Wurzel ist %f \n',antwort);

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

20

Einleitung

Schleifen

>> for k=1:5x(k)=k^2;end>> xx = 1 4 9 16 25

>> for k=1:2:5k^2endans = 1ans = 9ans = 25

>> z=10;>> while (z > 0)z=z–1endz = 9 ...z = 0

Vergleichsoperatoren<<=>=>test Real- und Imaginärteil==~=

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

21

Einleitung

Bedingungen

>> k = 5;>> if (k >= 0) fprintf('Heureka! \n')endHeureka!

>> k = 5;>> if (k=0) || (k>0)fprintf('Heureka! \n')endHeureka!

>> if (k = 0)...elseif (k=1)...else if(k>0) ... endend

Logische Operatoren

&& und || oder ~ nicht

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

22

Einleitung

Matrizen erzeugen

>> mat = [1, 2; 3, 4]mat= 1 2 3 4 Es geht auch anders

>> a(2,4) = 5a = 0 0 0 0 0 0 0 5>> b = zeros(2,4)b = 0 0 0 0

0 0 0 0>> b = ones(2,4)b = 1 1 1 1

1 1 1 1>> c = eye(2,4)c = 1 0 0 0

0 1 0 0>> d = rand(2,4)d = 0.93 0.79 0.46 0.63

0.15 0.49 0.36 0.23

Zufallszahlen zwischen 0 und 1

randrand(n,m)

normal ("Gauß-") verteilte Zufallszahlen

randn(n,m)

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

23

Einleitung

Der Doppelpunkt

>> for k = 1:6x(k)=2*k–1;end>> xx = 1 3 5 7 9 11

>> x = 1:2:11x = 1 3 5 7 9 11

>> mat = eye(4)mat = 1 0 0 0 0 1 0 0

0 0 1 0 0 0 0 1

>> y = mat(3,:)y = 0 0 1 0

>> plot( mat(3,:))

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

24

Einleitung

Grafik

plot

>> x = 1:0.1:50

>> y = sin(x)

>> plot(y)

>> plot(x,y)

>> axis( [3.0 12.0 -1.5 1.5])>> xlabel('time')>> ylabel('amplitude \alpha')>> title('\alpha = sin \omega t')>> text(7,1.1,'peak')>> text(5,-1.2,'valley')

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

25

Einleitung

Grafik

plot

>> x = 1:0.1:50>> y = sin(x)

>> plot(x)>> plot(y)>> plot(x,y)

>> axis( [3.0 12.0 -1.5 1.5])>> grid on

>> xlabel('time')>> ylabel('amplitude \alpha')>> title('\alpha = sin \omega t')>> text(7,1.1,'peak')>> text(5,-1.2,'valley')

>> y2 = sin(x-1)>> hold

>> plot(x,y2,'r-o')

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

26

Einleitung

Grafik

Farben

r Redy Yellowg Greenb Bluec Cyanm Magentak blacKw White

Linien

- durchgezogen_ gestrichelt: Punkte-. Strich-Punkte

Symbole

+ o * x s d ^ > < p h

allgemein >> plot( x, y, 'farbe linie symbol')

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

27

Einleitung

3d-Grafik

>> plot(v)

>> mesh(v)

>> waterfall(v)

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

28

Einleitung

3d-Grafik

>> surf(v) 3-D colored surface

>> view( [0 90] )

>> shading interp

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

29

Einleitung

3d-Grafik

>> x = 0:0.1:20>> plot3(cos(x),sin(x),x,'r–s')

Beschleunigerphysik I, TU Dortmund, Wintersemester 2008/09 Computerübungen

30

Einleitung

Weitere Spielereien

Ein Ton (ca. 500 Hz)

>> y = ones(10000,1);>> for k = 1:5000y(2*k) = -1;end>> sound(y,1000)

Angenehmere Töne

>> load handel>> sound(y,Fs)

Mehrere Bildermit subplot

>> subplot(2,1,1)>> plot(y)>> subplot(2,1,2)>> plot(y(1:1000))

Ha- le- luja ha- le- luja haleluja haleluja ha- le- lu- jaaa