350 likes | 462 Views
This lecture from the Electrical and Computer Engineering Department at SUNY New Paltz covers the fundamentals of MATLAB programming. Topics include the MATLAB Integrated Development Environment (IDE), the use of variables, operators, expressions, and arrays. Students will learn to write simple programs, manage workspaces, and utilize loops and decision-making structures in MATLAB. The practical exercises include calculating bank interest and creating vectors and matrices, aimed at enhancing understanding of arrays and arithmetic operations within MATLAB.
E N D
Computer Simulation Lab “Lecture 2” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
MATLAB fundamentals · Desktop, Editing · Variables, Operators, Expressions · Vectors · Input / Output · Repetition · Decision SUNY-New Paltz
The MATLAB Desktop SUNY-New Paltz
Integrated Development Environment (IDE) qCommand Window qCurrent Directory qCommand History qLaunch Pad qWorkspace SUNY-New Paltz
Programs 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
Write a program! 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? • 1. Get the data (initial balance and interest rate) into the program. • 2. Calculate the interest (9 per cent of $1000, i.e. $90). • Add the interest to the balance ($90 + $1000, i.e. $1090) • 4. Display the new balance SUNY-New Paltz
C Language include <stdio.h> include <interest.h> 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; • email=‘doej@gmail.com’; • vect=[1 2 3 4 5]; • 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 = [1 3 0 -1 5] 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
ArraySubscripts • 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([1 7 2 6]) • r([1 7 2]) = [ ] will remove elements 1, 7 and 2. SUNY-New Paltz
Matrices • a = [1 2 3; 4 5 6] = 1 2 3 4 5 6 • a’ = 1 4 2 5 3 6 • A matrix can be constructed from column vectors of the same lengths: x = 0:30:180; table = [x’ sin(x*pi/180)’] = 0.0000 0.0000 30.0000 0.5000 60.0000 0.8660 90.0000 1.0000 120.0000 0.8660 150.0000 0.5000 180.0000 0.0000 SUNY-New Paltz
Exercise Construct the following vectors and matrices: • A row vector of all odd numbers between –101 and 101 • A column vector of all numbers between –101 and 101 that are divisible by 3 • A matrix of 16 equally spaced angles between 0 and 2*pi along with sin(x) and cos(x) and tan(x). • 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 • OperationAlgebraic formMATLAB • 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 aba ^ b SUNY-New Paltz
Precedence of arithmetic operations PrecedenceOperator 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 Formulae1- Case of Scalar A = 750; r = 0.09; n = 10; B = A * (1 + r) ^ n; disp( [A B] ) • 750.00 1775.52 SUNY-New Paltz
Vectorization of Formulae2- Case of Mutiple Investment • A = [750 1000 3000 5000 11999]; • r = 0.09; • n = 10; • B = A * (1 + r) ^ n; • disp( [A’ B’] ) • 750.00 1775.52 • 1000.00 2367.36 • 3000.00 7102.09 • 5000.00 11836.82 • 11999.00 28406.00 SUNY-New Paltz SUNY-New Paltz
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 • 1234567890 is displayed as 1.2346e+009 • mantissa is between 1 and 9.9999 • 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 for i=0:2:1000 disp([i i*i]) end SUNY-New Paltz
Avoid Loops! s = 0; for n = 1:100000 s = s + n; end; n = 1:100000; s = sum( n ); Find x=1+1/2+1/3+1/4…. + 1/1000 SUNY-New Paltz
Decisions if r > 0.5 disp( ’greater indeed’ ) end Relational operators Relational OperatorMeaning < less than <= less than or equal == equal ~= not equal > greater than >= greater than or equal SUNY-New Paltz
Decisions if condition1 statementsA elseif condition2 statementsB elseif condition3 statementsC ... else statementsE end if condition statementsA else statementsB end SUNY-New Paltz
Logical operators ‘~’ ‘&’ ‘ |’ if bal >= 5000 & bal < 10000 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 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] 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i SUNY-New Paltz