1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.

Slides:



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

Introduction to Matlab
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Maths for Computer Graphics
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Plotting. Basic Plotting Two vectors of x and y values needed. Vectors need to be of the same length, but not necessarily of the same geometry. For example,
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.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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.
1 Advanced MATLAB Vectors and matrices fprintf Cell arrays Structures Flow of control Vectorization Functions.
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.
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.
Working with Arrays in MATLAB
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
MATLAB Jirawat Kanjanapitak (Tae). What is MATLAB A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
LESSON 3 MATRICES Azalya Rahmatika Fathul Fithrah JURUSAN MATEMATIKA FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM UNIVERSITAS SYIAH KUALA DARUSSALAM,
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
MATLAB Jirawat Kanjanapitak (Tae). What is MATLAB A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D.
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.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
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.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
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.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
12-1 Organizing Data Using Matrices
Matrices and Arrays.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Arrays and Matrices in MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
EECS Introduction to Computing for the Physical Sciences
Working with Arrays in MATLAB
Presentation transcript:

1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations 2.Engineering/Scientific programs 3.Audio processing 4.Image processing

% Scalars x = 1.23; y = pi; disp(x) format long disp(y) format short disp(y)

% Scalar arithmetic x = 3; y = 2; disp(x+y) % addition: + disp(x-y) % subtraction: - disp(x*y) % multiplication: * disp(x/y) % division: / disp(x^y) % power: ^

4 Exercise 1.Calculate 4 times 5. 2.Raise 2 to the power 10. Hints: * (multiplication) ^ (raising to a power)

% vectors x = [1,2,3]; % row vector x = [1 2 3]; % commas can be replaced by spaces disp(x) disp(length(x)) disp(size(x)) y = [1;2;3]; % column vector disp(y) disp(size(y))

% Transpose x = [1 2 3]; y = x'; % transpose of real vector x disp(y) z = y'; disp(z)

7 Exercise 1.Create a row vector consisting of the whole numbers (integers) from 0 to 5. Display it. 2.Have MATLAB identify the size of this “matrix.” 3.Convert your row vector into a column vector. Display it. 4.Have MATLAB identify the size of this new “matrix.” Hints: size() ' (transpose)

% Concatenation of row vectors x = [1 2 3]; y = [4 5 6]; z = [x y]; % concatenate row vectors disp(z) z = cat(2,x,y); % concatenate by adding columns % this accomplishes the same thing as above disp(z)

% Concatenation of column vectors x = [1; 2]; y = [3; 4]; z = [x; y]; % concatenate column vectors disp(z) z = cat(1,x,y); % concatenate by adding rows % this accomplishes the same thing as above disp(z)

10 Exercise 1.Create a row vector consisting of the whole numbers (integers) from 1 to 5 and a second row vector consisting of the whole numbers 6 to 8. 2.Concatenate these two row vectors into one long row vector using two different methods: square brackets the function cat 3.Create the transpose of each of the two component row vectors that you created in step 1. 4.Concatenate these two column vectors into one long column vector using two different methods: square brackets the function cat

11 sum Sum of array elements Syntax B = sum(A) B = sum(A,dim) My comments: A is a matrix, and dim is either 1 or 2. If A is a vector, then a scalar (the sum of all elements) results. If A is a matrix (with at least 2 rows and at least 2 columns), then either a row ( dim = 1 ) or a column ( dim = 2 ) vector results. MATLAB Documentation for sum

% sum function with vector argument x = [ ]; % row vector disp(x) disp(sum(x)) y = [1; 2; 3]; % column vector disp(y) disp(sum(y))

% The functions maximum and minimum x = [1 2 3]; % The functions maximum and minimum work equally well for % row and column vectors. disp(max(x)) disp(max(x')) disp(min(x)) disp(min(x'))

14 Exercise Create a column vector consisting of the whole numbers 0 to 10. Have MATLAB find the length of this vector, the sum of its elements, the maximum and minimum elements. Hints: length() sum() max() min()

% Create vectors with the functions zeros and ones x = zeros(1,5); disp(x) y = zeros(2,1); disp(y) y = ones(1,4); disp(y)

% Create row vectors with the colon operator a = 0:2:10; % start at 0, increment by 2, end at 10 disp(a) b = 0:5; % by default, increment by 1 disp(b) c = 0:-1:-5; disp(c)

% Create row vectors with linspace and logspace x = linspace(0,1,5); % 5 values: 0 through 1 disp(x) y = logspace(0,4,5); % 5 values: 10^0 through 10^4 disp(y)

18 Exercise 1.Create a column vector of length 6, with each element a 0. 2.Create a row vector consisting of the odd integers 1 through 11, using the colon operator. 3.Create a row vector of 21 equally-spaced values between 0 and Create a row vector of 7 logarithmically-spaced values between 1 and 1,000,000. Hints: zeros() linspace() logspace()

% Vector input to built-in mathematical function x = linspace(0,pi,5); disp(x) y = sin(x); % because x is a vector, sin produces a vector disp(y)

% Basic plot x = linspace(0,4,41); y = sqrt(x); figure(1) plot(x,y,'-b') axis([ ]) saveas(1,'basic','png')

% Bigger font size and thicker lines x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([ ]) set(gca,'FontSize',24) set(findobj(2,'LineWidth',0.5),'LineWidth',2) % thick lines saveas(2,'better','png')

22 Exercise Hint. Recall that we plotted square-root like this: x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([ ])

23 rred ggreen bblue ccyan mmagenta yyellow kblack w‘white’ Color Specifiers

24 ‘-’solid line ‘--’dashed line ‘:’dotted line ‘-.’dash-dot line Line Style Specifiers

25 Exercise Hint. Recall that we plotted square-root like this: x = linspace(0,4,41); y = sqrt(x); figure(2); plot(x,y,'-b') axis([ ])

% Two curves x = linspace(0,4,41); y = sqrt(x); z = x./2; figure(5); plot(x,y,'-k',x,z,'--b') % 2 curves on same axes axis([ ]) set(gca,'FontSize',24) set(findobj(5,'LineWidth',0.5),'LineWidth',2) saveas(5,'curves','png')

27 Using an Index to Address Elements of an Array In the C/C++ programming language, an index starts at 0 and elements of an array are addressed with square brackets [∙] : x[0] x[1] x[2] x[3] x[4] ↑ ↑ ↑ ↑ ↑ In MATLAB, an index starts at 1 and elements of an array are addressed with parentheses (∙) : x(1) x(2) x(3) x(4) x(5) ↑↑ ↑ ↑ ↑

28 Square Brackets in MATLAB In MATLAB, square brackets [∙] are used for building arrays, such as vectors, matrices, and three-dimensional arrays. For example, x = [1 2 3]; y = [4 5 6]; z = [x y]; % concatenate row vectors disp(z)

% Indices (spelling lesson: 1 index, 2 or more indices) x = zeros(1,5); x(1) = 8; % change element with index 1 (first element) disp(x) x(2:5) = 7; % change elements having indices 2 through 5 disp(x) y = 1:4; x(2:5) = y; disp(x)

30 Exercise 1.Create a row vector of length 8, with each element a 0. 2.Create a row vector consisting of the whole numbers 1 to 4. 3.In the row vector of step 1, replace the last 4 elements with the vector of step 2.