INTRODUCTION TO MATLAB LAB# 01

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to Matlab EE 2303 Lab. MATLAB stands for “Matrix Laboratory” APPLICATIONS OF MATLAB:  Mathematical Calculations  Data Analysis & Visualization.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
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 MATLAB MECH 300H Spring Starting of 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 Matlab Jianguo Wang CSSCR September 2009.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
1 Statistical Computing in MATLAB AMS 597 Ling Leng.
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.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Yasser F. O. Mohammad Approximations and Round-off Errors.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Eng Ship Structures 1 Introduction to Matlab.
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.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Basics of MATLAB By DR. Wafaa Shabana
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
OUTLINE Overview Numbers, variables and similar in Matlab
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
Introduction to Matlab
Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Matlab for Engineers Matlab Environment Chapter 2.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Mat lab
Statistical Computing in MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
Introduction To MATLAB
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Presentation transcript:

INTRODUCTION TO MATLAB LAB# 01

Introduction to Matlab What is Matlab? Matlab is a commercial “MATrix LABoratory” package by Mathworks, which operates as an interactive programming environment with graphical output. The MATLAB programming language is exceptionally straight forward since almost every data object is assumed to be an Array. In engineering MATLAB is displacing popular programming languages, due to its interactive interface, reliable algorithmic foundation, fully extensible environment and availability of different tool boxes.

Introduction to MATLAB Entering and Running MATLAB On a system running Microsoft Windows double click on the Matlab icon to launch Matlab. A command window will appear with the prompt >> you are now in MATLAB. Leaving Matlab A MATLAB session may be terminated by simply typing >> quit or by typing >>exit at the MATLAB prompt. Online Help Online help is available from the MATLAB prompt both generally and for specific commands. >> help >> help demo

Desktop Tools (Matlab v7) Command Window type commands Workspace view program variables clear to clear double click on a variable to see it in the Array Editor Command History view past commands save a whole session using diary

Variables MATLAB is case sensitive, that is Variable Assignment: ‘a’ is not the same as ‘A’ MATLAB has built in variables like pi, eps and ans. The variable ans will keep track of the last output which was not assigned to another variable. Variable Assignment: The equality sign is used to assigned values to variables. >> x = 3 y = x ^ 2 Out put can be suppressed by appending a semicolon to the command lines. >> x = 3 ; y = x ^ 2 ;

Variables Active Variables: Removing Variables Who Removing Variables Clear x Clear Saving and Restoring Variables Save filename Load filename

Variable Arithmetic Operator precedence Double Precision Arithmetic 2 + 3 *4 ^ 2 Double Precision Arithmetic Normally the results will be displayed in a shorter form. a = sqrt( 2 ) >> a = 1.4142 Format long b = sqrt ( 2 ) >> b = 1.41421356………. Format short Command Line Editing The arrow keys allow “ command line editing”

Built in Mathematical Functions Functions Meaning Examples Sin sine sin ( pi )=0.0 Cos cosine cos ( pi )=1.0 Tan tangent tan ( pi / 4)=1.0 Exp exponential exp(1.0)=2.7183 log natural log log(2.7183)=1.0 Arguments to trigonometric functions are given in radians. x= pi / 3; sin( x ) ^ 2 + cos ( x ) ^ 2 = ?

Matrices The element within a row of a matrix may be separated by a commas as well as a blank. The elements of a matrix being created are enclosed by brackets. A matrix is entered in “row major order” [i.e. all of the first row, then all of the second row; etc]; Rows are separated by semicolon [or a new line], and the elements of the row may be separated by either a comma or space. The following commands will create a 3 x 3 matrix and assigned it to the variable A. >> A = [1 2 3; 4 5 6; 7 8 9]; or A = [1,2,3;4,5,6;7,8,9] >> A = [ 1 2 3 4 5 6 7 8 9 ]

Matrices The matrix element located in the i-th row and j-th column of A is referred to in the usual way: >> A (1 , 2), A ( 2 , 3) Matrices can be easily modified: A ( 2 , 3 ) = 10; Building Matrices from a Block: Large matrices can be assembled from smaller matrix blocks i.e. C = [A;10 11 12]; [A; A; A] [A, A, A] >> B = [A, zeros(3,2); zeros(2,3), eye( 2 ) ] ?

Built in Matrix Functions Function Description diag return diagonal M.E as a vector eye identity matrix magic magic squares ones matrix of ones rand randomly generated matrix zeros matrix of zeros

Built in Matrix Functions Matrices of Random Entries: >> rand ( 3 ) >> rand ( m , n ) Magic Squares: A magic square is a square matrix which has equal sums along all its rows and columns. >> magic ( 4 ) Matrix of Ones: >> eye ( m , n ) >> eye ( n ) Matrices of Zeros: >> zeros ( m , n ) >> zeros ( n ) Diagonal Matrices: >> diag (A) diag ( diag ( A ) ) ?

Matrix Operations + Addition - Subtraction .* element-by-element mul * Multiplication ^ Power ‘ Transpose / Division * If the sizes of the matrices are incompatible for the matrix operation, an error message will result. .* element-by-element mul ./ element-by-element div .^ element-by-element power .‘ transpose

Matrix Operations Matrix Transpose: >> A’ Matrix Addition / Subtraction: A + B, A – B Matrix Multiplication; A * B , B * A. Round Floating Point Numbers to Integers: >> f = [-.5 .1 .5 ] round (f) ceil (f) floor (f) sum (f) prod (f) Matrix Element Level Operations: The matrix operation of addition and subtraction are already operates on an element by element basis but other operation given above do not. Matlab has a convention in which a dot in front of the operations is used. i.e [1 , 2 , 3 , 4 ] . * [ 1 , 2 , 3 , 4 ] [ 1 , 2 , 3 , 4 ] . ^ 2

Operators (relational, logical) == equal ~= not equal < less than <= less than or equal > greater than >= greater than or equal & AND | OR ~ NOT

Branching Constructs If – end Construct: If - else - end Construct: if < condition >, < program > end If - else - end Construct: if < condition 1 >, < program 1> else < program2 > If - elseif - end Construct: if < condition1 >, < program 1> elseif <condition2> < program2 > end

Looping Constructs For Loops: While Loops: Nested For Loops: for i = 1 : n , < program>, end While Loops: while < condition >, < program >, Nested For Loops: for i = 1 : n , for j = 1 : n , A(i,j) = i/j ; end

Matlab M-files Matlab commands can be run from one file without having to enter each command one by one at Matlab prompt. In order to use the programs later in Matlab they are to be saved first. For this purpose programs should be written in the editor / debugger. In command window go to File menu, new and select M-file. Code your algorithm Execute it from the command window by typing file name

Matlab User Defined Function Matlab User Defined Function can have an input and output. Arguments can be passed to a function for computation For this purpose programs should be written in the editor / debugger. In command window go to File menu, new and select M-file. function add x = 3; y = 5; z = x + y Save the file and write add at the command prompt function addv (x,y) Z = x + y Save the file and write addv(5,6) at the command prompt % is used for commenting in front of a statement

Input/ Output Request User Input Ouput Data data=input(‘message’); data=input(‘message’,’s’) Ouput Data disp(‘message’) disp(variable_name)

Matlab Graphics x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function')

Multiple Graphs t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on

Multiple Graphs x = 0 : .01 : 2 * pi; y1= sin (x); y2 =sin (2*x); plot(x,y1,‘--',x,y2,‘-‘,x,y3,‘+') grid title ('Dashed line and dotted line graph')

Multiple Plots t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1) plot(t,y1) subplot(2,2,2) plot(t,y2)

Three Dimensional Graphics x = -1:.1:1 ; y = -1:.1:1; for i=1:1:length(x) for j=1:1:length(y) z(i,j)=x(i)^2+y(j)^2; end mesh(z);

Graph Functions (summary) plot (x,y) linear plot plot (x,y1,x,y2) multiple plots on the same graph mesh(z) 3-D graph stem (x) discrete plot xlabel (‘X-axis label ’) add X-axis label ylabel (‘Y-axis label ’) add Y-axis label title (‘title of plot’) add graph title subplot (m,n,p) divide figure window grid add grid lines hold hold current graph in the figure zoom allow zoom in/out using mouse figure create new figure window pause wait for user response