MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.

Slides:



Advertisements
Similar presentations
Introduction to MATLAB
Advertisements

Multidimensional Array
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
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.
Introduction to MATLAB 7 for Engineers
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
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.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
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,
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)
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.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
26 - 3/20/2000AME 150L1 M ATLAB ® /20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
Introduction to Matlab By: Jafar Muhammadi April 2005.
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.
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)
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.
BASICS OF MATLAB Engr.Mian Shahzad Iqbal Lecturer,Telecom Department University of Engineering & Technology Taxila.
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.
ECE 1304 Introduction to Electrical and Computer Engineering
BASIC ELEMENTS OF A COMPUTER PROGRAM
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 (Lecture 2) BY:MAHA ALMOUSA.
MATLAB Basics.
Lecture 2 Introduction to MATLAB
Computational Methods
Introduction to Matlab
INTRODUCTION TO MATLAB
Experiment No. (1) - an introduction to MATLAB
Introduction to Matlab
CS 111 Introduction to Computing in Engineering and Science
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

MATLAB Basics CS 111 Introduction to Computing in Engineering and Science

CS 1112 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)

CS 1113 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.

CS 1114 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.

CS 1115 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 #

CS 1116 MATLAB BASICS Variables A region of memory containing an array, which is known by a user-specified name. Contents can be used or modified at any time. Variable names must begin with a letter, followed by any combination of letters, numbers and the underscore (_) character. Only the first 31 characters are significant. The MATLAB language is Case Sensitive. NAME, name and Name are all different variables. Give meaningful (descriptive and easy-to-remember) names for the variables. Never define a variable with the same name as a MATLAB function or command.

CS 1117 MATLAB BASICS Common types of MATLAB variables double: 64-bit double-precision floating-point numbers They can hold real, imaginary or complex numbers in the range from ± to ± with 15 or 16 decimal digits. >> var = 1 + i ; char: 16-bit values, each representing a single character The char arrays are used to hold character strings. >> comment = ‘This is a character string’ ; The type of data assigned to a variable determines the type of variable that is created.

CS 1118 Initializing Variables in Assignment Statements An assignment statement has the general form var = expression Examples: >> var = 40 * i;>> a2 = [0 1+8]; >> var2 = var / 5;>> b2 = [a2(2) 7 a]; >> array = [ ];>> c2(2,3) = 5; >> x = 1; y = 2; >> d2 = [1 2]; >> a = [3.4];>> d2(4) = 4; >> b = [ ]; >> c = [1.0; 2.0; 3.0]; >> d = [1, 2, 3; 4, 5, 6]; ‘;’ semicolon suppresses the >> e = [1, 2, 3 automatic echoing of values but 4, 5, 6]; it slows down the execution. MATLAB BASICS

CS 1119 MATLAB BASICS Initializing Variables in Assignment Statements Arrays are constructed using brackets and semicolons. All of the elements of an array are listed in row order. The values in each row are listed from left to right and they are separated by blank spaces or commas. The rows are separated by semicolons or new lines. The number of elements in every row of an array must be the same. The expressions used to initialize arrays can include algebraic operations and all or portions of previously defined arrays.

CS MATLAB BASICS Initializing with Shortcut Expressions first: increment: last Colon operator: a shortcut notation used to initialize arrays with thousands of elements >> x = 1 : 2 : 10; >> angles = (0.01 : 0.01 : 1) * pi; Transpose operator: (′) swaps the rows and columns of an array >> f = [1:4]′ ; >> g = 1:4; >> h = [ g′ g′ ]; h=h=

CS Initializing with Built-in Functions zeros(n)>> a = zeros(2); zeros(n,m)>> b = zeros(2, 3); zeros(size(arr))>> c = [1, 2; 3, 4]; ones(n)>> d = zeros(size(c)); ones(n,m) ones(size(arr)) eye(n) eye(n,m) length(arr) size(arr) MATLAB BASICS

CS Initializing with Keyboard Input 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`); MATLAB BASICS

CS MATLAB BASICS 8 11 Multidimensional Arrays A two dimensional array with m rows and n columns will occupy mxn successive locations in the computer’s memory. MATLAB always allocates array elements in column major order. a= [1 2 3; 4 5 6; 7 8 9; ]; a(5) = a(1,2) = 2 A 2x3x2 array of three dimensions c(:, :, 1) = [1 2 3; ]; c(:, :, 2) = [7 8 9; ];

CS Subarrays It is possible to select and use subsets of MATLAB arrays. arr1 = [ ]; arr1(3) is 3.3 arr1([1 4]) is the array [ ] arr1(1 : 2 : 5) is the array [ ] For two-dimensional arrays, a colon can be used in a subscript to select all of the values of that subscript. arr2 = [1 2 3; ; 3 4 5]; arr2(1, :) arr2(:, 1:2:3) MATLAB BASICS

CS Subarrays The end function: When used in an array subscript, it returns the highest value taken on by that subscript. arr3 = [ ]; arr3(5:end) is the array [ ] arr4 = [ ; ; ]; arr4(2:end, 2:end) Using subarrays on the left hand-side of an assignment statement: arr4(1:2, [1 4]) = [20 21; 22 23]; (1,1) (1,4) (2,1) and (2,4) are updated. arr4 = [20 21; 22 23]; all of the array is changed. MATLAB BASICS

CS Subarrays Assigning a Scalar to a Subarray: A scalar value on the right-hand side of an assignment statement is copied into every element specified on the left-hand side. >> arr4 = [ ; ; ]; >> arr4(1:2, 1:2) = 1 arr4 = MATLAB BASICS

CS Special Values MATLAB includes a number of predefined special values. These values can be used at any time without initializing them. These predefined values are stored in ordinary variables. They can be overwritten or modified by a user. If a new value is assigned to one of these variables, then that new value will replace the default one in all later calculations. >> circ1 = 2 * pi * 10; >> pi = 3; >> circ2 = 2 * pi * 10; Never change the values of predefined variables. MATLAB BASICS

CS Special Values pi:  value up to 15 significant digits i, j: sqrt(-1) Inf: infinity (such as division by 0) NaN: Not-a-Number (division of zero by zero) clock: current date and time in the form of a 6-element row vector containing the year, month, day, hour, minute, and second date: current date as a string such as 16-Feb-2004 eps: epsilon is the smallest difference between two numbers ans: stores the result of an expression MATLAB BASICS

CS Changing the data format >> value = ; format short  format long  format short e  e+001 format long e  e+001 format short g  format long g  format rat  1000/81 MATLAB BASICS

CS MATLAB BASICS The disp( array ) function >> disp( 'Hello' ) Hello >> disp(5) 5 >> disp( [ 'Bilkent ' 'University' ] ) Bilkent University >> name = 'Alper'; >> disp( [ 'Hello ' name ] ) Hello Alper

CS MATLAB BASICS The num2str() and int2str() functions >> d = [ num2str(16) '-Feb-' num2str(2004) ]; >> disp(d) 16-Feb-2004 >> x = 23.11; >> disp( [ 'answer = ' num2str(x) ] ) answer = >> disp( [ 'answer = ' int2str(x) ] ) answer = 23

CS MATLAB BASICS The fprintf( format, data ) function –%dinteger –%ffloating point format –%eexponential format –%geither floating point or exponential format, whichever is shorter –\nnew line character –\ttab character

CS MATLAB BASICS >> fprintf( 'Result is %d', 3 ) Result is 3 >> fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2 ) Area of a circle with radius 3 is >> x = 5; >> fprintf( 'x = %3d', x ) x = 5 >> x = pi; >> fprintf( 'x = %0.2f', x ) x = 3.14 >> fprintf( 'x = %6.2f', x ) x = 3.14 >> fprintf( 'x = %d\ny = %d\n', 3, 13 ) x = 3 y = 13

CS MATLAB BASICS Data files save filename var1 var2 … >> save myfile.mat x y  binary >> save myfile.dat x –ascii  ascii load filename >> load myfile.mat  binary >> load myfile.dat –ascii  ascii

CS MATLAB BASICS variable_name = expression; –additiona + b  a + b –subtractiona - b  a - b –multiplicationa x b  a * b –divisiona / b  a / b –exponenta b  a ^ b

CS MATLAB BASICS Hierarchy of operations x = 3 * / 2 Processing order of operations is important –parentheses (starting from the innermost) –exponentials (from left to right) –multiplications and divisions (from left to right) –additions and subtractions (from left to right) >> x = 3 * / 2 x = 9

CS MATLAB BASICS 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

CS MATLAB BASICS Types of errors in MATLAB programs Syntax errors –Check spelling and punctuation Run-time errors –Check input data –Can remove “;” or add “disp” statements Logical errors –Use shorter statements –Check typos –Check units –Ask your friends, assistants, instructor, …

CS MATLAB BASICS Summary help command  Online help lookfor keyword  Lists related commands which  Version and location info clear  Clears the workspace clc  Clears the command window diary filename  Sends output to file diary on/off  Turns diary on/off who, whos  Lists content of the workspace more on/off  Enables/disables paged output Ctrl+c  Aborts operation …  Continuation %  Comments