% Introduction to MATLAB for "Numerical modelling of weather and climate" % Springterm 2010 % delete all variables clear clear all % delete single variables clear x % Definition of variables a=1 b=2 c=1.3456 d=1.567e-2 % supress output of variables using ; e=23.1045; %print used variables and its dimensions who % variables whos % variablen, dimensions and typ % operations: f=a+b g=A+b % MATLAB destinguishes between capital and small letters % power f=e^2 g=e^1.4 % square root f=sqrt(e) f=e^0.45 % also for complex numbers f=a-c h=sqrt(f) z=e+h % Real- and Imaginary part real(z) imag(z) % textvariables %============== text='This is text' text=num2str(300) % transformation from numbers to text whos text % Vektors and Vectoroperations %=============================== a=[1 2 3] % rowvector, 1x3 size(a) b=[1;2;3] % columnvector, 3x1 size(b) c=[3 2 1 0] % rowvector, 1x4 % Indices of vectors (and matrices) have to be integer and % always lager than 0 % alternative Definition is a=[] a(1)=1 a(2)=2 a(3)=3 % Transpose %-------------- at1=a' % rowvector to columnvector size(at1) % for vectoroperations, pay attention to vector dimensions d=[3 2 1] e=a+d % 1x3 vector + 1x3 vector = 1x3 vector f=d*at1 % 1x3 vector * 3x1 vector = 1x1 vector = scalar % elementwise operations: ff = a.*d % ff(i) = a(i)*d(i) % Matrices and matrixoperations %=============================== A=[1 2 3; 4 5 6] % 2x3 matrix B=[1 2; 3 4; 5 6] % 3x2 matrix C=[-1 -2 -3; 1 2 3] % 2x3 matrix size(A) size(B) G=at1*d % 3x1 vector * 1x3 vector = 3x3 matrix % Transpose %-------------- AT1=A' GT1=G' % for matrixoperations, pay attention to matrix dimensions E=A+C F=A+B F2=G^2 F3=G^3 size(F2) H=[1 1 0; 0 1 1 ; 0 0 1] H2=H^2 H3=H^3 K=G*H % elementwise Operations: He=H2.^2 % Loops, if-statements, plotting etc. % Loops %========== for k=start:increment:end blabla end for k=0:2:10 k end % vectorfunction x=[1 3 6 8 10 8 6 3 1] % 'if' statement %============== if (condition) do this else do that end oder if (condition) do this end x=[]; for k=1:10 if (mod(k,2)==0) x(k)=0; else x(k)=1; end end x size(x) x=[]; for k=5:-1:-5 if (k<=0) break %break ends the for-loop end x(k)=k; end x size(x) % plotting of functions % Attention, the vector could already contain some elements x=[0:12] for k=1:10 x(k)=2*k; end plot(x) % strange plot x size(x) %better like this: x=[] %empty vector for k=1:10; x(k)=2*k; end; plot(x) %or like this: clear x for k=1:10; x(k)=2*k; end; plot(x) %create vector x with values 0,pi/100,2*pi/100,...,2*pi x=[0:pi/100:2*pi] y=sin(x) figure(1) % open plot window Nr 1 plot(y) % pay attention to x-axis plot(sin(x)) figure(2) % open plot window Nr 2 plot(x,y) % pay attention to x-axis % modifications to current axis set(gca,'XLim',[0 2*pi]) set(gca,'YLim',[-1 1]) clf(1) % delet plot Nr 1 close(1) % close Plot window 1 close(2) % close Plot window 2 % open several frames to create a liitle clip clear all close all %number of frames: nframes = 20; figure(1) % open plot window Nr 1 for i = 1:nframes x=0:0.1:i; y1=x.*2.; plot(x,y1); title('simple') set(gca,'XLim',[0 20]) % modifies "current axis" set(gca,'YLim',[0 40]) % modifies "current axis" M(i)=getframe; end movie(M) %number of frames: nframes = 20; figure(2) % open plot window Nr 2 for i = 1:nframes x=0:0.01:i; y1=x.*2.; y2=x.*x; plot(x,y1); title(['a bit more complex']) grid set(gca,'XLim',[0 20]) set(gca,'YLim',[0 400]) hold on plot(x,y2,'r') hold off M(i)=getframe; end %and just for the fun of it, 5 more times ... movie(M,5) close all clear all % and now it's your turn ...