Question? Leave a message!




Short introduction to MATLAB

Short introduction to MATLAB 28
LylaKnight Profile Pic
LylaKnight,France,Professional
Published Date:12-07-2017
Website URL
Comment
Short introduction to MATLAB Course: ASTM22 – Computational Astrophysics Teachers: Alexey Bobrick – alexeyastro.lu.se David Hobbs – davidastro.lu.se MATLAB introduction lecture notes by: Daniel Carrera (doktorand) Alexey Bobrick Short MATLAB introduction slide 1 of 26 Introduction to MATLAB ● Why MATLAB? ● Lightning tour of MATLAB – Variables and arrays – Conditionals – Loops – Functions – Importing data – Plots – Videos ● Machine precision – How computers store numbers – Arithmetic error – Implications for your programs Alexey Bobrick Short MATLAB introduction slide 2 of 26 Why MATLAB? Array-based programming language 1. The basic object is the array (e.g. vector, matrix). 2. Perform operations on entire arrays (e.g. vector addition, matrix product, QR factorization, etc). 3. Compact, clear code. for (i = 0; i n; i++) for (j = 0; j m; j++) C = AB Ci,j = 0; for (k = 0; k q; k++) Ci,j += Ai,kBk,j; Alexey Bobrick Short MATLAB introduction slide 3 of 26 Why MATLAB? Array-based programming language 1. The basic object is the array (e.g. vector, matrix). 2. Perform operations on entire arrays (e.g. vector addition, matrix product, QR factorization, etc). 3. Compact, clear code. 4. Good balance of performance and ease of use. 5. Array languages are a key tool in scientific computing. Alexey Bobrick Short MATLAB introduction slide 4 of 26 How to get MATLAB MATLAB download for Lund University students: http://program.ddg.lth.se/ Popular alternatives to MATLAB - Octave http://www.gnu.org/software/octave/ Extremely similar language to MATLAB. Get the latest version to get a GUI. - Python + SciPy http://www.enthought.com/products/epd.php General purpose programming language with a module for scientific computing. Alexey Bobrick Short MATLAB introduction slide 5 of 26 Lightning tour of MATLAB – Fancy calculator 2 + 3 + 7 + 12 + 31 55 pi 3.1416 sqrt(2) help sqrt 1.4142 cos(2pi) % Cosine uses radians. 1 (2+3i)2 % Complex numbers. -5 + 12i exp(123) 2.6195e+53 log(exp(123)) 123 Alexey Bobrick Short MATLAB introduction slide 6 of 26 Lightning tour of MATLAB – Variables and arrays a = 7 7 b = 2a 14 u = 6 5 4 3 2 6 5 4 3 2 u + 1 1 1 1 1 7 6 5 4 3 u 2 12 10 8 6 4 sqrt(u) 2.4495 2.2361 2.0000 1.7321 1.4142 Alexey Bobrick Short MATLAB introduction slide 7 of 26 Lightning tour of MATLAB – Variables and arrays u u = 6 5 4 3 2 max(u) ans = 6 min(u) ans = 2 sum(u) ans = 20 mean(u) ans = 4 length(u) ans = 5 Alexey Bobrick Short MATLAB introduction slide 8 of 26 Lightning tour of MATLAB – Variables and arrays A = 1 2 3 ; 4 5 6 % 2x3 Matrix. A = 1 2 3 4 5 6 size(A) % Matrix dimensions. ans = 2 3 u u = 6 5 4 3 2 size(u) % MATLAB sees 'u' as a 1x5 matrix. ans = 1 5 Alexey Bobrick Short MATLAB introduction slide 9 of 26 Lightning tour of MATLAB – Variables and arrays u' % Matrix transpose. 6 5 4 3 2 u' u % Matrix product. 36 30 24 18 12 30 25 20 15 10 24 20 16 12 8 18 15 12 9 6 12 10 8 6 4 u . u 36 25 16 9 4 u' . u error Alexey Bobrick Short MATLAB introduction slide 10 of 26 Lightning tour of MATLAB – Variables and arrays A = rand(3) % Random matrix 0.573291 0.663319 0.265920 0.531253 0.481396 0.361992 0.203692 0.745661 0.053230 b = 1;2;3 % Column vector 1 2 3 x = A\b % Solve the linear system Ax = b -13.6425 6.5480 16.8387 Alexey Bobrick Short MATLAB introduction slide 11 of 26 Lightning tour of MATLAB – Variables and arrays a = 4 5 6 b = 7 8 6 if a == b disp(“Drive a car”) if ( condition ) ... elseif any(a = b) elseif ( condition ) ... disp(“Take the bus”) else else disp(“Take the train”) end Alexey Bobrick Short MATLAB introduction slide 12 of 26 Lightning tour of MATLAB – Variables and arrays r = ; for i = 1:30 for i = start:end r(i) = ii ... end end y = 100; while (y 10) while ( condition ) y = sqrt(y)3 ... end end Alexey Bobrick Short MATLAB introduction slide 13 of 26 Lightning tour of MATLAB – Loops vs arrays Rule 1: Aim to replace loops with array operations Speed Loops are run by the MATLAB interpreter. Array ops are done by the backend, written in C. Clarity More often than not, your code will be more concise and clear. Education MATLAB was designed around arrays. Learn another way to approach programming. Alexey Bobrick Short MATLAB introduction slide 14 of 26 Lightning tour of MATLAB – Functions Each function is stored in a separate file, the file name must match the function name. File name: function_name.m File contents: function output vals = function_name(input vals) ... instructions ... Alexey Bobrick Short MATLAB introduction slide 15 of 26 Lightning tour of MATLAB – Functions Example: File name: lnorms.m File contents: % % Return the L1, L2 and L-infinity norms of a vector % function l1 l2 linf = lnorms(vals) l1 = sum( abs(vals) ) l2 = sqrt( sum(vals . vals) ) linf = max( abs(vals) ) Alexey Bobrick Short MATLAB introduction slide 16 of 26 Lightning tour of MATLAB – Importing data Suppose you have a data file called: data_file.dat If it stores arranged numeric data, it can be imported by: M = dlmread(data_file.dat) … and the data will be saved into matrix M dlmread automatically infers the format of the data in the file Alexey Bobrick Short MATLAB introduction slide 17 of 26 Lightning tour of MATLAB – Plots Example: Making a plot for arrays x,y: plot(x,y) %plot(x,y,'o') for different markers Setting the options (after 'plot' command): xh=xlabel('x (m)'); yh=ylabel('internal energy (J/Kg)'); th=title('Internal energy vs distance'); %labels and title Axis(-0.4 0.4 1.8 2.6) %plotrange: xmin,xmax,ymin,ymax set(gca,... 'linewidth',2,... %thickness of the line 'xcolor',0,0,0,... %plot color 'fontsize',18,... 'fontname','arial'); %main plot style set(xh,yh,th,... 'fontweight','bold',... 'fontsize',18,... 'color',0,0,0); %plot label and title style Alexey Bobrick Short MATLAB introduction slide 18 of 26 Lightning tour of MATLAB – Plots Example: To make M-row N-column multi-panel plot: subplot(M,N,plot number) … commands for making a plot of a given number subplot(M,N,next plot number) … commands for the next plot pause(0.1) %Add if the plots are made in a loop Alexey Bobrick Short MATLAB introduction slide 19 of 26 Lightning tour of MATLAB – Videos Example: To make a video consisting of several plots: vidObj = VideoWriter('peaks.avi'); open(vidObj); Z = peaks; surf(Z); axis tight set(gca,'nextplot','replacechildren'); for k = 1:20 surf(sin(2pik/20)Z,Z) currFrame = getframe; writeVideo(vidObj,currFrame); end close(vidObj); Alexey Bobrick Short MATLAB introduction slide 20 of 26