Matlab intro The Environment

Slides:



Advertisements
Similar presentations
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Advertisements

1 Eng. Mohamed El-Taher Eng. Ahmed Ibrahim. 2 1.FUNCTION SUMMARY polyfun  Polynomial functions are located in the MATLAB polyfun directory. For a complete.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
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.
Matlab Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you might find useful.
Exam #3 Review: Comprehensive Exam Class 14.2 Palm Matlab Book Ch. 1-5.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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.
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
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.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
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 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.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Introduction to MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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)
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
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
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.
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Peng Liu Haas School of Business University of California, Berkeley
Lecture 10 2D plotting & curve fitting Subplots Other 2-D Plots Other 2-D Plots Curve fitting © 2007 Daniel Valentine. All rights reserved. Published by.
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab.
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.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Matlab for Engineers Matlab Environment Chapter 2.
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.
Introduction to MATLAB Ehsan Adeli M. Iran University of Science and Technology, E-Learing Center, Fall 2008 (1387)
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
ECE 1304 Introduction to Electrical and Computer Engineering
MATLAB DENC 2533 ECADD LAB 9.
Introduction To MATLAB
EEE 244 Numerical Methods In Electrical Engineering
MATLAB How to use (using M-files)
INTRODUCTION TO MATLAB
How to Use MATLAB A Brief Introduction.
Presentation transcript:

Matlab intro The Environment Control Lab1 Matlab intro The Environment The command prompt  Getting Help : e.g help sin, lookfor cos  Variables  Vectors, Matrices, and Linear Algebra (det, inv …)  Plotting, plot(x,y,’r’), hist (colormap([0 0 0])), semilog, loglog

M-file(Script) How to creat a script M-file Editor write your command Save the Editor (automatical save it as a M file )

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

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

Variable cont(‘) Special variables:  ans : default variable name for the result  pi: = 3.1415926…………  eps: = 2.2204e-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,matrixes,Arrays linear algebra  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 righ bracket.  A column vector can be created the same way, and the rows are separated by semicolo  Example: >> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ] x = 0 0.7854 1.5708 2.3562 3.1416 x is a row vector. >> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ] y = 0 0.7854 1.5708 2.3562 3.1416 y is a column vector.

Vectors Cont’ Vector Addressing – A vector element is addressed in MATLAB with index enclosed in parentheses. Example: >> x(3) ans = 1.5708  1st to 3rd 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 is the ending index. A shortened format (start : end) may be used if increment Example: >> x(1:3) ans = 0 0.7854 1.5708  3rd element of vector x

Matrix A is an m x n matrix.  A Matrix array is two-dimensional, having both multiple rows and multiple co 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 = 1 2 3 4 5 6 >> h = [ 2 4 6 1 3 5] h = 2 4 6 1 3 5

Matrix Cont  Example: >> f(2,3) ans = 6 >> h(:,1) ans = 2  Matrix Addressing: -- matrixname(row, column) -- colon may be used in place of a row or column referrs the entire row or column. recall: f = 1 2 3 4 5 h = 2 4 6 1 3 5

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

ARRAYS 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 = 1 2 3 4 >> g = 2*f – 1 Each element in the g = multiplied by 2, then subtracted 1 3 by 1. 5 7

ARRAYS CONT() Example: >> x = [ 1 2 3 ]; >> y = [ 4 5 6 ]; >> z = x .* y z = 4 10 18

Solutions to Systems of Linear Equations (con’t…)  Solution by Matrix Division: The solution to the equation Ax = b can be computed using left division. MATLAB: Answer: >> A = [ 3 2 -1; -1 3 2; 1 -1 -1]; >> b = [ 10; 5; -1]; >> x = A\b x = -2.0000 5.0000 -6.0000 x1 = -2, x2 = 5, x3 = -6 Solution by Matrix Inverse: Ax = b A-1 Ax = A-1 b x = A-1 b  MATLAB: >> A = [ 3 2 -1; -1 3 2; 1 -1 -1]; >> b = [ 10; 5; -1]; >> x = inv(A)*b x = -2.0000 5.0000 -6.0000 Answer: x1 = -2, x2 = 5, x3 = -6

Plotting Plotting Curves:  plot (x,y) – generates a linear plot of the values of x (horizontal axis) and y (vertical axis).  semilogx (x,y) – generate a plot of the values of x and y using a logarithmic scale for x and a linear scale for y.  semilogy (x,y) – generate a plot of the values of x and y using a linear scale for x and a logarithmic scale for y.  loglog(x,y) – generate a plot of the values of x and y using logarithmic scales for both x and y.

Plotting Multiple Curves:  plot (x, y, w, z) – multiple curves can be plotted on the same graph by using multiple arguments in a plot command. The variables x, y, w, and z are vectors. Two curves will be plotted: y vs. x, and z vs. w.  legend (‘string1’, ‘string2’,…) – used to distinguish between plots on the same graph  Multiple Figures:  figure (n) – used in creation of multiple plot windows. place this command before the plot() command, and the corresponding figure will be labeled as “Figure n”  close – closes the figure n window.  close all – closes all the figure windows.  Subplots:  subplot (m, n, p) – m by n grid of windows, with p specifying the current plot as the pth window

Plotting Example: (polynomial function) plot the polynomial using linear/linear scale, log/ linear scale, linear/log scale, & log/log scale: y = 2x^2 + 7x + 9 CODE % Generate the polynomial: x = linspace (0, 10, 100); y = 2*x.^2 + 7*x + 9; % plotting the polynomial: figure (1); subplot (2,2,1), plot (x,y); title ('Polynomial, linear/linear scale'); ylabel ('y'), grid; subplot (2,2,2), semilogx (x,y); title ('Polynomial, log/linear scale'); ylabel ('y'), grid; subplot (2,2,3), semilogy (x,y); title ('Polynomial, linear/log scale'); xlabel('x'), ylabel ('y'), grid; subplot (2,2,4), loglog (x,y); title ('Polynomial, log/log scale'); xlabel('x'), ylabel ('y'), grid;

Polynomials in MATLAB Conv Multiply polynomials Polynomial Function Summary Function Description Conv Multiply polynomials Deconv Divide polynomials Poly Polynomial with specified roots Polyder Polynomial derivative Polyfit Polynomial curve fitting Polyval Polynomial evaluation Polyvalm Matrix polynomial evaluation Residue Partial-fraction expansion (residues) Roots Find polynomial roots

Roots, Poly etc………………… How to enter a polynome in Matlab p(x)=x^3 -2x -6 In matlab p=[1 0 -2 -6]; To find the roots R=roots(p) r = 2.0946 -1.0473 + 1.1359i -1.0473 - 1.1359i To estimate a polynome at a specific value Polyval(p,value) polyval(p,5) ans=110

Conv, Decon Convolution=multiplication of Polynome Ex: a(S)=S^2 + 2S +3 b(S)= 4S^2 + 5S + 6 Take their product a=[1 2 3]; b=[ 4 5 6]; c=con(a,b) ans = 13 28 27 18 Use Deconvolution to divide back the product [q,r]=Decon(c,a) q = 4 5 6 r= 0 0 0 0 0

Derivative of Polynome To obtain the derivative of a Polynome Use Polyder() Ex: >>p= [1 0 -2 -5] >>q = polyder(p) Ans q = 3 0 -2 Ex: >>a = [1 3 5]; >>b = [2 4 6]; c=polyder(a,b) take derivative of a*b Ans c = 8 30 56 38 Ex2: Derivative of a/b [q,d]=polyder(a,b) q = -2 -8 -2 d = 4 16 40 48 36 final answer is of course q/d

Partial Fraction Consider the partial Fraction A and B are polynomiales to sovle this Ex: Consider the transfer function >>b = [-4 8]; >>a = [1 6 8]; >>[r,p,k] = residue(b,a) Ans r = -12 8 p = -4 -2 k = []

Homework Section Exercise 1: Use Matlab command to obtain the following a) Extract the fourth row of the matrix generated by magic(6) b) Show the results of ‘x’ multiply by ‘y’ and ‘y’ divides by ‘x’. Given x = [0:0.1:1.1] and y = [10:21] c) Generate random matrix ‘r’ of size 4 by 5 with number varying between -8 and 9 Exercise 2: Use MATLAB commands to get exactly as the figure shown below x=pi/2:pi/10:2*pi; y=sin(x); z=cos(x);

Consider the two polynomials P(S )=S^2 + 2S + 1 and Q(S)= S+1 . Exercise 3: Consider the two polynomials P(S )=S^2 + 2S + 1 and Q(S)= S+1 . Using MATLAB compute a. P(S )*Q(S ) b. Roots of P(S) and Q(S) c. P(-1 ) and Q(6) Exercise 4: Use MATLAB command to find the partial fraction of the following

Exersice 5: MATLAB M-file Function Consider the following equation a) Write a MATLAB M-file function to obtain numerical values of y(t). Your function must take y(0), ζ, ωn, t and θ as function inputs and y(t) as output argument. b) Write a second script m-file to obtain the plot for y(t) for 0<t<10 with an increment of 0.1, by considering the following two cases Case 1: y0=0.15 m, ωn = √2 rad/sec, ζ = 3/(2√2 ) and θ = 0; Case 2: y0=0.15 m, ωn = √2 rad/sec, ζ = 1/(2√2 ) and θ = 0; Hint: When you write the function you would require element-by-element operator