Chapter 2 MATLAB Fundamentals.

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to Matlab
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
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.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4.
M ATLAB Matrix Laboratory. Internet Resources: Dr. H-C Chen’s CVEN 302 Web Page For MATLAB, select chap01b.ppt,
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
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.
Matlab intro The Environment
EPSII 59:006 Spring Outline Managing Your Session File Usage  Saving Workspace  Loading Data Files  Creating M-files More on Matrices Review.
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 Graphics One of the best things about MATLAB is interactive graphics “plot” is the one you will be using most often Many other 3D plotting functions.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
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.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
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.
Master in Optical Fiber Communications and Photonic Technologies Foundations of Digital Transmission - Fall quarter Introduction to Matlab.
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Plot (x-values, y-values) >> x = linspace (-3, 3, 20); >> x = linspace (-3, 3, 20); >> y = 2*x – 1; >> y = 2*x – 1; >> plot (x, y) >> plot (x, y)
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
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.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
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.
Solution Strategies 1. PROGRAMMING WITH MATLAB. MATLAB Fundamentals.
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
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
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.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
EGR 106 – Week 5 – 2-D Plots Question : Why do engineers use plots? Answer : To analyze, visualize, and present data. Matlab has many useful plotting options.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
MATLAB Fundamentals. MATLAB Matrix Laboratory Why MATLAB? Industry standard software application Wealth of built-in functions and libraries Toolboxes.
ECE 1304 Introduction to Electrical and Computer Engineering
Matrices and Arrays.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
MATLAB How to use (using M-files) Double click this icon
Matlab review Matlab is a numerical analysis system
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files)
Plotting Signals in MATLAB
How to Use MATLAB A Brief Introduction.
Introduction to Matlab
Programming The ideal style of programming is Structured or
Presentation transcript:

Chapter 2 MATLAB Fundamentals

MATLAB Matrix Laboratory

Problem-Solving Methodology H.C. Chen Problem-Solving Methodology State the problem clearly Describe the Input/Output (I/O) Work the problem by hand Algorithm - Numerical Method Develop a MATLAB Solution Debugging and Testing Documentation MATLAB Review

Why MATLAB? Industry standard software application H.C. Chen Why MATLAB? Industry standard software application Wealth of built-in functions and libraries Toolboxes (add-on software modules) – image and signal processing, control systems design, fuzzy logic, etc. Has own structured programming language Ease of application and testing (pre- and post-processing without lots of programming and formatting) Platform independent MATLAB Review

MATLAB MATLAB is a numerical analysis system Can write “programs”, but they are not formally compiled Should still use structured programming Should still use comments Comments are indicated by “%” at the beginning of the line

Program Documentation Comments!!! You must include comments in the computer programs you turn in -- otherwise we will have great difficulty knowing what you are doing

For example, here is some cryptic code without comment for j=0:2 k=(2-j)*(1+3*j)/2 end What does it do? Put a comment in % turns (0,1,2) into (1,2,0)

MATLAB Windows Command Window -- enter commands and data -- print results Graphics Window -- display plots and graphs Edit Window -- create and modify m-files

Managing MATLAB Environment who or whos -- See the current runtime environment clear -- remove all variables from memory clc -- clear the command window clf -- clear the graphics window save -- save the workspace environment load -- restore workspace from a disk file abort -- CTRL-C help -- help “command” Really good “help” command

MATLAB Syntax No complicated rules Perhaps the most important thing to remember is semicolons (;) at the end of a line to suppress output Type “more on” to keep text from leaving screen too fast diary “filename” saves a text record of session diary off turns it off

MATLAB MATLAB’s basic component is a Vector or Matrix Even single value variables (Scalars) All operations are optimized for vector use Loops run slower in MATLAB than in Fortran (not a vector operation) “size” command gives size of the matrix

Scalars, Vectors, Matrices MATLAB treat variables as “matrices” Matrix (m  n) - a set of numbers arranged in rows (m) and columns (n) Scalar: 1  1 matrix Row Vector: 1  n matrix Column Vector: m  1 matrix

>> pi ans = 3.1416 >> size(pi) 1 1 >> a=[1 2 3; 4 5 6] a = 1 2 3 4 5 6 >> size(a) ans = 2 3 a=

Complex variables MATLAB handles complex arithmetic automatically No need to compute real and imaginary parts separately The unit imaginary number i = is preassigned » x=5+2*i x = 5.0000 + 2.0000i » y=5*x+3 y = 28.0000 +10.0000i

MATLAB Example default filename filename “matrix1” » x=3+5-0.2 » who 7.8000 » y=3*x^2+5 y = 187.5200 » z=x*sqrt(y) z = 106.8116 » A=[1 2 3; 4 5 6] A = 1 2 3 4 5 6 » b=[3;2;5] b = 3 2 5 » C=A*b C = 22 52 » who Your variables are: A b y C x z » whos Name Size Bytes Class A 2x3 48 double array C 2x1 16 double array b 3x1 24 double array x 1x1 8 double array y 1x1 8 double array z 1x1 8 double array » save Saving to: matlab.mat » save matrix1 default filename filename “matrix1”

Data types All numbers are double precision Text is stored as arrays of characters You don’t have to declare the type of data (defined when running) MATLAB is case-sensitive!!!

Variable Names Usually, the name is identified with the problem Variable names may consist of up to 31 characters Variable names may be alphabetic, digits, and the underscore character ( _ ) Variable names must start with a letter ABC, A1, C56, CVEN_302 day, year, iteration, max time, velocity, distance, area, density, pressure Time, TIME, time (case sensitive!!)

Initializing Variables Explicitly list the values reads from a data file uses the colon (:) operator reads from the keyboard A = [1; 3; 5; 10]; B = [1 3 5; -6 4 -1] C = [2 3 5 1; 0 1 … (continuation) 1 -2; 3 5 1 -3] E = [A; 1; A]; F = [C(2,3); A]

Matrix Concatenation

Colon Operator Creating new matrices from an existing matrix F = C(:, 2:3) = [2,5; 0,1; 2,-1; 1,4]

Colon Operator Creating new matrices from an existing matrix E = C(2:3,:) = [-1 0 1; 3 2 -1]

Colon Operator Creating new matrices from an existing matrix G = C(3:4,1:2) = [3,2; 0,1]

Colon Operator Variable_name = a:step:b time = 0.0:0.5:2.5 Negative increment values = 10:-1:2 values = [10, 9, 8, 7, 6, 5, 4, 3, 2]

linspace Function linspace(x1, x2) gives 100 evenly spaced values between x1 and x2 x = linspace(x1,x2) linspace(a,b,n) generate n equally spaced points between a and b x = linspace(a,b,n) » linspace(0,2,11) ans = Columns 1 through 7 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 Columns 8 through 11 1.4000 1.6000 1.8000 2.0000

logspace Function logspace(a,b,n) generates a logarithmically equally spaced row vector x = logspace(a,b,n) logspace(a,b) generates 50 logarithmically equally spaced points x = logspace(a,b) » logspace(-4,2,7) ans = 0.0001 0.0010 0.0100 0.1000 1.0000 10.0000 100.0000

Special Matrices

Scalar Arithmetic Operations In order of priority (Matrix inverse) Example: x = (a + b*c)/d^2 count = count + 1

Order of Precedence of Arithmetic Operations Parentheses, starting with the innermost pair Exponentiation, from left to right Multiplication and division with equal precedence, from left to right Addition and subtraction with equal precedence, from left to right Examples: factor = 1 + b/v + c/v^2 slope = (y2 - y1)/(x2 - x1) loss = f * length/dia * (1/2 * rho * v^2) func = 1 + 0.5*(3*x^4 + (x + 2/x)^2)

Order of Precedence of Arithmetic Operations The priority order can be overridden with parentheses » a=3; b=5; c=2; » s1 = a-b*c s1 = -7 » s2=(a-b)*c s2 = -4 » y = -7.3^2 y = -53.2900 » y=(-7.3)^2 53.2900 Multiplication has higher priority than subtraction Exponentiation has higher priority than negation

Array Operations An array operation is performed element-by-element MATLAB: C = A.*B;

Element-by-Element Operations

Vector and Matrix operations But a*b gives an error (undefined) because dimensions are incorrect. Need to use .*

Vectorized Matrix Operations

Array Operations for m  n Matrices

Matrix Transpose

Built-in Functions All the standard operators +, , *, /, ^ Sqrt( ), abs( ), sin( ), cos( ), exp( ), tanh( ), acos( ), log( ), log10( ), etc. These operators are vectorized

Built-in Functions Certain functions, such as exponential and square root, have matrix definition also Use “help expm” and “help sqrtm” for details >> A = [1 3 5; 2 4 6; -3 2 -1] A = 1 3 5 2 4 6 -3 2 -1 >> B = sqrt(A) B = 1.0000 1.7321 2.2361 1.4142 2.0000 2.4495 0 + 1.7321i 1.4142 0 + 1.0000i >> C = sqrtm(A) C = 2.1045 + 0.0000i 0.1536 - 0.0000i 1.8023 + 0.0000i 1.7141 - 0.0000i 1.1473 + 0.0000i 1.7446 + 0.0000i -2.0484 + 0.0000i 1.3874 + 0.0000i 0.5210 - 0.0000i (element by element) (C*C = A)

MATLAB Graphics One of the best things about MATLAB is interactive graphics “plot” is the one you will be using most often Many other 3D plotting functions -- plot3, mesh, surfc, etc. Use “help plot” for plotting options To get a new figure, use “figure” logarithmic plots available using semilogx, semilogy and loglog

Plotting Commands plot(x,y) defaults to a blue line plot(x,y,’ro’) uses red circles plot(x,y,’m*’) uses magenta asterisks If you want to put two plots on the same graph, use “hold on” plot(a,b,’r:’) (red dotted line) hold on plot(a,c,’ko’) (black circles)

Free-Falling Bungee Jumper Use built-in functions sqrt & tanh >> g = 9.81; m = 75.2; cd = 0.24; >> t = 0:1:20 t = Columns 1 through 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Columns 16 through 21 15 16 17 18 19 20 >> v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t) v = Columns 1 through 9 0 9.7089 18.8400 26.9454 33.7794 39.2956 43.5937 46.8514 49.2692 Columns 10 through 18 51.0358 52.3119 53.2262 53.8772 54.3389 54.6653 54.8956 55.0579 55.1720 Columns 19 through 21 55.2523 55.3087 55.3484

Plot the velocity versus time curve >> plot(t,v); grid on >> title('Free Falling Bungee Jumper') >> xlabel('time t (second)'); ylabel('velocity v (m/s)') >> print –djpeg bungee.jpg How to change line thickness, line color, font size, symbols, marker size, etc.?

Color, Symbols, and Line Types Use “help plot” to find available Specifiers b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star y yellow s square k black d diamond v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram Colors Symbols Line Types

Adjust line thickness, font size, marker size, etc. » x=0:0.1:5; » y=2*x.^3-12*x.^2+8*x-6; » H=plot(x,y,'b',x,y,'r*'); » set(H,'LineWidth',3,'MarkerSize',12) » xlabel('x'); ylabel('y'); » title('f(x)=2x^3-12x^2+8x-6'); » print -djpeg075 poly.jpg Adjust line thickness, font size, marker size, etc. element-by-element operations x.^n

» x=0:0.1:10; » y=sin(2.*pi*x)+cos(pi*x); » H1=plot(x,y,'m'); set(H1,'LineWidth',3); hold on; » H2=plot(x,y,'bO'); set(H2,'LineWidth',3,'MarkerSize',10); hold off; » xlabel('x'); ylabel('y'); » title('y = sin(2\pix)+cos(\pix)'); » print -djpeg075 function.jpg

» x=0:0.1:3; y1=exp(-x); y2=sqrt(x); » H=plot(x,y1,'b-',x,y2,'r--'); » set(H,'LineWidth',3) » xlabel('x'); ylabel('y'); title('MATLAB Plots'); » H1=text(1.8,0.2,'exp(-x)'); set(H1,'FontSize',18); » H2=text(1.8,1.3,'sqrt(x)'); set(H2,'FontSize',18);

figure or figure (#) : open a figure Plotting Commands plot (x, y) plot(x1, y1, x2, y2) plot (x, y, ‘color symbol line style’) » x = linspace(0, 2*pi); » y = sin (2.*x); » z = cos (0.5*x); » plot (x, y) » plot (x, y, x, z) » figure (2) » plot (x, y, 'r o -'); grid on » hold on » plot (x, z, 'b * :') figure or figure (#) : open a figure (red, circle, solid line) (blue, star, dotted line)

Graphics Commands Axis, Labels, and Title xlabel ( label ) ylabel ( label ) title ( title of the plot ) text ( x_location, y_location,  text  ) axis ( [ x_min x_max y_min y_max ] ) » xlabel (Time) » ylabel (Temperature) » title (Temperature Record : 1900 - 2000) » text (17, 120, Record High ) » text (85, -40, Record Low ) » axis ([0 100 -50 140]) » hold off   - text string

CVEN 302-501 Homework No. 1 Chapter 2 Problems 2.2 (15), 2.4 (15), 2.7 (20), 2.10 (20), 2.11 (20). Due Friday. 09/05/2008 at the beginning of the period