Chapter 2 MATLAB Basics

Size: px
Start display at page:

Download "Chapter 2 MATLAB Basics"

Transcription

1 EGR115 Introduction to Computing for Engineers MATLAB Basics from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed Cengage Learning Topics 2.1 Variables & Arrays 2.2 Creating & Initializing Variables in MATLAB 2.3 Multidimensional Arrays 2.4 Subarrays 2.5 Special Values 2.6 Displaying Output Data 2.7 Data Files 2.8 Scalar & Array Operations 2.9 Hierarchy of Operations 2.10 Built-in MATLAB Functions 2.11 Introduction to Plotting 2.12 Examples 2.13 Debugging MATLAB Programs

2 Slide 1 of Variables & Arrays m 1 m (m, Variables & Arrays The fundamental unit of data (variable) in MATLAB is the ARRAY: a collection of data values organized into rows (m) & columns (n) with a given single name. Scalar: 1 1 array (single value). Vector: 1-D array (either row vector or column vector). The MATLAB default is the row vector, but typically the column vector is used in mathematics (calculus): a TRNSPOSE ( ) of matrix (transforming row vector => column vector) is often necessary. Matrix: 2-D array with the size of m n (rows columns). The actual value stored within the matrix can be specified at the location of (row, column). Multidimensional array (3-D/4-D): you can define 3-D (even 4-D) arrays in MATLAB. User-Defined Variable Names MATLAB standard user-defined variables: use all lower case with underscores between words: (i.e., example_1_1_sin_x.m) The first 63 characters of user-defined variable name is significant: make sure that your variable names are unique in the first 63 characters (else, MATLAB will not be able to tell the difference between them).

3 Slide 2 of Variables & Arrays MATLAB Variable Assignment Examples: Creating (Initializing) Variables Usually, the very first programming instruction in any MATLAB session (or code) is to create (initialize) user-defined variables: Creating (new) variables: for a new MATLAB session. Initializing variables: when you run different scripts (multiple MATLAB m-files), you want to clear your workspace (initialize variables) for each different script run (unless you want to share same variables between them). 3 ways to create (initialize) MATLAB variables: (i) Assign data to the variable in an assignment statement (ii) Input data into the variable from the keyboard (iii) Read data from a file MATLAB Variables: double & char double: a variable class, consisting of scalars or arrays of 64-bit double-precision floating-point numbers (can be real, imaginary, or complex numbers). char: a variable class, consisting of scalars or arrays of 16-bit values, each representing a single character. Arrays of this type are used to hold character strings.

4 2.2 Creating & Initializing Variables in MATLAB Slide 3 of 27 (a) The simplest way to initialize a variable is to assign it one or more values in an assignment statement. var = expression ; EXAMPLE 2-1 (NOTE) The semicolon (;) means suppression of the automatic echoing of assigned values in the given statement. (b) Initializing the variables by keyboard inputs: var = input( Enter an input value: ) ; (c) Initializing the variables using shortcut expressions: [first:(incr):last] ( ) or [first:last ] ( ) (incr=1) (NOTE) The (optional) apostrophe ( ) means transpose operation (swapping the rows and columns of any array. (a) Assignment statement create_var_a.m Creating (new) variables Demo var1 = 10.5 var2 = 4i var3 = i var4 = 20; comment = This is a character string >> create_var_a var1 = var2 = 0 + 4i var3 = i comment = This is a character string (b) Keyboard inputs create_var_b.m Creating (new) variables Demo var = input( Enter an input value: ); var >> create_var_b Enter an input value: (c) Shortcut expressions >> [2:2:10] ans = >> a = [0 1+7] a = 0 8 >> [2:1:5] ans = >> b = [a(2) 7 a] b = >> [2:2:6] ans = >> e = [1 2 3; 4 5] error!

5 2.2 Creating & Initializing Variables in MATLAB Slide 4 of 27 EXAMPLE 2-1 (continued) (d) Initializing the variables using MATLAB built-in Functions. >> a = zeros(2) a = >> b = zeros(2,3) b = >> c = eye(3) c = >> length(c) ans = 3 >> size(c) ans = 3 3 >> d = ones(3) d = >> e = ones(3,2) >> f = 1:4 f = >> g = [f f ] g = Input Function The input function includes the character ( s ) argument option: >> in1 = input( Enter data: ); Enter data: 1.23 >> in2 = input( Enter data:, s ); Enter data: 1.23 in1 is double 1.23 in2 is char 1.23

6 2.2 Creating & Initializing Variables in MATLAB Slide 5 of 27 Do-It-Yourself (DIY) EXERCISE 2-1 Answer the followings: (a) What is the difference between an array, a matrix, and a vector? (b) Let: c = (b-1) What is the size of c? (b-2) What is the value of c(2,3)? (b-3) List the subscripts of all elements containing the value 0.6. (a) In MATLAB, an array is a collection of data values organized into rows (m) & columns (n) with a given single name. The array is the fundamental unit of data in any MATLAB program. A MATLAB array can be categorized into vector (1-D array) or matrix (2-D array). (b) exercise_2_1b.m exercise_2_1b.m >> exercise_2_1b c = c = [ ; ; ] b_1 = 3 4 b_1 = size(c) b_2 = c(2,3) b_2 = c_1_1 = c(1,1) c_1_1 = c_1_2 = c(1,2) c_1_2 = c_1_3 = c(1,3) c_1_3 = c_1_4 = c(1,4) c_1_4 = c_2_1 = c(2,1) c_2_1 = c_2_2 = c(2,2) c_2_2 = c_2_3 = c(2,3) c_2_3 = c_2_4 = c(2,4) c_2_4 = ) c_3_1 = c(3,1) c_3_1 = c_3_2 = c(3,2) c_3_2 = c_3_3 = c(3,3) c_3_3 = c_3_4 = c(3,4) c_3_4 = 0 c(1,4), c(2,1), and c(3,2) are the elements containing the value of 0.6

7 2.2 Creating & Initializing Variables in MATLAB Slide 6 of 27 Do-It-Yourself (DIY) EXERCISE 2-2 Determine the size of the following arrays, using MATLAB. Check your answers by entering the arrays into MATLAB and using whos command (or checking the Workspace Browser). Note that the later arrays may depend on the definitions of arrays defined earlier in this exercise. (a) u = [10 20*i 10+20]; (b) v = [-1; 20; 3]; (c) w = [1 0-9; 2-2 0; 1 2 3]; (d) x = [u v]; (e) y(3,3) = -7; (f) z = [zeros(4,1) ones(4,1) zeros(1,4) ]; (g) v(4) = x(2,1); exercise_2_2_var.m

8 2.2 Creating & Initializing Variables in MATLAB Slide 7 of 27 Do-It-Yourself (DIY) EXERCISE 2-2 (continued) exercise_2_2_var.m u = [10 20*i 10+20]; v = [-1; 20; 3]; w = [1 0-9; 2-2 0; 1 2 3]; x = [u v]; y(3,3) = -7; z = [zeros(4,1) ones(4,1) zeros(1,4) ]; v(4) = x(2,1); Create a MATLAB script (M-File: exercise_2_2_var.m) and execute a series of commands above. Answer the following questions: (a) What is the value of w(2,1)? (b) What is the value of x(2,1)? (c) What is the value of y(2,1)? (d) What is the value of v(3)? exercise_2_2_var.m exercise_2_2_var.m >> exercise_2_2_var u = [10 20*i 10+20]; w_2_1 = 2 v = [-1; 20; 3]; w = [1 0-9; 2-2 0; 1 2 3]; x_2_1 = 0 20i x = [u v]; y(3,3) = -7; y_2_1 = 0 z = [zeros(4,1) ones(4,1) zeros(1,4) ]; v_3 = 3 v(4) = x(2,1); w_2_1 = w(2,1) x_2_1 = x(2,1) y_2_1 = y(2,1) v_3 = v(3)

9 Slide 8 of /2.4 Multi-Dimensional Arrays/Subarrays Multi-Dimensional Arrays: Subarrays: Multi-Dimensional Arrays MATLAB allows as many dimensions as necessary for any given problem. The multi-dimensional arrays have one subscript for each dimension, and an individual element is selected by specifying a value for each subscript. create_md_array.m Create 3-D Array Demo c(:,:,1) = [1 2 3; 4 5 6]; c(:,:,2) = [7 8 9; ]; whos c c >> create_md_array Name Size Bytes Class Attributes c 2x3x2 96 double c(:,:,1) = c(:,:,2) = A colon (:) can be used (instead of an integer subscript) to represent ALL of the elements within a row or a column of a given array. Subarrays It is possible to select and use subsets of MATLAB arrays as though they were separate arrays (subarrays). To select a portion of an array, just include the list of the elements to be selected in the parentheses after the array name.

10 Slide 9 of Special Values MATLAB Special Values Type clock & date in command window... Subarrays (Example) create_subarray.m Create Subarray Demo arr1 = [ ]; arr1a = arr1(3) arr1b = arr1([1 4]) arr1c = arr1(1:2:5) arr2 = [1 2 3; ; 3 4 5]; arr2a = arr2(1,:) arr2b = arr2(:,1:2:3) arr3 = [ ]; arr3a = arr3(5:end) arr3b = arr3(end) arr4 = [ ; ; ]; arr4a = arr4(2:end, 2:end) arr4 arr4(1:2,[1 4]) = [20 21; 22 23] >> create_subarray arr1a = arr1b = arr1c = arr2a = arr2b = arr3a = arr3b = 8 arr4a = arr4 = arr4 = The last command: the elements: arr4(1,1) arr4(1,4) arr4(2,1), and arr4(2,4) were updated with new values. In order to make this command legal, the array shape (numbers of rows & columns) MUST be the same on both sides of the equal sign (else, MATLAB gives an error).

11 Slide 10 of Special Values Do-It-Yourself (DIY) EXERCISE 2-3 Determine the contents of the following subarrays: 1. Let: c = (a) c(2,:) (b) c(:,end) (c) c(1:2,2:end) (d) c(6) (e) c(4:end) (f) c(1:2,2:4) (g) c([1 3],2) (h) c([2 2],[3 3]) exercise_2_3_1.m >> exercise_2_3_1 exercise_2_3_1.m c_a = c = [ ; ; c_b = ]; c_a = c(2,:) c_c = c_b = c(:,end) c_c = c(1:2,2:end) c_d = c_e = c_d = c(6) c_e = c(4:end) c_f = c_f = c(1:2,2:4) c_g = c_g = c([1 3],2) c_h = c([2 2],[3 3]) c_h = (d): c(6) = c(3,2) WHY??? Since MATLAB (in fact) stores data in column major order, one can (actually) treat a multidimensional array as though it were a 1-D array whose length is equal to the number of elements in the multidimensional array... (NOTE) under normal circumstance, you should never use this feature of MATLAB!

12 Slide 11 of Special Values Do-It-Yourself (DIY) EXERCISE 2-3 (continued) 2. Determine the contents of array a after the following statements are executed : (a) a = [1 2 3; 4 5 6; 7 8 9]; a([3 1],:) = a([1 3],:); (b) a = [1 2 3; 4 5 6; 7 8 9]; a([1 3],:) = a([2 2],:); (c) a = [1 2 3; 4 5 6; 7 8 9]; a = a([2 2],:); 3. Determine the contents of array a after the following statements are executed : (a) (b) (c) a = eye(3,3); b = [1 2 3]; a(2,:) = b; a = eye(3,3); b = [4 5 6]; a(:,3) = b ; a = eye(3,3); b = [7 8 9]; a(3,:) = b([3 1 2]); exercise_2_3_2.m / exercise_2_3_3.m exercise_2_3_2.m a = [1 2 3; 4 5 6; 7 8 9]; a([3 1],:) = a([1 3],:); a_a = a a = [1 2 3; 4 5 6; 7 8 9]; a([1 3],:) = a([2 2],:); a_b = a a = [1 2 3; 4 5 6; 7 8 9]; a_c = a([2 2],:) >> exercise_2_3_2 a_a = a_b = a_b = exercise_2_3_3.m >> exercise_2_3_3 a_a = a = eye(3,3); b = [1 2 3]; a(2,:) = b; a_a = a a_b = a = eye(3,3); b = [4 5 6]; a(:,3) = b'; a_c = a_b = a a = eye(3,3); b = [7 8 9]; a(3,:) = b([3 1 2]); a_c = a

13 Slide 12 of Displaying Output Data MATLAB Output Display: format and fprintf MATLAB Default Format When data is echoed in the command window, integers, characters (strings), and other numerical values (real/imaginary) are displayed in default format. A user can change this default. The disp & fprintf Functions output_demo_a.m str = ['The value of pi = ' num2str(pi)]; disp(str); fprintf('the value of pi is f \n',pi); fprintf('the value of pi is 6.2f \n',pi); >> output_demo_a The value of pi = The value of pi is The value of pi is 3.14 d Display value as integer e Display value in exponential format f Display value in floating point format g Display value in either floating point or exponential format (whichever is shorter) \n Skip to a new line (same as: )

14 Slide 13 of Data Files MATLAB Data File Handling: save and load I/O The Limitation of fprintf The fprintf function only displays the real part of a complex value (imaginary part is ignored). output_demo_b.m x = 2 * (1-2i)^3; str = ['disp: x = ' num2str(x)]; disp(str); fprintf('fprintf: x = 8.4f \n',x); >> output_demo_b disp: x = -22+4i fprintf: x = The MATLAB Data Handling The save command saves data from the current MATLAB workspace into a disk file: save filename var1 var2 var3 The load command is opposite of the save command (loads data from a disk file into the current MATLAB workspace): load filename >> x = [1 2 3; 4 5 6] x = e e e e e e+000 x.dat save x.dat x -ascii y.dat >> load y.dat >> y y = MATLAB can load comma or space separated ASCII format files from other programs (typically the file extension.dat is given).

15 Slide 14 of Data Files Do-It-Yourself (DIY) EXERCISE 2-4 Answer the followings: 1. How would you tell MATLAB to display all real values in exponential format with 15 significant digits? 2. What do the following sets of statements do? What is the output from them? (a) (b) radius = input( Enter circle radius:\n ); area = pi * radius^2; str = [ The area is num2str(area)]; disp(str); value = int2str(pi); disp([ The value is value! ]); 3. What do the following sets of statements do? What is the output from them? value = e2; fprintf( value = e\n,value); fprintf( value = f\n,value); fprintf( value = g\n,value); fprintf( value = 12.4f\n,value); 1. format long e will display all real values in exponential format with 15 significant digits. 2. exercise_2_4_2.m exercise_2_4_2.m >> exercise_2_4_2 (a) radius = input('enter circle radius:\n'); Enter circle radius: area = pi*radius^2; 25 str = ['The area is ' num2str(area)]; The area is disp(str); disp(' '); Hit Enter to continue... disp('hit Enter to continue...'); disp(' '); The value is 3! pause (b) value = int2str(pi); disp(['the value is ' value '!']); 3. exercise_2_4_3.m exercise_2_4_3.m value = e2; fprintf('value = e\n',value); fprintf('value = f\n',value); fprintf('value = g\n',value); fprintf('value = 12.4f\n',value); >> exercise_2_4_3 value = e+04 value = value = value =

16 Slide 15 of Scalar & Array Operations MATLAB Array & Matrix Operations MATLAB Calculations (Operations) MATLAB performs calculations (like a calculator) with an assignment statement: variable_name = expression; (this is NOT math: store the value of expression into the location variable_name) MATLAB Arithmetic Operators (for Scalars) Addition (a b): Subtraction (a b): Multiplication (a b): Division (a b): Exponentiation (a b ): a + b a - b a * b a / b a ^ b MATLAB Array & Matrix Operators (for Vectors & Matrices) Array operations: operations performed between arrays on an element-by-element basis. Matrix operations: follow the normal rules of linear algebra (if you don t know what it means... see this: (NOTE: these are fundamentally very different mathematical operations: be very careful)

17 Slide 16 of Scalar & Array Operations Assume that the following variables are defined as: 1 0 a 2 1 What is the result of each of the following expressions? (a)a + b (b)a.* b (c)a * b (d)a * c (e)a + c (f)a + d (g)a.* d (h)a * d 1 2 b 0 1 EXAMPLE c 2 d 5 example_2_2.m example_2_2.m a = [1 0; 2 1]; b = [-1 2; 0 1]; c = [3 2]'; d = 5; ans_a = a + b ans_b = a.* b ans_c = a * b ans_d = a * c ans_e = a + c <= CANNOT DO THIS! >> example_2_2 ans_a = ans_b = ans_c = ans_d = 3 8 ans_f = a + d ans_g = a.* d ans_h = a * d ans_f = ans_g = ans_h =

18 Slide 17 of Hierarchy of Operations MATLAB Hierarchy of Operations MATLAB Operations (Hierarchy) Many arithmetic operations are combined into a single expression. In order to accurately represent mathematical expressions in MATLAB assignment statement, it is very important to exactly follow the hierarchy of operations. MATLAB Operations (Hierarchy) Basics distance = 0.5 * accel * time ^ 2 (operation1) = (time ^ 2) (operation2) = (0.5 * accel) (operation3) = (operation2) * (operation1) distance = (0.5 * accel * time) ^ 2 => This will produce the different result!!! (operation1) = (0.5 * accel * time) (operation2) = (operation1) ^ 2 distance = 0.5 * accel * (time ^ 2) => This will produce the same result (parentheses NOT required, but recommended: makes the order of operation crystal clear and can avoid confusion for the operation hierarchy)

19 Slide 18 of Hierarchy of Operations Variables a, b, c, and d have been initialized to the following vales: a = 3; b = 2; c = 5; d = 3; Evaluate the following MATLAB assignment statements, showing the details of hierarchy of operations. (a)output = a*b+c*d; (b)output = a*(b+c)*d; (c)output = (a*b)+(c*d); (d)output = a^b^d; (e)output = a^(b^d); EXAMPLE 2-3 MATLAB Operations (Hierarchy): Some Important Professional Practices It is extremely important that every expression in a program be made as clear as possible. Ask yourself a series of questions: Can I easily understand this expression if I come back to it in 6 month? Can another programmer look at my code and instantly understand what my instructions are? If you are not sure, use extra parentheses in the expression to make it sure. Alternatively, separate your expression into a few parts (avoid using extremely long mathematical expression in a single line). (a) output = a*b+c*d = 3*2+5*3 = (3 * 2) + (5 * 3) = 6 + (5 * 3) = = 21 (b) output = a*(b+c)*d = 3*(2+5)*3 = 3 * (2 + 5) * 3 = 3 * 7 * 3 = 21 * 3 = 63 (c) output = (a*b)+(c*d) = (3*2)+(5*3) = (3 * 2) + (5 * 3) = 6 + (5 * 3) = = 21 (d) output = a^b^d = 3^2^3 = (3 ^ 2) ^ 3 = 9 ^ 3 = 729 (e) output = a^(b^d) = 3^(2^3) = 3 ^ (2 ^ 3) = 3 ^ 8 = 6561

20 Slide 19 of Hierarchy of Operations Do-It-Yourself (DIY) EXERCISE 2-5 Assume that the following variables are defined as: 2 1 a b 3 1 Evaluate the following MATLAB assignment statements, showing the details of hierarchy of operations. (a) result = a.* c; (b) result = a * [c c]; (c) result = a.* [c c]; (d) result = a + b * c; (e) result = a + b.* c; 1 c 2 d 3 exercise_2_5.m exercise_2_5.m >> exercise_2_5 a = [2 1; -1 2]; result_b = b = [0-1; 3 1]; 4 4 c = [1 2]'; 3 3 d = -3; result_c = result_a = a.* c <= CANNOT DO THIS! result_b = a * [c c] result_c = a.* [c c] result_d = a + b * c <= CANNOT DO THIS! result_e = a + b.* c <= CANNOT DO THIS!

21 Slide 20 of Hierarchy of Operations Do-It-Yourself (DIY) EXERCISE 2-6 Solve for x in the equation Ax = B, where: A B Understand that you are solving a set of simultaneous linear equations: a x a x a x b a x a x a x b a x a x a x b a11 a12 a13 b1 x1 Ax B A a21 a22 a 23 B b x x 2 2 a 31 a32 a33 b 3 x 3 This can be solved by a multiplication of Matrix Inverse of A and B, as: 1 x A B In MATLAB, the Matrix Left Division will do the job... A \ B <= which is defined as: inv(a) * B exercise_2_6.m exercise_2_6.m >> exercise_2_6 Simultaneous Linear Equations Solver x = A = [1 2 1; 2 3 2; ]; B = [1 1 0]'; x = A \ B; x x1 = x2 = x = inv(a)*b x3 = x x1 = x(1) x2 = x(2) x3 = x(3)

22 Slide 21 of Built-in MATLAB Function MATLAB Built-in Functions MATLAB Functions One of the greatest strength of MATLAB is it comes with an incredible (very comprehensive) library of predefined built-in functions. (for example) Trigonometry Functions include... LIST GOES ON...

23 Slide 22 of Introduction to Plotting MATLAB Plotting Another great feature of MATLAB is its very powerful plotting capability. Plotting is extremely easy in MATLAB, yet MATLAB offers plenty of freedom for customizing the plot. A brif summary of MATLAB plotting includes: Simple xy plots to 3-D plots Manipulating the plot figure in a Figure Window: rotate, pan, crop, zoom, etc. Exporting and printing the plots as graphical images Multiple plots within a single figure Customize axis, line colors, line style, marker style, and legends Logarithmic scales and more... (It is most likely the best to learn MATLAB plotting by actually using it... try plotting something and customize the plot until it becomes exactly as you want it to be. MATLAB allows you great freedom and has enough capability to custom design your own plot, in order to meet your problem solution needs.)

24 Slide 23 of Introduction to Plotting Do-It-Yourself (DIY) EXERCISE 2-7 Starting from the Example 1-1 (plot of sin(x) function), modify this plot as shown below: Modify: line color/style and marker style Add: title, axis labels, and grid exercise_2_7_sin_x.m exercise_2_7_sin_x.m Based on: example_1_1_sin_x.m clc close all clear all x = 0:0.1:6; y = sin(x); plot(x,y,'k-',x,y,'bo'); title('plot of f(x)=sin(x)'); xlabel('x'); ylabel('sin(x)'); grid on axis equal

25 Slide 24 of Introduction to Plotting Do-It-Yourself (DIY) EXERCISE 2-8 Starting from the Example 1-2 (plot of sin(x) and cos(x) function), modify this plot as shown below: Modify: line color/style Add: title, axis labels, legend, and grid exercise_2_8_cos_x.m exercise_2_8_cos_x.m Based on: example_1_2_cos_x.m clc clear all close all x = 0:0.1:6; y1 = sin(x); y2 = cos(x); plot(x,y1,'b-',x,y2,'r--'); title('plot of sin(x) and cos(x)'); xlabel('x'); ylabel('f(x)'); legend ('f(x)=sin(x)','f(x)=cos(x)') grid on axis equal

26 Slide 25 of Examples EXAMPLE 2-4 Design a MATLAB program that reads an input temperature in degrees Fahrenheit, converts it to an absolute temperature in kelvin, and writes out the result. The relationship between temperature in degrees Fahrenheit (F) and temperature in kelvins (K) is: T 5 K F T 1. Prompt the user to enter an input temperature in F. 2. Read the input temperature. 3. Calculate the temperature in kelvin. 4. Write the result and stop. example_2_4_temp_conversion.m Script file: temp_conversion.m Purpose: To convert an input temperature from degrees Fahrenheit to an output temperature in kelvin. Record of revisions: Date Programmer Description of change ==== ========== ===================== 01/03/14 S. J. Chapman Original code Define variables: temp_f -- Temperature in degrees Fahrenheit temp_k -- Temperature in kelvin Prompt the user for the input temperature. temp_f = input('enter the temperature in degrees Fahrenheit: '); Convert to kelvin. temp_k = (5/9) * (temp_f - 32) ; Write out the result. fprintf('6.2f degrees Fahrenheit = 6.2f kelvin.\n',... temp_f,temp_k);

27 Slide 26 of Examples EXAMPLE 2-5 A voltage source V = 120 V with an internal resistance R S of 50 W is applied to a load of resistance R L. Find the value of load resistance R L that will result in the maximum possible power being supplied by the source to the load. How much power will be supplied in this case? Also, plot the power supplied to the load as a function of the load resistance R L. In this exercise, we need to vary the load resistance R L and compute the power supplied to the load at each value of R L. 2 The power supplied to the load resistance is given by: PL I RL where I is the current supplied to the load. The current supplied to the load can be calculated by Ohm s Law: V V I R R R TOT S L 1. Create an array of possible values for the load resistance. 2. Calculate the current for each value of R L. 3. Calculate the power supplied to the load. 4. Plot the power supplied to the load. example_2_5_calc_power.m Script file: calc_power.m Define variables: amps -- Current flow to load (amps) pl -- Power supplied to load (watts) rl -- Resistance of the load (ohms) rs -- Internal resistance of the power source (ohms) volts -- Voltage of the power source (volts) Set the values of source voltage and internal resistance volts = 120; rs = 50; Create an array of load resistances rl = 1:1:100; Calculate the current flow for each resistance amps = volts./ ( rs + rl ); Calculate the power supplied to the load pl = (amps.^ 2).* rl; Plot the power versus load resistance plot(rl,pl); title('plot of power versus load resistance'); xlabel('load resistance (ohms)'); ylabel('power (watts)'); grid on;

28 Slide 27 of Debugging MATLAB Programs Debug a MATLAB Program Debug a MATLAB Program MATLAB Errors 3 Types of erros in MATLAB: (i) Syntax error: errors in the MATLAB statement itself (spelling, punctuation, format, etc.) (ii) Run-time error: errors when an illegal mathematical operation is attempted during the program execution (i.e., attempting to divide by zero, producing the infinity). The program returns inf or Nan, which is then used for further calculations. (iii) Logical error: errors when the program compiles and runs successfully but produces wrong answer (the toughest one to discover... MATLAB cannot detect this type of error: the program appears to run OK, but produces wrong results). You must spend some time, trying to debug your code. The MATLAB Symbolic Debugger MATLAB offers a powerful tool for debugging, embedded into the MATLAB editor. The MATLAB symbolic editor allows you to: walk through the code execution one statement at a time and to examine the values of any variables at each step along the way. see all of the intermediate results without having to insert a lot of output statements into your code.

2.2 Creating & Initializing Variables in MATLAB

2.2 Creating & Initializing Variables in MATLAB 2.2 Creating & Initializing Variables in MATLAB Slide 5 of 27 Do-It-Yourself (DIY) EXERCISE 2-1 Answer the followings: (a) What is the difference between an array, a matrix, and a vector? (b) Let: c =

More information

Finding, Starting and Using Matlab

Finding, Starting and Using Matlab Variables and Arrays Finding, Starting and Using Matlab CSC March 6 &, 9 Array: A collection of data values organized into rows and columns, and known by a single name. arr(,) Row Row Row Row 4 Col Col

More information

Chapter 4 Branching Statements & Program Design

Chapter 4 Branching Statements & Program Design EGR115 Introduction to Computing for Engineers Branching Statements & Program Design from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Program Design

More information

2. MATLAB Basics Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

2. MATLAB Basics Cengage Learning. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. MATLAB Programming for Engineers 5th Edition Chapman Solutions Manual Full Download: http://testbanklive.com/download/matlab-programming-for-engineers-5th-edition-chapman-solutions-manual/ 2. MATLAB Basics

More information

Chapter 1 Introduction to MATLAB

Chapter 1 Introduction to MATLAB EGR115 Introduction to Computing for Engineers Introduction to MATLAB from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Computing for Engineers 1.1

More information

Lab 1 Intro to MATLAB and FreeMat

Lab 1 Intro to MATLAB and FreeMat Lab 1 Intro to MATLAB and FreeMat Objectives concepts 1. Variables, vectors, and arrays 2. Plotting data 3. Script files skills 1. Use MATLAB to solve homework problems 2. Plot lab data and mathematical

More information

Chapter 2. MATLAB Basis

Chapter 2. MATLAB Basis Chapter MATLAB Basis Learning Objectives:. Write simple program modules to implement single numerical methods and algorithms. Use variables, operators, and control structures to implement simple sequential

More information

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1 Chapter 2 (Part 2) MATLAB Basics dr.dcd.h CS 101 /SJC 5th Edition 1 Display Format In the command window, integers are always displayed as integers Characters are always displayed as strings Other values

More information

To start using Matlab, you only need be concerned with the command window for now.

To start using Matlab, you only need be concerned with the command window for now. Getting Started Current folder window Atop the current folder window, you can see the address field which tells you where you are currently located. In programming, think of it as your current directory,

More information

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill Lecture 2 Introduction to MATLAB Dr.Tony Cahill The MATLAB Environment The Desktop Environment Command Window (Interactive commands) Command History Window Edit/Debug Window Workspace Browser Figure Windows

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name: 1 Matlab Tutorial 1- What is Matlab? Matlab is a powerful tool for almost any kind of mathematical application. It enables one to develop programs with a high degree of functionality. The user can write

More information

Chapter 1 Introduction to MATLAB

Chapter 1 Introduction to MATLAB Chapter 1 Introduction to MATLAB 1.1 What is MATLAB? MATLAB = MATrix LABoratory, the language of technical computing, modeling and simulation, data analysis and processing, visualization and graphics,

More information

1 Introduction to Matlab

1 Introduction to Matlab 1 Introduction to Matlab 1. What is Matlab? Matlab is a computer program designed to do mathematics. You might think of it as a super-calculator. That is, once Matlab has been started, you can enter computations,

More information

Introduction to MATLAB

Introduction to MATLAB ELG 3125 - Lab 1 Introduction to MATLAB TA: Chao Wang (cwang103@site.uottawa.ca) 2008 Fall ELG 3125 Signal and System Analysis P. 1 Do You Speak MATLAB? MATLAB - The Language of Technical Computing ELG

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 3 Creating, Organising & Processing Data Dr Richard Greenaway 3 Creating, Organising & Processing Data In this Workshop the matrix type is introduced

More information

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab MATH 495.3 (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab Below is a screen similar to what you should see when you open Matlab. The command window is the large box to the right containing the

More information

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 10 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB The Desktop When you start MATLAB, the desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB. The following

More information

Laboratory 1 Octave Tutorial

Laboratory 1 Octave Tutorial Signals, Spectra and Signal Processing Laboratory 1 Octave Tutorial 1.1 Introduction The purpose of this lab 1 is to become familiar with the GNU Octave 2 software environment. 1.2 Octave Review All laboratory

More information

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB? Experiment 1: Introduction to MATLAB I Introduction MATLAB, which stands for Matrix Laboratory, is a very powerful program for performing numerical and symbolic calculations, and is widely used in science

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures Introduction to Octave/Matlab Deployment of Telecommunication Infrastructures 1 What is Octave? Software for numerical computations and graphics Particularly designed for matrix computations Solving equations,

More information

MATLAB GUIDE UMD PHYS401 SPRING 2012

MATLAB GUIDE UMD PHYS401 SPRING 2012 MATLAB GUIDE UMD PHYS40 SPRING 202 We will be using Matlab (or, equivalently, the free clone GNU/Octave) this semester to perform calculations involving matrices and vectors. This guide gives a brief introduction

More information

Introduction to MATLAB

Introduction to MATLAB 58:110 Computer-Aided Engineering Spring 2005 Introduction to MATLAB Department of Mechanical and industrial engineering January 2005 Topics Introduction Running MATLAB and MATLAB Environment Getting help

More information

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis Introduction to Matlab 1 Outline What is Matlab? Matlab desktop & interface Scalar variables Vectors and matrices Exercise 1 Booleans Control structures File organization User defined functions Exercise

More information

MATLAB Introduction to MATLAB Programming

MATLAB Introduction to MATLAB Programming MATLAB Introduction to MATLAB Programming MATLAB Scripts So far we have typed all the commands in the Command Window which were executed when we hit Enter. Although every MATLAB command can be executed

More information

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

More information

Chapter 2. MATLAB Fundamentals

Chapter 2. MATLAB Fundamentals Chapter 2. MATLAB Fundamentals Choi Hae Jin Chapter Objectives q Learning how real and complex numbers are assigned to variables. q Learning how vectors and matrices are assigned values using simple assignment,

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014 PS 12a Laboratory 1 Spring 2014 Objectives This session is a tutorial designed to a very quick overview of some of the numerical skills that you ll need to get started. Throughout the tutorial, the instructors

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

Introduction to MATLAB

Introduction to MATLAB to MATLAB Spring 2019 to MATLAB Spring 2019 1 / 39 The Basics What is MATLAB? MATLAB Short for Matrix Laboratory matrix data structures are at the heart of programming in MATLAB We will consider arrays

More information

Chapter 8 Complex Numbers & 3-D Plots

Chapter 8 Complex Numbers & 3-D Plots EGR115 Introduction to Computing for Engineers Complex Numbers & 3-D Plots from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Complex Numbers & 3-D

More information

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks MATLAB Basics Stanley Liang, PhD York University Configure a MATLAB Package Get a MATLAB Student License on Matworks Visit MathWorks at https://www.mathworks.com/ It is recommended signing up with a student

More information

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY What is MATLAB? MATLAB (MATrix LABoratory) developed by The Mathworks, Inc. (http://www.mathworks.com) Key Features: High-level language for numerical

More information

EE 1105 Pre-lab 3 MATLAB - the ins and outs

EE 1105 Pre-lab 3 MATLAB - the ins and outs EE 1105 Pre-lab 3 MATLAB - the ins and outs INTRODUCTION MATLAB is a software tool used by engineers for wide variety of day to day tasks. MATLAB is available for UTEP s students via My Desktop. To access

More information

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University MATLAB Fundamentals Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University Reference: 1. Applied Numerical Methods with MATLAB for Engineers, Chapter 2 &

More information

Getting started with MATLAB

Getting started with MATLAB Sapienza University of Rome Department of economics and law Advanced Monetary Theory and Policy EPOS 2013/14 Getting started with MATLAB Giovanni Di Bartolomeo giovanni.dibartolomeo@uniroma1.it Outline

More information

Introduction to MatLab. Introduction to MatLab K. Craig 1

Introduction to MatLab. Introduction to MatLab K. Craig 1 Introduction to MatLab Introduction to MatLab K. Craig 1 MatLab Introduction MatLab and the MatLab Environment Numerical Calculations Basic Plotting and Graphics Matrix Computations and Solving Equations

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB 1 Introduction to MATLAB A Tutorial for the Course Computational Intelligence http://www.igi.tugraz.at/lehre/ci Stefan Häusler Institute for Theoretical Computer Science Inffeldgasse

More information

Introduction to MATLAB

Introduction to MATLAB Chapter 1 Introduction to MATLAB 1.1 Software Philosophy Matrix-based numeric computation MATrix LABoratory built-in support for standard matrix and vector operations High-level programming language Programming

More information

Programming in Mathematics. Mili I. Shah

Programming in Mathematics. Mili I. Shah Programming in Mathematics Mili I. Shah Starting Matlab Go to http://www.loyola.edu/moresoftware/ and login with your Loyola name and password... Matlab has eight main windows: Command Window Figure Window

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab By:Mohammad Sadeghi *Dr. Sajid Gul Khawaja Slides has been used partially to prepare this presentation Outline: What is Matlab? Matlab Screen Basic functions Variables, matrix, indexing

More information

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT PROGRAMMING WITH MATLAB DR. AHMET AKBULUT OVERVIEW WEEK 1 What is MATLAB? A powerful software tool: Scientific and engineering computations Signal processing Data analysis and visualization Physical system

More information

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018) Introduction to MATLAB Programming Chapter 3 Linguaggio Programmazione Matlab-Simulink (2017/2018) Algorithms An algorithm is the sequence of steps needed to solve a problem Top-down design approach to

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Introduction MATLAB is an interactive package for numerical analysis, matrix computation, control system design, and linear system analysis and design available on most CAEN platforms

More information

Digital Image Analysis and Processing CPE

Digital Image Analysis and Processing CPE Digital Image Analysis and Processing CPE 0907544 Matlab Tutorial Dr. Iyad Jafar Outline Matlab Environment Matlab as Calculator Common Mathematical Functions Defining Vectors and Arrays Addressing Vectors

More information

2.0 MATLAB Fundamentals

2.0 MATLAB Fundamentals 2.0 MATLAB Fundamentals 2.1 INTRODUCTION MATLAB is a computer program for computing scientific and engineering problems that can be expressed in mathematical form. The name MATLAB stands for MATrix LABoratory,

More information

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM TOPIC 1 INTRODUCING SOME MATHEMATICS SOFTWARE (Matlab, Maple and Mathematica) This topic provides

More information

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB Name Purpose: To learn to create matrices and use various MATLAB commands for reference later MATLAB built-in functions used: [ ] : ; + - * ^, size, help, format, eye, zeros, ones, diag, rand, round, cos,

More information

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu 0. What is MATLAB? 1 MATLAB stands for matrix laboratory and is one of the most popular software for numerical computation. MATLAB s basic

More information

CME 192: Introduction to Matlab

CME 192: Introduction to Matlab CME 192: Introduction to Matlab Matlab Basics Brett Naul January 15, 2015 Recap Using the command window interactively Variables: Assignment, Identifier rules, Workspace, command who and whos Setting the

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables Chapter 2 (Part 1) MATLAB Basics Arrays The fundamental unit of data in MATLAB is the array. An array is a collection of data values organized into rows and columns and is known by a specified name. Individual

More information

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) Introduction ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) MATLAB is a powerful mathematical language that is used in most engineering companies today. Its strength lies

More information

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras Module No. #01 Lecture No. #1.1 Introduction to MATLAB programming

More information

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment What is MATLAB? MATLAB PROGRAMMING Stands for MATrix LABoratory A software built around vectors and matrices A great tool for numerical computation of mathematical problems, such as Calculus Has powerful

More information

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB? Appendix A Introduction to MATLAB A.1 What Is MATLAB? MATLAB is a technical computing environment developed by The Math- Works, Inc. for computation and data visualization. It is both an interactive system

More information

QUICK INTRODUCTION TO MATLAB PART I

QUICK INTRODUCTION TO MATLAB PART I QUICK INTRODUCTION TO MATLAB PART I Department of Mathematics University of Colorado at Colorado Springs General Remarks This worksheet is designed for use with MATLAB version 6.5 or later. Once you have

More information

CITS2401 Computer Analysis & Visualisation

CITS2401 Computer Analysis & Visualisation FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Topic 3 Introduction to Matlab Material from MATLAB for

More information

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB Name Purpose: To learn to create matrices and use various MATLAB commands for reference later MATLAB functions used: [ ] : ; + - * ^, size, help, format, eye, zeros, ones, diag, rand, round, cos, sin,

More information

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple Chapter 2 Interactive MATLAB use only good if problem is simple Often, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated

More information

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

University of Alberta

University of Alberta A Brief Introduction to MATLAB University of Alberta M.G. Lipsett 2008 MATLAB is an interactive program for numerical computation and data visualization, used extensively by engineers for analysis of systems.

More information

Programming 1. Script files. help cd Example:

Programming 1. Script files. help cd Example: Programming Until now we worked with Matlab interactively, executing simple statements line by line, often reentering the same sequences of commands. Alternatively, we can store the Matlab input commands

More information

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group Introduction to MATLAB Simon O Keefe Non-Standard Computation Group sok@cs.york.ac.uk Content n An introduction to MATLAB n The MATLAB interfaces n Variables, vectors and matrices n Using operators n Using

More information

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed. Example 4 Printing and Plotting Matlab provides numerous print and plot options. This example illustrates the basics and provides enough detail that you can use it for typical classroom work and assignments.

More information

Computer Programming in MATLAB

Computer Programming in MATLAB Computer Programming in MATLAB Prof. Dr. İrfan KAYMAZ Atatürk University Engineering Faculty Department of Mechanical Engineering What is a computer??? Computer is a device that computes, especially a

More information

INTRODUCTION TO MATLAB

INTRODUCTION TO MATLAB 1 of 18 BEFORE YOU BEGIN PREREQUISITE LABS None EXPECTED KNOWLEDGE Algebra and fundamentals of linear algebra. EQUIPMENT None MATERIALS None OBJECTIVES INTRODUCTION TO MATLAB After completing this lab

More information

ENGR 1181 MATLAB 05: Input and Output

ENGR 1181 MATLAB 05: Input and Output ENGR 1181 MATLAB 05: Input and Output Learning Objectives 1. Create a basic program that can be used over and over or given to another person to use 2. Demonstrate proper use of the input command, which

More information

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors. 1 LECTURE 3 OUTLINES Variable names in MATLAB Examples Matrices, Vectors and Scalar Scalar Vectors Entering a vector Colon operator ( : ) Mathematical operations on vectors examples 2 VARIABLE NAMES IN

More information

Introduction to MATLAB

Introduction to MATLAB CHEE MATLAB Tutorial Introduction to MATLAB Introduction In this tutorial, you will learn how to enter matrices and perform some matrix operations using MATLAB. MATLAB is an interactive program for numerical

More information

Lecturer: Keyvan Dehmamy

Lecturer: Keyvan Dehmamy MATLAB Tutorial Lecturer: Keyvan Dehmamy 1 Topics Introduction Running MATLAB and MATLAB Environment Getting help Variables Vectors, Matrices, and linear Algebra Mathematical Functions and Applications

More information

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to. Starting Matlab Go to MATLAB Laboratory 09/09/10 Lecture Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu http://ctx.loyola.edu and login with your Loyola name and password...

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Zhiyu Zhao (sylvia@cs.uno.edu) The LONI Institute & Department of Computer Science College of Sciences University of New Orleans 03/02/2009 Outline What is MATLAB Getting Started

More information

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations MATLAB Tutorial The following tutorial has been compiled from several resources including the online Help menu of MATLAB. It contains a list of commands that will be directly helpful for understanding

More information

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab and Octave: Quick Introduction and Examples 1 Basics Matlab and Octave: Quick Introduction and Examples 1 Basics 1.1 Syntax and m-files There is a shell where commands can be written in. All commands must either be built-in commands, functions, names of

More information

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013 Lab of COMP 406 MATLAB: Quick Start Lab tutor : Gene Yu Zhao Mailbox: csyuzhao@comp.polyu.edu.hk or genexinvivian@gmail.com Lab 1: 11th Sep, 2013 1 Where is Matlab? Find the Matlab under the folder 1.

More information

A General Introduction to Matlab

A General Introduction to Matlab Master Degree Course in ELECTRONICS ENGINEERING http://www.dii.unimore.it/~lbiagiotti/systemscontroltheory.html A General Introduction to Matlab e-mail: luigi.biagiotti@unimore.it http://www.dii.unimore.it/~lbiagiotti

More information

Introduction to MATLAB

Introduction to MATLAB Quick Start Tutorial Introduction to MATLAB Hans-Petter Halvorsen, M.Sc. What is MATLAB? MATLAB is a tool for technical computing, computation and visualization in an integrated environment. MATLAB is

More information

EGR 111 Introduction to MATLAB

EGR 111 Introduction to MATLAB EGR 111 Introduction to MATLAB This lab introduces the MATLAB help facility, shows how MATLAB TM, which stands for MATrix LABoratory, can be used as an advanced calculator. This lab also introduces assignment

More information

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

LAB 2: Linear Equations and Matrix Algebra. Preliminaries Math 250C, Section C2 Hard copy submission Matlab # 2 1 Revised 07/13/2016 LAB 2: Linear Equations and Matrix Algebra In this lab you will use Matlab to study the following topics: Solving a system of

More information

Stokes Modelling Workshop

Stokes Modelling Workshop Stokes Modelling Workshop 14/06/2016 Introduction to Matlab www.maths.nuigalway.ie/modellingworkshop16/files 14/06/2016 Stokes Modelling Workshop Introduction to Matlab 1 / 16 Matlab As part of this crash

More information

Matlab Tutorial 1: Working with variables, arrays, and plotting

Matlab Tutorial 1: Working with variables, arrays, and plotting Matlab Tutorial 1: Working with variables, arrays, and plotting Setting up Matlab First of all, let's make sure we all have the same layout of the different windows in Matlab. Go to Home Layout Default.

More information

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP) Digital Signal Processing Prof. Nizamettin AYDIN naydin@yildiz.edu.tr naydin@ieee.org http://www.yildiz.edu.tr/~naydin Course Details Course Code : 0113620 Course Name: Digital Signal Processing (Sayısal

More information

A Guide to Using Some Basic MATLAB Functions

A Guide to Using Some Basic MATLAB Functions A Guide to Using Some Basic MATLAB Functions UNC Charlotte Robert W. Cox This document provides a brief overview of some of the essential MATLAB functionality. More thorough descriptions are available

More information

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline MATLAB Tutorial EE351M DSP Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Kitaek Bae Outline Part I: Introduction and Overview Part II: Matrix manipulations and common functions Part III: Plots

More information

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr A0B17MTB Matlab Part #1 Miloslav Čapek miloslav.capek@fel.cvut.cz Filip Kozák, Viktor Adler, Pavel Valtr Department of Electromagnetic Field B2-626, Prague You will learn Scalars, vectors, matrices (class

More information

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices Introduction to Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> V = 50 >> V + 2 >> V Ans = 52 >> a=5e-3; b=1; a+b Most elementary functions and constants are

More information

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System CHAPTER 1 INTRODUCTION Digital signal processing (DSP) technology has expanded at a rapid rate to include such diverse applications as CDs, DVDs, MP3 players, ipods, digital cameras, digital light processing

More information

MATLAB QUICK START TUTORIAL

MATLAB QUICK START TUTORIAL MATLAB QUICK START TUTORIAL This tutorial is a brief introduction to MATLAB which is considered one of the most powerful languages of technical computing. In the following sections, the basic knowledge

More information

Lecture 1: Hello, MATLAB!

Lecture 1: Hello, MATLAB! Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21 Syllabus Instructor: Eric Hallman Class Website: https://math.berkeley.edu/~ehallman/98-fa18/ Login:!cmfmath98

More information

An Introduction to MATLAB II

An Introduction to MATLAB II Lab of COMP 319 An Introduction to MATLAB II Lab tutor : Gene Yu Zhao Mailbox: csyuzhao@comp.polyu.edu.hk or genexinvivian@gmail.com Lab 2: 16th Sep, 2013 1 Outline of Lab 2 Review of Lab 1 Matrix in Matlab

More information

6.094 Introduction to MATLAB January (IAP) 2009

6.094 Introduction to MATLAB January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.094 Introduction to MATLAB January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 6.094 Introduction

More information

Course Layout. Go to https://www.license.boun.edu.tr, follow instr. Accessible within campus (only for the first download)

Course Layout. Go to https://www.license.boun.edu.tr, follow instr. Accessible within campus (only for the first download) Course Layout Lectures 1: Variables, Scripts and Operations 2: Visualization and Programming 3: Solving Equations, Fitting 4: Images, Animations, Advanced Methods 5: Optional: Symbolic Math, Simulink Course

More information

AN INTRODUCTION TO MATLAB

AN INTRODUCTION TO MATLAB AN INTRODUCTION TO MATLAB 1 Introduction MATLAB is a powerful mathematical tool used for a number of engineering applications such as communication engineering, digital signal processing, control engineering,

More information