Presentation is loading. Please wait.

Presentation is loading. Please wait.

September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB

Similar presentations


Presentation on theme: "September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB"— Presentation transcript:

1 September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB http://www.etcs.ipfw.edu/~lin

2 September 13, 2005 Lecture 4 - By P. Lin 2 Lecture 4: MATLAB Array Basics 4.1 MATLAB Variables and Arrays Variables Variables Variable Initialization Variable Initialization AssignmentAssignment input()input() Variable Value Display Variable Value Display disp() functiondisp() function fprintf() functionfprintf() function Example 4.1.a Row Vector Example 4.1.a Row Vector Example 4.1.b Column Vector Example 4.1.b Column Vector Example 4.1.c Square Matrix or Array Example 4.1.c Square Matrix or Array Example 4.1.d A 2-by-3 Array Example Example 4.1.d A 2-by-3 Array Example

3 September 13, 2005 Lecture 4 - By P. Lin 3 Lecture 4: MATLAB Array Basics (continue) 4.2 Problem Solving Examples Example 4-2: Calculating Series Resistance Example 4-3: Temperature Conversion

4 September 13, 2005 Lecture 4 - By P. Lin 4 4.1 MATLAB Variables and Arrays Variables Variables Variable names are case sensitive and weakly typedVariable names are case sensitive and weakly typed Numerical variable type – “double” type in the range of 10 -308 to 10 308 with 15 to 16 significant digitsNumerical variable type – “double” type in the range of 10 -308 to 10 308 with 15 to 16 significant digits String variables – 16-bit value characters, enclosed by a pair of single quotesString variables – 16-bit value characters, enclosed by a pair of single quotes >> n = 1e-308 n = 1.0000e-308 1.0000e-308 >> n = 1e-400 n = 0 >> name = ‘MATLAB’ name = MATLAB

5 September 13, 2005 Lecture 4 - By P. Lin 5 4.1 MATLAB Variables and Arrays Variable Initialization Variable Initialization Using assignment statementUsing assignment statement Using keyboard input function – input( )Using keyboard input function – input( ) Variable Value Display Variable Value Display disp( )disp( ) fprintf( )fprintf( ) >> x = 10; >> y = x y = 10 10 >> x1 = input('Please enter a number: ') Please enter a number: 100 x1 = 100 >> disp(x1) 100 100 >> fprintf('%f',x1) 100.000000 >> fprintf('%e',x1) 1.000000e+002

6 September 13, 2005 Lecture 4 - By P. Lin 6 4.1 MATLAB Variables and Arrays (continue) Arrays Arrays Definition – an array is a collection data values organized into rows and columns, and accessed by its name and subscriptsDefinition – an array is a collection data values organized into rows and columns, and accessed by its name and subscripts Fundamental unit of data in MATLAB programsFundamental unit of data in MATLAB programs Scalars are treated as arrays with one column and one rowScalars are treated as arrays with one column and one row A MATALB Example: scalar variable n A MATALB Example: scalar variable n >> n = 100;

7 September 13, 2005 Lecture 4 - By P. Lin 7 4.1 MATLAB Variables and Arrays (continue) Arrays Arrays An array can be single dimension or multidimensionalAn array can be single dimension or multidimensional The dimension of the MATLAB array can be defined implicitly by assignment statement or by size( ) functionThe dimension of the MATLAB array can be defined implicitly by assignment statement or by size( ) function Array Initialization ExamplesArray Initialization Examples 1-D row vector 1-D row vector 1-D column vector 1-D column vector 2-D array or matrix 2-D array or matrix

8 September 13, 2005 Lecture 4 - By P. Lin 8 4.1 MATLAB Variables and Arrays (continue) Example 4.1.a - A 1- by-4 Array, or Row Vector Example Example 4.1.a - A 1- by-4 Array, or Row Vector Example >> product = [10 20 30 40]; >> product(1,1) ans = 10 >> m = product(1,2) m = 20 >> product(1,3) = m product = 10 20 20 40 >> r = 1; c = 4; >> k = product(r,c) K = 40 Syntax for accessing individual element: product(r, c) Subscripts: r - row, c - column

9 September 13, 2005 Lecture 4 - By P. Lin 9 4.1 MATLAB Variables and Arrays (continue) Example 4.1.b - A 3-by-1 Array, or Column Vector Example Example 4.1.b - A 3-by-1 Array, or Column Vector Example >> column_vector = [10; 20; 30] column_vector = 10 10 20 20 30 30

10 September 13, 2005 Lecture 4 - By P. Lin 10 4.1 MATLAB Variables and Arrays (continue) Example 4.1.c - A 4 by 4 Array Example Example 4.1.c - A 4 by 4 Array Example >>items = [10 20 30 40; 50 60 70 80; 90 100 110 120; 130 140 150 160]; >> r = 3; c = 3; >> items(r, c) ans = 110 >> size(items) ans = 4 4

11 September 13, 2005 Lecture 4 - By P. Lin 11 4.1 MATLAB Variables and Arrays (continue) Example 4.1.d - A 2 by 3 Array Example Example 4.1.d - A 2 by 3 Array Example >> num = [10 20; 30 40; 50 60] num = 10 20 10 20 30 40 30 40 50 60 50 60

12 September 13, 2005 Lecture 4 - By P. Lin 12 4.2 Problem Solving Examples Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Statement: Problem Statement: You are asked by your supervisor to design a MATLAB program for your colleague. This program should allow the user to calculate the total series resistance of a circuit.You are asked by your supervisor to design a MATLAB program for your colleague. This program should allow the user to calculate the total series resistance of a circuit.

13 September 13, 2005 Lecture 4 - By P. Lin 13 4.2 Problem Solving Examples (continue) Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Solving Process and Steps: Problem Solving Process and Steps: Define the problemDefine the problem Formulate a mathematical modelFormulate a mathematical model Develop an algorithmDevelop an algorithm Write the code for the problemWrite the code for the problem Test program and verify the solutionTest program and verify the solution Document the program and solutionDocument the program and solution

14 September 13, 2005 Lecture 4 - By P. Lin 14 4.2 Problem Solving Examples (continue) Example 4-2: Calculating Series Resistance. Example 4-2: Calculating Series Resistance. Problem Solving Process and Steps: Problem Solving Process and Steps: 1.Define the problem 2.Formulate a mathematical model - Total R = R1 + R2 + R3 + … + Rn 3.Develop an algorithm Step 3.1: User input R1, R2, R3, … Rn, one at a time Step 3.1: User input R1, R2, R3, … Rn, one at a time Step 3.2: Compute subtotal resistance Step 3.2: Compute subtotal resistance Step 3.3: Repeat until the end of resistance entering Step 3.3: Repeat until the end of resistance entering Step 3.4: Print total resistance Step 3.4: Print total resistance 4. Write the code for the problem

15 September 13, 2005 Lecture 4 - By P. Lin 15 Example 4-2: Calculating Series Resistance – Solution 1 MATLAB Solution 1 MATLAB Solution 1 Understand who users areUnderstand who users are How to input resistance values?How to input resistance values? The mathematical model or equationThe mathematical model or equation Total R = R1 + R2 + R3 + … + Rn How to display or print total resistance value?How to display or print total resistance value? >> r1 = 10; r2 = 20; >> r3 = 40; >> r_total = r1 + r2 + r3 r_total = 70 70

16 September 13, 2005 Lecture 4 - By P. Lin 16 Example 4-2: Calculating Series Resistance – Solution 2

17 September 13, 2005 Lecture 4 - By P. Lin 17 Example 4-2: Calculating Series Resistance – Solution 2 % Program: n_resistor.m % Author: M. Lin % Date: August 20, 2004 % Description: % This program does the follwoings: % 1. Prompts the user to enter the % number of resistors in series % 2. Prompts the user to enter individual % resistor value, and compute the % subtotal % value. % 3. Updates the loop counter until all % resistance are entered % 4. Compute and print the total resistance % Variables used: % r_total -- Total Resistance % r -- Input Resistance % n -- Number of resistors r_total = 0; n = input('Number of Resistors: ‘ ) while n > 0 r = input('Enter a resistance: ‘ ) r = input('Enter a resistance: ‘ ) r_total = r_total + r; r_total = r_total + r; n = n-1; n = n-1;end fprintf('Total Resistance = %f', r_total)

18 September 13, 2005 Lecture 4 - By P. Lin 18 Example 4-2: Calculating Series Resistance – Solution 2 (continue)

19 September 13, 2005 Lecture 4 - By P. Lin 19 Example 4-2: Run n_resistor.m at the Command Window Current Directory n_resistor.m 3 hit Enter key 10 hit Enter key 30 hit Enter key 20 hit Enter key Total Resistance

20 September 13, 2005 Lecture 4 - By P. Lin 20 4.2 Problem Solving Examples Example 4-3: Problem Statement: Design a MATLAB program that reads an input temperature in degree Fahrenheit, converts it to an temperature in degree Kelvin and degree Celsius. Example 4-3: Problem Statement: Design a MATLAB program that reads an input temperature in degree Fahrenheit, converts it to an temperature in degree Kelvin and degree Celsius. Analysis of the Problem Analysis of the Problem ReferencesReferences National Institute of Standards, http://physics.nist.gov/cuu/Units/index.htmlNational Institute of Standards, http://physics.nist.gov/cuu/Units/index.html http://physics.nist.gov/cuu/Units/index.html Conversion of Temperatures, http://www.chemie.fu- berlin.de/chemistry/general/units_en.html#tempConversion of Temperatures, http://www.chemie.fu- berlin.de/chemistry/general/units_en.html#temphttp://www.chemie.fu- berlin.de/chemistry/general/units_en.html#temphttp://www.chemie.fu- berlin.de/chemistry/general/units_en.html#temp http://www.bipm.org/en/si/http://www.bipm.org/en/si/http://www.bipm.org/en/si/

21 September 13, 2005 Lecture 4 - By P. Lin 21 Example 4.3 Temperature Conversion - Analysis of the Problem (cont.) The degree Fahrenheit (°F) - non-metric temperature scale The degree Fahrenheit (°F) - non-metric temperature scale Freezing temperature of water - 32°F Freezing temperature of water - 32°F Boiling temperature of water - 212°F Boiling temperature of water - 212°F Formula for C to F ConversionFormula for C to F Conversion Degree F = C (9/5) + 32 Degree F = C (9/5) + 32

22 September 13, 2005 Lecture 4 - By P. Lin 22 Example 4-3 Temperature Conversion – Analysis of the Problem (cont.) The degree Celsius (°C) scale - dividing the range of temperature between the freezing and boiling temperatures of pure water at standard atmospheric conditions (sea level pressure) into 100 equal parts. The degree Celsius (°C) scale - dividing the range of temperature between the freezing and boiling temperatures of pure water at standard atmospheric conditions (sea level pressure) into 100 equal parts. Formulas Formulas Degree C = kelvin - 273.15 Degree C = (F – 32)/1.8

23 September 13, 2005 Lecture 4 - By P. Lin 23 Example 4-3 Temperature Conversion – Analysis of the Problem (cont.) The kelvin (K) temperature scale is an extension of the degree Celsius scale down to absolute zero, a hypothetical temperature characterized by a complete absence of heat energy. The kelvin (K) temperature scale is an extension of the degree Celsius scale down to absolute zero, a hypothetical temperature characterized by a complete absence of heat energy. Temperatures on this scale are called kelvin, NOT degrees kelvin, kelvin is not capitalized, and the symbol (capital K) stands alone with no degree symbol. Temperatures on this scale are called kelvin, NOT degrees kelvin, kelvin is not capitalized, and the symbol (capital K) stands alone with no degree symbol. Formulas Formulas kelvin = C + 273.15 degree F = C (9/5) + 32

24 September 13, 2005 Lecture 4 - By P. Lin 24 Example 4-3 Temperature Conversion – Program Design We use the following values for validating the computes answers: We use the following values for validating the computes answers: The boiling point temperature of water 212°F 100°C 373.15 K212°F 100°C 373.15 K The sublimation point of dry ice -110°F-77.89°C195.26 K-110°F-77.89°C195.26 K Formulas for computation Formulas for computation T C in degree C = (T F – 32.0)* (9/5) kelvin = T C + 273.15 User input will be from keyboard using input( ) function User input will be from keyboard using input( ) function Display output using fprintf() function Display output using fprintf() function

25 September 13, 2005 Lecture 4 - By P. Lin 25 Example 4-3 Temperature Conversion – Coding MATLAB Program

26 September 13, 2005 Lecture 4 - By P. Lin 26 Example 4-3 Temperature Conversion – Coding MATLAB Program

27 September 13, 2005 Lecture 4 - By P. Lin 27 Example 4-3 Temperature Conversion – Testing the Program >>temp_convert Please enter the temperature in F: 212 Temperature in F 212.00 Temperature in C 100.00 Temperature in kelvin 373.15 >> temp_convert Please enter the temperature in F: -110 Temperature in F -110.00 Temperature in C -78.89 Temperature in kelvin 194.26

28 September 13, 2005 Lecture 4 - By P. Lin 28 Summary MATLAB Variables and Arrays MATLAB Variables and Arrays VariableVariable Variable InitializationVariable Initialization Arrays and examplesArrays and examples Problem Solving Examples Problem Solving Examples Calculating series resistanceCalculating series resistance Temperature conversionTemperature conversion

29 September 13, 2005 Lecture 4 - By P. Lin 29 Question? Answers Email: lin@ipfw.edu


Download ppt "September 13, 2005 Lecture 4 - By P. Lin 1 CPET 190 Lecture 4 Problem Solving with MATLAB"

Similar presentations


Ads by Google