Lecture 2 Introduction to MATLAB

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Multidimensional Array
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.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
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
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.
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.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
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,
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
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 &
Working with Arrays in MATLAB
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
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 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.
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
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 Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
An Introduction to Programming in Matlab Emily Blumenthal
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Matlab Programming for Engineers
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Matlab
EGR 115 Introduction to Computing for Engineers
Lecture: MATLAB Chapter 1 Introduction
EGR 115 Introduction to Computing for Engineers
Other Kinds of Arrays Chapter 11
2) Platform independent 3) Predefined functions
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
MATH 493 Introduction to MATLAB
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.
MATLAB Basics.
Fourth Year – Software Engineering
Communication and Coding Theory Lab(CS491)
Computational Methods
INTRODUCTION TO MATLAB
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.
Experiment No. (1) - an introduction to MATLAB
CS 111 Introduction to Computing in Engineering and Science
Working with Arrays in MATLAB
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Lecture 2 Introduction to MATLAB Dr .Qi Ying

The MATLAB Environment The “Desktop” Environment Command Window (Interactive commands) Edit/Debug Window Workspace Browser Figure Windows Help Window

Variables and Arrays The fundamental unit of data in MATLAB is the array Vectors (1 dimension, row vector or column vector) Matrices (2 or more dimensions) Scalars are treated as arrays too (1x1 array) A MATLAB variable is a region of memory containing an array indexed by the variable name Naming convention letters and numbers, ‘_’ Case sensitive: use only lowercase names to avoid confusion Only first 63 characters are significant Pick meaningful names (year, month, exchange_rate, etc. are good names; a1, ccc, xx are examples of bad names) Predefined variables (pi, i, j, clock, date, eps, ans) Unlike many other programming languages (Fortran, C, C++…), MATLAB variables are not type specific: No need to specify the variable type (integer, real, double precision, or character) before using the variables

Initializing variables Method 1: Assignment statement var = expression Note: when defining an array, make sure the number of elements in every row and column are the same, or an error will occur Use semicolon(;) to suppress the auto-echoing of the variable value in the command window The colon (:) operator can be used to assign a series of equally spaced values Built-in function (zeros, ones, eye) Multiple assignments can be done in a single line, using ‘;’ to separate the assignment statement Method 2: Interactive input from keyboard age=input(‘Please enter your age:’) Method 3: Read data from a data file (load, dlmread, etc.) array1=load(‘mydata.dat’)

Multidimensional Arrays Row 1 (1,1) Row 2 Row 3 (3,1) (3,4) Col 1 Col 4 Sequence in the memory: (1,1) , (2,1), (3,1), (1,2), (2,2), (3,2), … (2,4), (3,4) Elements in a multidimensional array can be addressed using a single index. For example: a=[1 2 3; 4 5 6] The value of a(3) will be 2.

Subarray Elements of the subarray can be selected by: Colon operator a(1:3, 2:5), a(:,2), a(4:end,2:end) A vector of array index a([1 4 7], 2), a([1 2 3], 4:end)

Display output data disp command fprintf: formattable display function >>x=335.2344564356 >>fprintf(‘My number is %6.2f\n’, x); My number is 535.23 %: indicator of a formatted variable to be displayed 6: saves at least 6 characters 2: number of decimals f: real number (with decimals). Other data types are: d: integer; e: scientific notation g: let matlab choose f or e itself Note: the data type must match the variable to print. \n: change line. x: the numerical variables to be printed.

Operations Scalar Matrix Element-by-element operation (.). For example, a.*b vs. a*b Left division (\). For example, x=A\b, which is equivalent to x=inv(A)*b. Useful for solving simultaneous linear equations

Basic 2D Plotting Basic plotting function in MATLAB is ‘plot’ Example: Plot colors, marker styles and line styles: x=0:0.1*pi:2*pi; y=sin(x); plot(x,y,’o-’) legend(‘y=sin(x)’)

Basic 2D Plotting Subplot - Generate a figure with multiple-panels x=0:0.1*pi:2*pi; y1=sin(x); y2=cos(x); subplot(1,2,1); plot(x,y1,'o-'); xlabel('x'); ylabel('y=sin(x)'); title('(a)‘); legend('y=sin(x)') subplot(1,2,2); plot(x,y2,'x:'); xlabel('x'); ylabel('y=cos(x)'); title('(b)') legend('y=cos(x)','Location','Best')

Save a plot Using the menu in the figure window Using command print Can be used in the MATLAB program to automatically save the figure. >> print -dpng test2_ver2.png Other useful formats: -djpeg JPEG image -deps encapsulated postscript image -dtiff compressed TIFF image

M-file A series of commands can be save in the M-file as a program. For example, the plotting codes used in the previous example can be entered using the editor:

M-file MATLAB will search under current directory for M-files. By default, all M-files will have suffix .m in the file name. For example, test1.m. You don’t need to type the .m suffix when executing a program

Directory related commands Current directory: pwd Make a directory: mkdir Change to a directory: cd >> pwd ans = C:\Users\qying\Documents\MATLAB >> mkdir test2 >> cd test2 Special command: cd .. Go to the parent directory

Directory related commands List files in the current directory >> cd lab1 >> dir . test1.png test2_ver2.png .. test2.m untitled.png test1.m test2.png Special files: .. parent directory . Current directory