Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”

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 M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
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 7 for Engineers
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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 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.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
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.
Essential MATLAB® for Engineers and Scientists
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
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.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
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.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
INTRODUCTION TO MATLAB LAB# 01
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
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 &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Matlab for Engineers Matlab Environment Chapter 2.
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.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
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
Lecture: MATLAB Chapter 1 Introduction
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
INTRODUCTION TO MATLAB
Experiment No. (1) - an introduction to MATLAB
Matlab Basics.
CS100A Lecture 21 Previous Lecture This Lecture
Computer Simulation Lab
Electrical and Computer Engineering Department SUNY – New Paltz
Introduction to Matlab
Chapter 2 MATLAB Environment
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”

MATLAB fundamentals  Desktop, Editing  V ariables, Operators, Expressions  V ectors  I nput / Output  R epetition  D ecision SUNY-New Paltz

The MATLAB Desktop SUNY-New Paltz

Integrated Development Environment (IDE)  Command Window  Current Directory  Command History  Launch Pad  Workspace SUNY-New Paltz

Program s A collection of statements to solve a problem is called a program. Different Types of Languages: –Machine Language (Executable File) – Low-Level Languages (Assembly) –High-Level Languages (C, C++, Java, Basic, Fortran, etc.) –Script Languages ( PERL, MATLAB, PHP, etc.) SUNY-New Paltz

A Simple Program: Suppose you have $1000 saved in the bank. Interest is compounded at the rate of 9% per year. What will your bank balance be after one year? SUNY-New Paltz 1.Get the data (initial balance and interest rate) into the program. 2.Calculate the interest (9 per cent of $1000, i.e. $90). 3.Add the interest to the balance ($90 + $1000, i.e. $1090) 4.Display the new balance

C Language include define int interest, balance main{ balance = 1000; rate = 0.09; interest = rate * balance; balance = balance + interest; printf(“balance=%d”, balance); } SUNY-New Paltz

MATLAB Program balance = 1000; rate = 0.09; interest = rate * balance; balance = balance + interest; disp( ’New balance:’ ); disp( balance ); SUNY-New Paltz

Variables and the workspace A variable name (like balance) must comply with the following two rules: 1. It may consist only of the letters a–z, the digits 0–9 and the underscore ( _ ). 2. It must start with a letter. Valid: r2d2, pay_day Invalid : pay-day, 2a name$, _2a SUNY-New Paltz

Case sensitivity Different variables: – balance, BALANCE and BaLance Camel Caps: – camelCaps – milleniumBug – dayOfTheWeek Commands are all in lower case SUNY-New Paltz

Examples of variables interest=.09; years=10; delta=1.e-3; vect=[ ]; matx=[1 2 3; 4 5 6]; SUNY-New Paltz

workspace who: lists the names of all the variables in your workspace whos: lists the size of each variable as well ans: returns the value of the last expression evaluated but not assigned to a variable Workspace –Name Size Bytes Class –balance 1x1 8 double array –interest 1x1 8 double array –rate 1x1 8 double array SUNY-New Paltz

Arrays: vectors and matrices Initializing vectors: explicit lists x = [ ] use spaces a = [5,6,7] use commas a = [1 2 3], b = [4 5], c = [a -b] x = [ ] Initializing vectors: the colon operator x = 1:10 x = 1:0.5:4 x = 10:-1:1 x = 0:-2:-5 linspace –linspace(0, pi/2, 10) SUNY-New Paltz

Array Subscripts r = rand(1,7) This gives you a row vector of seven random numbers. r(3) This will display the third element of r. The number 3 is the subscript. r(2:4) This should give you the second, third and fourth elements. r(1:2:7) r([ ]) r([1 7 2]) = [ ] will remove elements 1, 7 and 2. SUNY-New Paltz

Matrices a = [1 2 3; 4 5 6] = a’ = A matrix can be constructed from column vectors of the same lengths: x = 0:30:180; table = [x’ sin(x*pi/180)’] = SUNY-New Paltz

Exercise Construct the following vectors and matrices: 1.A row vector of all odd numbers between –101 and A column vector of all numbers between –101 and 101 that are divisible by 3 3.A matrix of 16 equally spaced angles between 0 and 2*pi along with sin(x) and cos(x) and tan(x). 4.A matrix of size 5x5 having random numbers between 0 and 1 for each element. SUNY-New Paltz

Capturing output diary filename diary off SUNY-New Paltz

Operators, Expressions, Statements Arithmetic operations between two scalars Operation Algebraic form MATLAB Addition a + b a + b Subtraction a - b a - b Multiplication a × b a * b Right division a/b a / b Left division b/a a \ b Power a b a ^ b SUNY-New Paltz

Precedence of arithmetic operations Precedence Operator 1 Parentheses 2 Power, left to right 3 Multiplication and division, left to right 4 Addition and subtraction, left to right SUNY-New Paltz

Exercise Evaluate the following arithmetic expressions: A= 2/4^2*2+1\4 B=1/2^2*2/4 SUNY-New Paltz

Arithmetic operations on arrays Arithmetic operators that operate element-by-element on arrays Operator Description.* Multiplication./ Right division.\ Left division.^ Power Examples: a = [2 4 8]; 3.* a = ? b = [3 2 2];a.^ 2 = ? a.* b = ? a./ b = ? SUNY-New Paltz

Statements, Commands and Functions Statements: s = u * t - g / 2 * t.^ 2; Functions: sin(x), plot (x) Commands: load, save, clear SUNY-New Paltz

Vectorization of Formulae 1- Case of Scalar A = 750; r = 0.09; n = 10; B = A * (1 + r) ^ n; disp( [A B] ) SUNY-New Paltz

Vectorization of Formulae 2- Case of Mutiple Investment SUNY-New Paltz A = [ ]; r = 0.09; n = 10; B = A * (1 + r) ^ n; disp( [A’ B’] )

Output disp( variable ) {variable could be numerical or textual} disp( ’Pilate said, ’’What is truth?’’’ ); disp( [x y z] ) disp( [’The answer is ’, num2str(x)] ); SUNY-New Paltz

format is displayed as e+009 mantissa is between 1 and format short, format short e format long, format long e format long g, format short g format compact format hex format rat format bank SUNY-New Paltz

Repeating with for for i = 1:5 disp(i) end for index = j:m:k for index = v (where v is any vector such as [2 3 8]) Show the squares of all even numbers between 0 and 1000 SUNY-New Paltz for i=0:2:1000 disp([i i*i]) end

Avoid Loops! s = 0; for n = 1: s = s + n; end; n = 1:100000; s = sum( n ); SUNY-New Paltz Find x=1+1/2+1/3+1/4…. + 1/1000

Decisions if r > 0.5 disp( ’greater indeed’ ) end Relational operators Relational Operator Meaning < less than <= less than or equal == equal ~= not equal > greater than >= greater than or equal SUNY-New Paltz

Decisions SUNY-New Paltz if condition statementsA else statementsB end if condition1 statementsA elseif condition2 statementsB elseif condition3 statementsC... else statementsE end

Logical operators ‘~’ ‘&’ ‘ |’ if bal >= 5000 & bal < SUNY-New Paltz

Nested if’s d = b^2 - 4*a*c; if a ~= 0 if d < 0 disp( ’Complex roots’ ) else x1 = (-b + sqrt( d )) / (2*a); x2 = (-b - sqrt( d )) / (2*a); end SUNY-New Paltz

Switch/Case d = floor(3*rand) + 1 switch d case 1 disp( ’That’’s a 1!’ ); case 2 disp( ’That’’s a 2!’ ); otherwise disp( ’Must be 3!’ ); end SUNY-New Paltz

Switch/Case d = floor(10*rand); switch d case {2, 4, 6, 8} disp( ’Even’ ); case {1, 3, 5, 7, 9} disp( ’Odd’ ); otherwise disp( ’Zero’ ); end SUNY-New Paltz

Complex numbers z = 2 + 3*i sqrt(2 + 3*i) exp(i*pi) circle = exp( 2*i*[1:360]*pi/360 ); % select all points around a circle plot(circle) % is equivalent to: plot(real(y), imag(y)) axis equal a = [1+i, 2+2i; 3+3i, 4+4i] SUNY-New Paltz i i i i