Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Introduction to Matlab Οικονομίδης Δημήτρης
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
1 Statistical Computing in MATLAB AMS 597 Ling Leng.
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.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
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,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
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.
Basics of MATLAB By DR. Wafaa Shabana
Matlab The language of Technical computing Mr. D. Suresh Assistant Professor, Dept. of CSE, PSNA CET, Dindigul.
Introduction to Matlab
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
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.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CSE123 Lecture 3 Files and File ManagementScripts.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Introduction to Matlab
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.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
INTRODUCTION TO MATLAB 1. OUTLINE:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Introduction to Matlab
L – Modeling and Simulating Social Systems with MATLAB
Introduction to Matlab ( Google Search )
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
Introduction To MATLAB
StatLab Matlab Workshop
Part I – Matlab Basics.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Introduction to Matlab LAB 4
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.
Presentation transcript:

Introduction to Matlab

What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra  Solving nonlinear equations  Numerical solution of differential equations  Mathematical optimization  Statistics and data analysis  Signal processing  Modelling of dynamical systems  Solving partial differential equations  Simulation of engineering systems

Matlab Screen  Command Window type commands  Current Directory View folders and m-files  Workspace View program variables Double click on a variable to see it in the Array Editor  Command History View past commands Save a whole session using diary

MATLAB Variable Names Variable names ARE case sensitive Variable names must start with a letter followed by letters, digits, and underscores.

MATLAB Special Variables ansDefault variable name for results piValue of  epsSmallest incremental number infInfinity NaNNot a numbere.g. 0/0 i square root of -1

MATLAB Math & Assignment Operators Power ^ or.^a^b or a.^b Multiplication * or.*a*b or a.*b Division / or./a/b or a./b or \ or.\b\a or b.\a NOTE: 56/8 = 8\56 Addition + a + b Subtraction - a - b Assignment =a = b (assign b to a )

Interactive Calculations Matlab is interactive, no need to declare variables >> a=5 >>b=a/2 >> a=2+3*4/2 >> a=5e-3; b=1; c=a+b Most elementary functions and constants are already defined >> cos(pi) >> abs(1+i) >> sin(pi) Last call gives answer e-016 !?

Vectors and Matrices Vectors (arrays) are defined as >> v = [1, 2, 4, 5] >> w = [1; 2; 4; 5] Matrices (2D arrays) defined similarly >> A = [1,2,3;4,-5,6;5,-6,7]

Vectors and Matrices  a vectorx = [ ] x =  a matrixx = [1 2 3; 5 1 4; ] x =  transposey = x’ y =

Vectors and Matrices  t =1:10 t =  k =2:-0.5:-1 k =  B = [1:4; 5:8] x =

Generating Vectors from functions zeros(M,N)MxN matrix of zeros >> zeros(5,1); ones(M,N)MxN matrix of ones rand(M,N)MxN matrix of uniformly distributed random numbers on (0,1) A=rand(5); B=rand(1,5); C=rand(5,1) a=randn(5); b=randn(1,5); c=randn(5,1) x = zeros(1,5) x = x = ones(1,3) x = x = rand(1,3) x =

Matrix Operators All common operators are overloaded >> v + 2 Common operators are available >> B = A’ >> A*B >> A+B Note:  Matlab is case-sensitive A and a are two different variables Transponate conjugates complex entries; avoided by >> B=A’

Indexing Matrices Index complete row or column using the colon operator >> A(1,:) Can also add limit index range >> A(1:2,:) >> A([1 2],:) General notation for colon operator >> v=1:5 >> w=1:2:5

Matrix Index The matrix indices begin from 1 (not 0 (as in C)) The matrix indices must be positive integer Given: A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions.

Concatenation of Matrices x = [1 2], y = [4 5], z=[ 0 0] A = [ x y] B = [x ; y] C = [x y ;z] Error: ??? Error using ==> vertcat CAT arguments dimensions are not consistent.

Concatenation of Matrices

Operators (arithmetic) +addition -subtraction *multiplication /division ^power

Matrices Operations Given A and B: AdditionSubtractionProductTranspose

Operators (Element by Element).*element-by-element multiplication./element-by-element division.^element-by-element power

The use of “.” – “Element” Operation K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y Erorr: ??? Error using ==> mtimes Inner matrix dimensions must agree. A = [1 2 3; 5 1 4; 3 2 1] A = y = A(:,3) y= b = x.* y b= c = x. / y c= d = x.^2 d= x = A(1,:) x= 1 2 3

Basic Task: Plot the function sin(x) between 0 ≤ x ≤4π Create an x-array of 100 samples between 0 and 4 π. Calculate sin(.) of the x-array Plot the y-array >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y)

Plot the function e -x/3 sin(x) between 0 ≤ x ≤4π Create an x-array of 100 samples between 0 and 4 π. Calculate sin(.) of the x-array Calculate e -x/3 of the x-array Multiply the arrays y and y1 >>x=linspace(0,4*pi,100); >>y=sin(x); >>y1=exp(-x/3); >>y2=y*y1;

Plot the function e -x/3 sin(x) between 0 ≤ x ≤4π Multiply the arrays y and y1 correctly Plot the y2-array >>y2=y.*y1; >>plot(y2)

Display Facilities plot(.) stem(.) Example: >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y) >>plot(x,y) Example: >>stem(y) >>stem(x,y)

Display Facilities title(.) xlabel(.) ylabel(.) >>title(‘This is the sinus function’) >>xlabel(‘x (secs)’) >>ylabel(‘sin(x)’)

Operators (relational, logical) == Equal to ~= Not equal to < Strictly smaller > Strictly greater <= Smaller than or equal to >= Greater than equal to & And operator | Or operator

Flow Control if for while break ….

Control Structures If Statement Syntax if (Condition_1) Matlab Commands elseif (Condition_2) Matlab Commands elseif (Condition_3) Matlab Commands else Matlab Commands end Some Dummy Examples if ((a>3) & (b==5)) Some Matlab Commands; end if (a<3) Some Matlab Commands; elseif (b~=5) Some Matlab Commands; end if (a<3) Some Matlab Commands; else Some Matlab Commands; end

Control Structures For loop syntax for i=Index_Array Matlab Commands end Some Dummy Examples for i=1:100 Some Matlab Commands; end for j=1:3:200 Some Matlab Commands; end for m=13:-0.2:-21 Some Matlab Commands; end for k=[ ] Some Matlab Commands; end

Control Structures While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end

Use of M-File Click to create a new M-File Extension “.m” A text file containing script or function or program to run

Use of M-File If you include “;” at the end of each statement, result will not be shown immediately Save file as Denem430.m

Writing User Defined Functions Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is You should write this command at the beginning of the m- file and you should save the m-file with a file name same as the function name function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)

Writing User Defined Functions Examples  Write a function : out=squarer (A, ind) Which takes the square of the input matrix if the input indicator is equal to 1 And takes the element by element square of the input matrix if the input indicator is equal to 2 Same Name

Writing User Defined Functions Another function which takes an input array and returns the sum and product of its elements as outputs The function sumprod(.) can be called from command window or an m-file as

Notes: “%” is the neglect sign for Matlab (equaivalent of “//” in C). Anything after it on the same line is neglected by Matlab compiler. Sometimes slowing down the execution is done deliberately for observation purposes. You can use the command “pause” for this purpose pause %wait until any key pause(3) %wait 3 seconds

Useful Commands The two commands used most by Matlab users are >>help functionname >>lookfor keyword

Questions ?

Thank You…