Presentation is loading. Please wait.

Presentation is loading. Please wait.

Electrical and Computer Engineering Department SUNY – New Paltz

Similar presentations


Presentation on theme: "Electrical and Computer Engineering Department SUNY – New Paltz"— Presentation transcript:

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

2 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 ab a ^ b SUNY-New Paltz

3 Operators, Expressions, Statements (Exercises)
Write MATLAB statements to evaluate: (3.01 – 2.55)x(.123/.001)/( ) (1+ .09)10 sin(15º) ( ) 2 / ( ) 3 SUNY-New Paltz

4 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

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

6 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]; * a = ? b = [3 2 2]; a .^ 2 = ? a .* b = ? a ./ b = ? SUNY-New Paltz

7 Arithmetic operations on arrays (Exercises)
Form a vector that contains the squares of numbers between 1 and 5. Form a vector that contains the cubes of numbers between -10 and 10. SUNY-New Paltz

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

9 Exercise Execute the following program from Command Window:
t = 0: 0.1: 2*pi; x = cos(t); y = sin(t); plot(x,y); Run the same program from a “.m” file. Save your file as “myex1.m” SUNY-New Paltz

10 Vectorization of Formulae 1- Use a calculator
Initial deposit= 750; Interest rate = 0.09; Calculate account value for 3 years based on compound interest: End of the first year: End of the second year: End of the third year: *.09 = 817.5 *.09 = *.09 = ( *.09) + ( *.09)*.9 = ( *.09)*( ) = 750 * ( )2 *.09 = 750 * ( )2 SUNY-New Paltz

11 Vectorization of Formulae 2- Case of Scalar
Write a sequence of MATLAB statements to find and display the account value after 10 years. Initialization A = 750; r = 0.09; n = 10; B = A * (1 + r) ^ n; User Inetrface (I/O) disp( [A B] ) SUNY-New Paltz

12 Vectorization of Formulae 3- Case of Multiple Investment
Write a sequence of MATLAB statements to find the account value after 10 years for 5 different deposits: A = [ ]; r = 0.09; n = 10; B = A .* (1 + r) ^ n; disp( [A’ B’] ) SUNY-New Paltz SUNY-New Paltz

13 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

14 Output /Exercise Write a MATLAB program (‘mysum.m’) that receives the length of 2 the sides (e.g. 3 and 4) and prints out the length of its hypotenue as ‘Hypotenuse = 5’ clear all clc a = input(‘Enter Length of Side 1: ’); SUNY-New Paltz

15 Types of Variables Fixed Point (int): Floating Point (float):
Unsigned integer (0, 1, 2, …) Integer ( …, -3, -2, -1, 0, 1, 2, 3, …) Floating Point (float): Single ( example: ) Double (example: ) SUNY-New Paltz

16 format 1234567890 is displayed as 1.2346e+009
mantissa is between 1 and format short - 4 digits after the decimal point. For example, 3.1416 format short e - Floating-point format, with 4 digits after the decimal point. For example, 3.1416e+00 format long - Scaled fixed-point format with 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example,  format long e - Floating-point format, with 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, e+00. format long g - Fixed- or floating-point, whichever is more readable, with 14 to 15 digits after the decimal point for double; and 6 or 7 digits after the decimal point for single. For example,  format short g - Fixed- or floating-point, whichever is more readable, with 4 digits after the decimal point. For example,  format compact, format hex, format rat, format bank SUNY-New Paltz

17 Format (exercise) Put MATLAB in each of the following format and display the number and .3 format short format short e format long g – format short g – format rat Format bank SUNY-New Paltz


Download ppt "Electrical and Computer Engineering Department SUNY – New Paltz"

Similar presentations


Ads by Google