EEE 242 Computer Tools for Electrical Engineering Lecture I Mustafa KARABULUT.

Slides:



Advertisements
Similar presentations
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Advertisements

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 Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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 Jianguo Wang CSSCR September 2009.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
You can make this in matlab!. Matlab Introduction and Matrices.
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.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
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.
Basics of MATLAB By DR. Wafaa Shabana
Sundermeyer MAR 550 Spring Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Introduction to Matlab ® Programming.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Engineering Analysis – Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
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 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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
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.
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.
What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Mat lab
Lecture: MATLAB Chapter 1 Introduction
Statistical Computing in MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Laboratory in Oceanography: Data and Methods
Computer Simulation Lab
Presentation transcript:

EEE 242 Computer Tools for Electrical Engineering Lecture I Mustafa KARABULUT

Contact information Lecturer: Mustafa KARABULUT ▫PhD in Computer Science ▫Technical Sciences Vocational School, Computer Programming Department I am not working at your department, so please save and use my address to contact me about the course Course related material including the presentations will be periodically uploaded to my website: ▫

Course Layout Homeworks / Laboratory hours ▫Attend lab. Hours (Tuesday). The Schedule details will be announced by research assistants. Prerequisites ▫For Matlab:  Basic familiarity with programming  Linear Algebra such as vectors, matrices, equations, etc. ▫For C++:  Basic C++ syntax and commands Textbook ▫Lots of resources on the internet ▫Books :  MATLAB: An Introduction with Applications, Amos Gilat  C++ How To Program (Deitel Publications)

Content Matlab variables, scripts, and operations Visualization and programming Data presentation and analysis approaches Symbolics Simulink, file I/O Building Graphical User Interface (GUI)s Fundamentals of Object Oriented Programming C++ Header Files and Namespace C++ Console I/O (cout and cin) C++ Class and Object (Encapsulation) C++ Function Overloading (Polymorphism). C++ Operator Overloading (Polymorphism) C++ Inheritance C++ Standard Template Library MATLAB C++

Introduction to Matlab WHAT IS MATLAB? ▫The MATLAB® high-performance language for technical computing integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. ▫Numerical computation and simulation tool ▫High level programming language (easier to use)

Overview of the MATLAB environment ▫MATLAB (MATrix LABoratory) ▫MATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation. Using the MATLAB product, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran.

Matlab vs C++ Matlab provides a scripting language and a GUI ▫Matlab code is not compiled (interpreted scripting language) ▫You don’t need to add header files, main function and other stuff while writing your code ▫You don’t need to define variables and declare type ▫You don’t even need to create a file to develop your program ▫Matlab programs are slower than equivalent C++ programs ▫But it is easier and faster to develop programs in Matlab than in C++ ▫Matlab libraries are way larger than those of C++

Using Matlab Matlab provides you two options to run your programs 1.The «interactive command window» to immediately run your commands separately 2.The «m files (*.m)» to create, save and load your programs.

Introduction to Matlab Current Directory Workspace Command History Command Window New m file (Code editor)

Using Matlab help

Script editor

Writing your own code in Matlab You can use either the command window or m file editor. Basic programming structures are valid for Matlab: ▫Variables (Scalar, matrix) ▫Control structures (if, for, etc) ▫Operators (+, -, *, /, etc) ▫Built-in functions (sin, cos, sqrt, pow, etc) ▫Function declaration to define your own functions

Variables You have two different forms of variables ▫Scalar valued (double, int, char) ▫Matrices (nxm matrices, column or row vectors) You don’t need to define variables like you do in C++ Assigning a value to a «name» will create a variable with the type of the value ▫e.g: The command «a=2» will create a variable with the name «a» and type «double»

Naming variables

Scalar valued variables

Numbers MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are e e23 1i j 3e5i All numbers are stored internally using the long format specified by the IEEE® floating-point standard. Floating-point numbers have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10e-308 to 10e30

Matrices and arrays The best way for you to get started with MATLAB is to learn how to handle matrices. You can enter matrices into MATLAB in several different ways: ▫Enter an explicit list of elements. ▫Load matrices from external data files. ▫Generate matrices using built-in functions. ▫Create matrices with your own functions in M-files.

Entering matrices Start by entering a matrix as a list of its elements. You only have to follow a few basic conventions: Separate the elements of a row with blanks or commas. Use a semicolon, ;, to indicate the end of each row. Surround the entire list of elements with square brackets, [ ].

Row vectors

Column vectors

Size and length functions

Entering matrices

Simply type in the Command Window A = [ ; ; ; ] MATLAB displays the matrix you just entered: A = Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can refer to it simply as A. Now that you have A in the workspace.

Concatenation Concatenation is the process of joining small matrices to make bigger ones. In fact, you made your first matrix by concatenating its individual elements. The pair of square brackets, [ ], is the concatenation operator. For an example, start with the 4-by-4 magic square, A, and form B = [A A+32; A+48 A+16] The result is an 8-by-8 matrix, obtained by joining the four submatrices: B =

Addition and substraction

Element-wise Functions

Operators: Element-wise

Operators: Standard

Vector indexing

Matrix indexing

Advanced indexing

Advanced indexing 2

sum,transpose, diag The sum(M) function gives the summary of each column of the given matrix. If M is a vector, then the total of all elements in the vector is returned. sum(A) MATLAB replies with ans = When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of a calculation.

transpose The dot-apostrophe operator (e.g., A.'), transposes without affecting the sign of complex elements. For matrices containing all real elements, the two operators return the same result. Therefore, for such matrices you may use transpose(A) or simply A’ So A‘ produces ans =

diag The main diagonal is obtained with the diag function: diag(A) produces ans = Note: diag(M) returns a matrix if the given input M is a vector. In this matrix, the diagonal is filled with the given vector and all other elements are set to zero.

fliplr The other diagonal, the so-called antidiagonal, is not so important mathematically, so MATLAB does not have a ready-made function for it. But a function originally intended for use in graphics, fliplr, flips a matrix from left to right: diag(fliplr(A)) ans =

The Colon Operator The colon, :, is one of the most important MATLAB operators. It occurs in several different forms. The expression 1:10 is a row vector containing the integers from 1 to 10: To obtain nonunit spacing, specify an increment. For example, 100:-7: :pi/4:pi

The Colon Operator Subscript expressions involving colons refer to portions of a matrix: A(1:k,j) is the first k elements of the jth column of A. Thus: sum(A(1:4,4)) computes the sum of the fourth column.

The Colon Operator The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column. Thus: sum(A(:,end)) computes the sum of the elements in the last column of A: ans = 34

The Colon operator vs linspace Colon operator and the linspace function are similar to each other. Colon operator creates a range of elements using an increment value Linspace creates a range of elements where number of elements is given to the function. Example:

Functions MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is not an error; the appropriate complex result is produced automatically. MATLAB also provides many more advanced mathematical functions, including Bessel and gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type help elfun For a list of more advanced mathematical and matrix functions, type help specfun help elmat

Functions MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. Taking the square root or logarithm of a negative number is not an error; the appropriate complex result is produced automatically. MATLAB also provides many more advanced mathematical functions, including Bessel and gamma functions. Most of these functions accept complex arguments. For a list of the elementary mathematical functions, type help elfun % Help for elementary math functions For a list of more advanced mathematical and matrix functions, type help specfun % Help for specialized math functions help elmat % Help for elementary matrix functions

Functions Some of the functions, like sqrt and sin, are built in. Built-in functions are part of the MATLAB core so they are very efficient, but the computational details are not readily accessible. Other functions, like gamma and sinh, are implemented in M-files. There are some differences between built-in functions and other functions. For example, for built-in functions, you cannot see the code. For other functions, you can see the code and even modify it if you want.

Functions Several special functions provide values of useful constants. pi iimaginary unit, jSame as i eps Floating-point relative precision, realmin Smallest floating-point number, realmax Largest floating-point number, Inf Infinity NaNNot-a-number

Functions Infinity is generated by dividing a nonzero value by zero, or by evaluating well defined mathematical expressions that overflow, i.e., exceed realmax. Not-a-number is generated by trying to evaluate expressions like 0/0 or Inf-Inf that do not have well defined mathematical values. The function names are not reserved. It is possible to overwrite any of them with a new variable, such as eps = 1.e-6 and then use that value in subsequent calculations. The original function can be restored with clear eps

Examples of Expressions rho = (1+sqrt(5))/2 rho = a = abs(3+4i) a = 5 huge = exp(log(realmax)) huge = e+308 toobig = pi*huge toobig = Inf

Working with matrices MATLAB software provides four functions that generate basic matrices. zerosAll zeros onesAll ones randUniformly distributed random elements randn Normally distributed random elements

Automatic initalization of matrices

Working with matrices Here are some examples: Z = zeros(2,4) Z = F = 5*ones(3,3) F = N = fix(10*rand(1,10)) N = R = randn(4,4) R =

Working with matrices N = fix(10*rand(1,10)) N = R = randn(4,4) R =

The load function The load function reads binary files containing matrices generated by earlier MATLAB sessions, or reads text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, outside of MATLAB, create a text file containing these four lines: Save the file as magik.dat in the current directory. The statement load magik.dat reads the file and creates a variable, magik, containing the example matrix. An easy way to read data into MATLAB from many text or binary formats is to use the Import Wizard.

M-files You can create your own matrices using M-files, which are text files containing MATLAB code. Use the MATLAB Editor or another text editor to create a file containing the same statements you would type at the MATLAB command line. Save the file under a name that ends in.m. For example, create a file in the current directory named magik.m containing these five lines: A = [ ]; The statement magik reads the file and creates a variable, A, containing the example matrix.

Deleting Rows and Columns You can delete rows and columns from a matrix using just a pair of square brackets. Start with X = A; Then, to delete the second column of X, use X(:,2) = [ ] This changes X to X =

Deleting Rows and Columns If you delete a single element from a matrix, the result is not a matrix anymore. So, expressions like X(1,2) = [ ] result in an error. However, using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So X(2:2:10) = [ ] results in X =

Whos, clc, clear, clear all whos List current variables clc Clear command window clear Clear variables and functions from memory clear all removes all variables, globals, functions

End of lecture 1 Thank you for listening See you next week