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

Slides:



Advertisements
Similar presentations
Introduction to MATLAB The language of Technical Computing.
Advertisements

Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
1 Pendahuluan Pertemuan 5 Matakuliah: H0062/Teori Sistem Tahun: 2006.
Introduction to Matlab Tutorial for CS4MN3/SE3X03 Wen Yu McMaster University.
Introduction to Matlab Οικονομίδης Δημήτρης
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
CIS 601 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
You can make this in matlab!. Matlab Introduction and Matrices.
Introduction to M ATLAB EE 100 – EE Dept. - JUST.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Yasser F. O. Mohammad Approximations and Round-off Errors.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Introduction to Matlab
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Matlab.
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Introduction to MATLAB
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Communication and Coding Theory Lab(CS491)
INTRODUCTION TO MATLAB
Matlab Basic Dr. Imtiaz Hussain
Experiment No. (1) - an introduction to MATLAB
Simulation And Modeling
Presentation transcript:

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

Eng Advanced Marine Vehicles Introduction to Matlab

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.

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)

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:

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].

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) ‏

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.

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.

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

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) ‏

 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

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

 A matrix can be indexed using another matrix, to produce a subset of its elements:  a = [ ] b = [3 5 6]  c = a(b): 

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

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

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

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

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 = x = ones(1,3)‏ x = x = rand(1,3)‏ x =

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.

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

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

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

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

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]) ‏

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

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) ‏

Differences between Scripts and Functions

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) ‏

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

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

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

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

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

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

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

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

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

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

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