Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.

Slides:



Advertisements
Similar presentations
Introduction to MATLAB
Advertisements

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Introduction to MATLAB
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Matrix Mathematics in MATLAB and Excel
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.
Matlab intro The Environment
MATLAB Fundamentals.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to MATLAB
Introduction to MATLAB 58:111 Numerical Calculations Department of Mechanical and Industrial Engineering.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
How to Use MATLAB A Brief Introduction. 2 What can MATLAB do? Matrix Operations Symbolic Computations Simulations Programming 2D/3D Visualization.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
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.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
INTRODUCTION TO MATLAB LAB# 01
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.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
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.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to MATLAB Ehsan Adeli M. Iran University of Science and Technology, E-Learing Center, Fall 2008 (1387)
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
MATLAB Basics.
INTRODUCTION TO MATLAB
Experiment No. (1) - an introduction to MATLAB
How to Use MATLAB A Brief Introduction.
CS 111 Introduction to Computing in Engineering and Science
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal

Topics Introduction MATLAB Environment Getting Help Variables Vectors, Matrices, and Linear Algebra Plotting Built in Functions Selection Programming M-Files User Defined Functions Specific Topics

Introduction  What is MATLAB ? MATLAB is a computer program that combines computation and visualization power that makes it particularly useful tool for engineers. MATLAB is an executive program, and a script can be made with a list of MATLAB commands like other programming language.  MATLAB Stands for MATrix LABoratory. The system was designed to make matrix computation particularly easy.  The MATLAB environment allows the user to: manage variables import and export data perform calculations generate plots develop and manage files for use with MATLAB.

To start MATLAB: START  PROGRAMS  MATLAB 7.0  MATLAB 7.0 MATLAB Environment

Display Windows

Display Windows (con’t…) Graphic (Figure) Window –Displays plots and graphs –Created in response to graphics commands. M-file editor/debugger window –Create and edit scripts of commands called M-files.

Getting Help type one of following commands in the command window: –help – lists all the help topic –help topic – provides help for the specified topic –help command – provides help for the specified command help help – provides information on use of the help command –helpwin – opens a separate help window for navigation –lookfor keyword – Search all M-files for keyword

Getting Help (con’t…) Google “MATLAB helpdesk” Go to the online HelpDesk provided by HelpDesk You can find EVERYTHING you need to know about MATLAB from the online HelpDesk.

Variables Variable names: –Must start with a letter –May contain only letters, digits, and the underscore “_” –Matlab is case sensitive, i.e. one & OnE are different variables. –Matlab only recognizes the first 31 characters in a variable name. Assignment statement: –Variable = number; –Variable = expression; Example: >> tutorial = 1234; >> tutorial = 1234 tutorial = 1234 NOTE: when a semi-colon ”;” is placed at the end of each command, the result is not displayed.

Variables (con’t…) Special variables: –ans : default variable name for the result –pi:  = ………… –eps:  = e-016, smallest amount by which 2 numbers can differ. –Inf or inf : , infinity –NaN or nan: not-a-number Commands involving variables: –who: lists the names of defined variables –whos: lists the names and sizes of defined variables –clear: clears all varialbes, reset the default values of special variables. –clear name: clears the variable name –clc: clears the command window –clf: clears the current figure and the graph window.

Vectors, Matrices and Linear Algebra Vectors Array Operations Matrices Solutions to Systems of Linear Equations.

MATLAB BASICS Variables and Arrays Array : A collection of data values organized into rows and columns, and known by a single name. Row 1 Row 2 Row 3 Row 4 Col 1Col 2Col 3Col 4Col 5 arr(3,2)

MATLAB BASICS Arrays The fundamental unit of data in MATLAB Scalars are also treated as arrays by MATLAB (1 row and 1 column). Row and column indices of an array start from 1. Arrays can be classified as vectors and matrices.

MATLAB BASICS Vector: Array with one dimension Matrix: Array with more than one dimension Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first ( For example: n x m array ). Total number of elements in an array is the product of the number of rows and the number of columns.

MATLAB BASICS a=3x2 matrix  6 elements b=[ ]1x4 array  4 elements, row vector c= x1 array  3 elements, column vector a(2,1)=3b(3)=3c(2)=3 Row #Column #

Vectors A row vector in MATLAB can be created by an explicit list, starting with a left bracket, entering the values separated by spaces (or commas) and closing the vector with a right bracket. A column vector can be created the same way, and the rows are separated by semicolons. Example: >> x = [ *pi 0.5*pi 0.75*pi pi ] x = >> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ] y = x is a row vector. y is a column vector.

Vectors (con’t…) Vector Addressing – A vector element is addressed in MATLAB with an integer index enclosed in parentheses. Example: >> x(3) ans =  1 st to 3 rd elements of vector x The colon notation may be used to address a block of elements. (start : increment : end) start is the starting index, increment is the amount to add to each successive index, and end is the ending index. A shortened format (start : end) may be used if increment is 1. Example: >> x(1:3) ans = NOTE: MATLAB index starts at 1.  3 rd element of vector x

Vectors (con’t…) Some useful commands: x = start:end create row vector x starting with start, counting by one, ending at end x = start:increment:end create row vector x starting with start, counting by increment, ending at or before end length(x) returns the length of vector x y = x’ transpose of vector x dot (x, y) returns the scalar dot product of the vector x and y.

Array Operations Scalar-Array Mathematics For addition, subtraction, multiplication, and division of an array by a scalar simply apply the operations to all elements of the array. Example: >> f = [ 1 2; 3 4] f = >> g = 2*f – 1 g = Each element in the array f is multiplied by 2, then subtracted by 1.

Array Operations (con’t…) Element-by-Element Array-Array Mathematics. OperationAlgebraic FormMATLAB Additiona + b Subtractiona – b Multiplicationa x ba.* b Division a  b a./ b Exponentiationabab a.^ b Example: >> x = [ ]; >> y = [ ]; >> z = x.* y z = Each element in x is multiplied by the corresponding element in y.

Matrices A is an m x n matrix.  A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays:  it begins with [, and end with ]  spaces or commas are used to separate elements in a row  semicolon or enter is used to separate rows. Example: >> f = [ 1 2 3; 4 5 6] f = >> h = [ ] h = the main diagonal

Matrices (con’t…) Matrix Addressing: -- matrixname(row, column) -- colon may be used in place of a row or column reference to select the entire row or column. recall: f = h = Example: >> f(2,3) ans = 6 >> h(:,1) ans = 2 1

Matrices (con’t…) Some useful commands: zeros(n) zeros(m,n) ones(n) ones(m,n) size (A) length(A) returns a n x n matrix of zeros returns a m x n matrix of zeros returns a n x n matrix of ones returns a m x n matrix of ones for a m x n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix. returns the larger of the number of rows or columns in A.

Matrices (con’t…) TransposeB = A’ Identity Matrixeye(n)  returns an n x n identity matrix eye(m,n)  returns an m x n matrix with ones on the main diagonal and zeros elsewhere. Addition and subtractionC = A + B C = A – B Scalar Multiplication B =  A, where  is a scalar. Matrix MultiplicationC = A*B Matrix InverseB = inv(A), A must be a square matrix in this case. rank (A)  returns the rank of the matrix A. Matrix PowersB = A.^2  squares each element in the matrix C = A * A  computes A*A, and A must be a square matrix. Determinantdet (A), and A must be a square matrix. more commands A, B, C are matrices, and m, n,  are scalars.

Solutions to Systems of Linear Equations Example: a system of 3 linear equations with 3 unknowns (x 1, x 2, x 3 ): 3x 1 + 2x 2 – x 3 = 10 -x 1 + 3x 2 + 2x 3 = 5 x 1 – x 2 – x 3 = -1 Then, the system can be described as: Ax = b Let :

Solutions to Systems of Linear Equations (con’t…) Solution by Matrix Inverse: Ax = b A -1 Ax = A -1 b x = A -1 b MATLAB: >> A = [ ; ; ]; >> b = [ 10; 5; -1]; >> x = inv(A)*b x = Answer: x 1 = -2, x 2 = 5, x 3 = -6 Solution by Matrix Division: The solution to the equation Ax = b can be computed using left division. Answer: x 1 = -2, x 2 = 5, x 3 = -6 NOTE : left division: A\b  b  Aright division: x/y  x  y MATLAB: >> A = [ ; ; ]; >> b = [ 10; 5; -1]; >> x = A\b x =

The input function displays a prompt string in the Command Window and then waits for the user to respond. my_val = input( ‘Enter an input value: ’ ); in1 = input( ‘Enter data: ’ ); in2 = input( ‘Enter data: ’,`s`); Initializing with Keyboard Input

How to display data The disp( ) function >> disp( 'Hello' ) Hello >> disp(5) 5 >> disp( [ 'Bilkent ' 'University' ] ) Bilkent University >> name = 'Alper'; >> disp( [ 'Hello ' name ] ) Hello Alper

Plotting For more information on 2-D plotting, type help graph2d Plotting a point: >> plot ( variablename, ‘symbol’) the function plot () creates a graphics window, called a Figure window, and named by default “Figure No. 1”  Example : Complex number >> z = j; >> plot (z, ‘.’)

Plotting (con’t…)

Built-in MATLAB Functions result = function_name( input ); –abs, sign –log, log10, log2 –exp –sqrt –sin, cos, tan –asin, acos, atan –max, min –round, floor, ceil, fix –mod, rem help elfun  help for elementary math functions

Selection Programming Flow Control Loops

Flow Control Simple if statement: if logical expression commands end Example: (Nested) if d <50 count = count + 1; disp(d); if b>d b=0; end Example: (else and elseif clauses) if temperature > 100 disp (‘Too hot – equipment malfunctioning.’) elseif temperature > 90 disp (‘Normal operating range.’); elseif (‘Below desired operating range.’) else disp (‘Too cold – turn off equipment.’) end

Flow Control (con’t…) The switch statement: switch expression case test expression 1 commands case test expression 2 commands otherwise commands end Example: switch interval < 1 case 1 xinc = interval /10; case 0 xinc = 0.1; end

Loops for loop for variable = expression commands end while loop while expression commands end Example (for loop): for t = 1:5000 y(t) = sin (2*pi*t/10); end Example (while loop): EPS = 1; while ( 1+EPS) >1 EPS = EPS/2; end EPS = 2*EPS the break statement break – is used to terminate the execution of the loop.

M-Files The M-file is a text file that consists a group of MATLAB commands. MATLAB can open and execute the commands exactly as if they were entered at the MATLAB command window. To run the M-files, just type the file name in the command window. (make sure the current working directory is set correctly) All MATLAB commands are M-files. So far, we have executed the commands in the command window. But a more practical way is to create a M-file.

User-Defined Function Add the following command in the beginning of your m-file: function [output variables] = function_name (input variables); NOTE: the function_name should be the same as your file name to avoid confusion.  calling your function: -- a user-defined function is called by the name of the m-file, not the name given in the function definition. -- type in the m-file name like other pre-defined commands.  Comments: -- The first few lines should be comments, as they will be displayed if help is requested for the function name. the first comment line is reference by the lookfor command.

Specific Topics This tutorial gives you a general background on the usage of MATLAB. There are thousands of MATLAB commands for many different applications, therefore it is impossible to cover all topics here. For a specific topic relating to a class, you should consult the TA or the Instructor.

Questions?

Topics Introduction MATLAB Environment Getting Help Variables Vectors, Matrices, and Linear Algebra Mathematical Functions and Applications Plotting Selection Programming M-Files User Defined Functions Specific Topics