Presentation is loading. Please wait.

Presentation is loading. Please wait.

Eng. 8054 Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab.

Similar presentations


Presentation on theme: "Eng. 8054 Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab."— Presentation transcript:

1 Eng. 8054 Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab

2 Eng. 8054 Advanced Marine Vehicles Introduction to Matlab

3 What is Matlab? Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming environment. Matlab is available for PC's, Macintosh and UNIX systems. Matlab is well adapted to numerical experiments. Matlab program and script files (m-files) always have filenames ending with ".m"; The programming language is exceptionally straightforward since almost every data object is assumed to be an array. Graphical output (figure) is available to supplement numerical results. Online help is available from the Matlab prompt (a double arrow) by typing help.

4 What kind of graphics are possible in Matlab? Polar plot: t=0:.01:2*pi; polar(t,abs(sin(2*t).*cos(2*t))); Line plot: x=0:0.05:5;,y=sin(x.^2);,plot(x,y); Stem plot: x = 0:0.1:4;, y = sin(x.^2).*exp(-x); stem(x,y)

5 What kind of graphics is possible in Matlab? Mesh plot: z=peaks(25);, mesh(z); Surface plot: z=peaks(25);, surf(z);, colormap(jet); Contour plot: z=peaks(25);,contour(z,16); Quiver plot:

6 Using Help in Matlab Online help is available from the Matlab prompt (>> a double arrow), both generally (listing of all available commands): >> help [a long list of help topics follows] and for specific commands: >> help fft [a help message on the fft function follows].

7 What is Matlab?  MATLAB consists of:  The MATLAB language  a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features.  The MATLAB working environment  the set of tools and facilities that you work with as the MATLAB user or programmer, including tools for developing, managing, debugging, and profiling  Handle Graphics  the MATLAB graphics system. It includes high-level commands for two- dimensional and three-dimensional data visualization, image processing, animation, and presentation graphics.  …(cont’d) ‏

8 What is Matlab?  The MATLAB function library.  a vast collection of computational algorithms ranging from elementary functions like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms as well as special image processing related functions  The MATLAB Application Program Interface (API) ‏  a library that allows you to write C and Fortran programs that interact with MATLAB. It include facilities for calling routines from MATLAB (dynamic linking), calling MATLAB as a computational engine, and for reading and writing MAT-files.

9 What is Matlab?  Some facts for a first impression  Everything in MATLAB is a matrix !  MATLAB is an interpreted language, no compilation needed (but possible) ‏  MATLAB does not need any variable declarations, no dimension statements, has no packaging, no storage allocation, no pointers  Programs can be run step by step, with full access to all variables, functions etc.

10 What does matlab code look like?  t = 0:pi/100:2*pi;  y = sin(t);  plot(t,y) ‏

11 What does matlab code look like? Remember: EVERYTHING IN MATLAB IS A MATRIX ! creates 1 x 200 Matrix Argument and result: 1 x 200 Matrix  t = 0:pi/100:2*pi;  y = sin(t);  plot(t,y) ‏

12

13  Rows and columns are always numbered starting at 1  A single number is really a 1 x 1 matrix in Matlab!  Matlab variables are not given a type, and do not need to be declared  Any matrix can be assigned to any variable

14 Building matrices with [ ]: A = [2 7 4] A = [2; 7; 4] A = [2 7 4; 3 8 9] B = [ A A ] 274 2 7 4 274 389 274 389 274 389

15

16  A matrix can be indexed using another matrix, to produce a subset of its elements:  a = [100 200 300 400 500 600 700] b = [3 5 6]  c = a(b):  300 500 600

17 Matrices  a vector x = [1 2 5 1] x = 1 2 5 1  a matrix x = [1 2 3; 5 1 4; 3 2 -1] x = 1 2 3 5 1 4 3 2 -1

18 Matrices  x(i,j) subscription  whole row  whole column y=x(2,3)‏ y = 4 y=x(3,:)‏ y = 3 2 -1 y=x(:,2)‏ y = 2 1 2

19 Operators (arithmetic) ‏ +addition -subtraction *multiplication /division ^power ‘complex conjugate transpose.*element-by-element mult./element-by-element div.^element-by-element power.‘transpose

20 Operators (relational, logical) ==equal ~=not equal <less than <=less than or equal >greater than >=greater than or equal &AND |OR ~NOT pi3.14159265… jimaginary unit, isame as j

21 Generating Vectors from functions  zeros(M,N)MxN matrix of zeros  ones(M,N)MxN matrix of ones  rand(M,N)MxN matrix of uniformly distributed random numbers on (0,1) ‏ x = zeros(1,3)‏ x = 0 0 0 x = ones(1,3)‏ x = 1 1 1 x = rand(1,3)‏ x = 0.9501 0.2311 0.6068

22 Programming in Matlab (M-files) ‏  Executing commands in the command window is fine for short scripts; but when dealing with long scripts for complex problem-solving (or when programming) M-files is a must.  It allows the user to write MATLAB command in a text file (called M-file) and then MATLAB will open the file and execute the commands exactly as it would if the user typed them at the MATLAB Command Window. The M-file editor is the MATLAB’s tool for creating M-files.

23 Programming in Matlab (M-files) ‏  There are two basic kinds of m-files: Scripts Functions  Scripts are m-files that execute a series of statements  Functions are m-files that accept arguments and produce an output

24 Example Script 1  Find the solution, x, to the following system of equations

25 Example Script 1: solution  Use the MATLAB editor to create a new file  Enter the following statement: A = [1 2 3; 3 3 4; 2 3 3]; b = [1; 1; 2]; x = A\b

26 Example Script 2  Plot the following cosine functions, y1 = 2 cos(x), y2 = cos(x) y3 = 0.5cos(x) ‏ in the interval:

27 Example Script 1: solution  Use the MATLAB editor to create a new file  Enter the following statement: x = 0:pi/100:2*pi; y1 = 2*cos(x); y2 = cos(x); y3 = 0.5*cos(x); plot(x,y1,'--',x,y2,'-',x,y3,':') ‏ xlabel('0 \leq x \leq 2\pi') ‏ ylabel('Cosine functions') ‏ legend('2*cos(x)','cos(x)','0.5*cos(x)') ‏ title('Typical example of multiple plots') ‏ axis([0 2*pi -3 3]) ‏

28 Script shortcomings  All variables created in a script file are added to the workspace. This may be undesirable because: Variables already existing in the workspace may be overwritten. The execution of the script can be affected by the state variables in the workspace.  Use a function for anything complicated

29 The Anatomy of a Function  function f = factorial(n) (1) ‏  % FACTORIAL(N) returns the factorial of N. (2) ‏  % Compute a factorial value. (3) ‏  f = prod(1:n); (4) ‏

30 Differences between Scripts and Functions

31 Functions: input & output arguments  input arguments are listed inside parentheses following the function name.  The output arguments are listed inside the brackets on the left side. They are used to transfer the output from the function file.  The general form looks like this function [outputs] = function_name(inputs) ‏

32 Functions: input & output arguments  Examples of input and output arguments function area=TrapArea(a,b,h) Three inputs and one output function [h,d]=motion(v,angle) Two inputs and two outputs

33 Inputs to scripts? Inputs are sent to script files by:  Defining the variable in the script itself  Defining the variable in the workspace  Defining the variable when the script executes

34 Inputs to scripts? We've discussed the first two, so lets look at  Defining the variable when the script executes  This means that we will ask the user to supply some information when the script is executed Using the input command

35 The Input Command Lets try an example script % This script file calculates the average of points % scored in three games. % The point from each game are assigned to a variable % by using the `input' command. game1 = input('Enter the points scored in the first game '); game2 = input('Enter the points scored in the second game '); game3 = input('Enter the points scored in the third game '); average = (game1+game2+game3)/3

36 Flow Control  The most common decision-making (or flow control) structures in matlab are:  The if... else... end structure  Example, based on quadratic formula: discr = b*b - 4*a*c; if discr < 0 disp('Warning: discriminant is negative, roots are imaginary'); elseif discr == 0 disp('Discriminant is zero, roots are repeated') ‏ else disp('Roots are real') ‏ end

37 Flow Control  Logical operators are used to compare two objects:

38 Flow Control  Another common flow control structure is:  The for... end loop  Example: for ii=1:5 x=ii*ii end

39 Flow Control  Can also use nested loops as well.  To populate a 5x5 symmetric matrix where the indices correspond to i/j we have: n = 5; A = eye(n); for j=2:n for i=1:j-1 A(i,j)=i/j; A(j,i)=i/j; end

40 Saving output to a file  The command fprintf is used to write output to a file. To save the results of some computation to a file in a text format requires the following steps: 1. Open a file using fopen 2. Write the output using fprintf 3. Close the file using fclose

41 Saving output to a file % write some variable length strings to a file: op = fopen('weekdays.txt','wt'); fprintf(op,'Sunday\nMonday\nTuesday\nWednesday\n'); fprintf(op,'Thursday\nFriday\nSaturday\n'); fclose(op); Notes:  the option 'wt' is used in the windows platform to set a file for writing.  the command \n forces a carriage return

42 Furthering your matlab skills  The scripts presented in this lecture are merely an introduction to matlab that barely scrape the surface  To understand it yourself you must really get your hands dirty and dig into programming.  The built-in help is very good in matlab, so if you are unsure of how to do something you should check there first


Download ppt "Eng. 8054 Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) ‏ Advanced Marine Party Introduction to Matlab."

Similar presentations


Ads by Google