Date post: | 05-Jan-2016 |
Category: |
Documents |
Upload: | stephany-lester |
View: | 217 times |
Download: | 1 times |
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 11
CPET 190CPET 190
Lecture 4 Lecture 4
Problem Solving with MATLABProblem Solving with MATLAB
http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 22
Lecture 4: MATLAB Array Basics Lecture 4: MATLAB Array Basics
4.1 MATLAB Variables and Arrays4.1 MATLAB Variables and Arrays VariablesVariables Variable InitializationVariable Initialization
• AssignmentAssignment• input()input()
Variable Value DisplayVariable Value Display• disp() functiondisp() function• fprintf() functionfprintf() function
Example 4.1.a Row VectorExample 4.1.a Row Vector Example 4.1.b Column VectorExample 4.1.b Column Vector Example 4.1.c Square Matrix or ArrayExample 4.1.c Square Matrix or Array Example 4.1.d A 2-by-3 Array ExampleExample 4.1.d A 2-by-3 Array Example
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 33
Lecture 4: MATLAB Array Basics Lecture 4: MATLAB Array Basics (continue)(continue)
4.2 Problem Solving Examples4.2 Problem Solving ExamplesExample 4-2: Calculating Series ResistanceExample 4-2: Calculating Series ResistanceExample 4-3: Temperature ConversionExample 4-3: Temperature Conversion
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 44
4.1 MATLAB Variables and Arrays4.1 MATLAB Variables and Arrays
VariablesVariables• Variable names are case Variable names are case
sensitive and weakly sensitive and weakly typed typed
• Numerical variable type Numerical variable type – “double” type in the – “double” type in the range of 10range of 10-308-308 to 10 to 10308308 with 15 to 16 significant with 15 to 16 significant digitsdigits
• String variables – 16-bit String variables – 16-bit value characters, value characters, enclosed by a pair of enclosed by a pair of single quotes single quotes
>> n = 1e-308>> n = 1e-308n =n = 1.0000e-3081.0000e-308
>> n = 1e-400>> n = 1e-400n =n = 00>> name = ‘MATLAB’>> name = ‘MATLAB’
name =name =
MATLABMATLAB
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 55
4.1 MATLAB Variables and Arrays4.1 MATLAB Variables and Arrays
Variable InitializationVariable Initialization• Using assignment Using assignment
statement statement • Using keyboard input Using keyboard input
function – input( )function – input( )
Variable Value DisplayVariable Value Display• disp( )disp( )• fprintf( )fprintf( )
>> x = 10;>> x = 10;>> y = x>> y = xy =y = 1010>> x1 = input('Please enter a >> x1 = input('Please enter a number: ')number: ')Please enter a number: 100Please enter a number: 100x1 = 100x1 = 100>> disp(x1)>> disp(x1) 100100>> fprintf('%f',x1)>> fprintf('%f',x1)100.000000100.000000>> fprintf('%e',x1)>> fprintf('%e',x1)1.000000e+0021.000000e+002
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 66
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
ArraysArrays• Definition – an array is a Definition – an array is a
collection data values organized collection data values organized into rows and columns, and into rows and columns, and accessed by its name and accessed by its name and subscriptssubscripts
• Fundamental unit of data in Fundamental unit of data in MATLAB programsMATLAB programs
• Scalars are treated as arrays with Scalars are treated as arrays with one column and one rowone column and one row
A MATALB Example: scalar A MATALB Example: scalar variable nvariable n
>> n = 100;>> n = 100;
Row1
Col 1A Scalar
n
100
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 77
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
ArraysArrays• An array can be single dimension or An array can be single dimension or
multidimensionalmultidimensional• The dimension of the MATLAB array can The dimension of the MATLAB array can
be defined implicitly by assignment be defined implicitly by assignment statement or by size( ) functionstatement or by size( ) function
• Array Initialization ExamplesArray Initialization Examples 1-D row vector1-D row vector 1-D column vector1-D column vector 2-D array or matrix2-D array or matrix
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 88
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
Example 4.1.a - A 1-Example 4.1.a - A 1-by-4 Array, or Row by-4 Array, or Row Vector ExampleVector Example
>> product = [10 20 30 40];>> product = [10 20 30 40];>> product(1,1)>> product(1,1)ans = 10ans = 10>> m = product(1,2)>> m = product(1,2)m = 20m = 20>> product(1,3) = m>> product(1,3) = mproduct = 10 20 20 40product = 10 20 20 40>> r = 1; c = 4;>> r = 1; c = 4;>> k = product(r,c)>> k = product(r,c)K = 40K = 40
Syntax for accessing individual element:
product(r, c)
Subscripts: r - row, c -column
40302010
Col 1 Col 2 Col 3 Col 4
Row1
product
An array of 1 x 4
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 99
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
Example 4.1.b - A 3-by-1 Example 4.1.b - A 3-by-1 Array, or Column Vector Array, or Column Vector ExampleExample
>> column_vector = [10; 20; 30]>> column_vector = [10; 20; 30]
column_vector =column_vector =
1010
2020
3030
Row1
Row 2
10
20
Row 3 30
Col 1
A Column Vector of 3 x 1
column_vector
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1010
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
Example 4.1.c - A 4 by 4 Example 4.1.c - A 4 by 4 Array ExampleArray Example
>>items = [10 20 30 40; 50 60 >>items = [10 20 30 40; 50 60 70 80; 90 100 110 120; 130 70 80; 90 100 110 120; 130 140 150 160];140 150 160];
>> r = 3; c = 3;>> r = 3; c = 3;
>> items(r, c)>> items(r, c)
ans = 110ans = 110
>> size(items)>> size(items)
ans = 4 4ans = 4 4
Row1
Row 2
40302010
80706050
Row 3
Row 4 160150140130
12011010090
Col 1
Col 2
Col 3
Col 4
An Array of 4 x 4
items
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1111
4.1 MATLAB Variables and Arrays 4.1 MATLAB Variables and Arrays (continue)(continue)
Example 4.1.d - A 2 by 3 Example 4.1.d - A 2 by 3 Array ExampleArray Example
>>>> num = [10 20; 30 40; 50 60] num = [10 20; 30 40; 50 60]
num =num =
10 2010 20
30 4030 40
50 6050 60
Row1
Row 2
2010
4030
Row 3 6050
Col 1
Col 2
An Array (matrix) of 3 x 2
num
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1212
4.2 Problem Solving Examples4.2 Problem Solving Examples
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance.Resistance.
Problem Statement:Problem Statement:• You are asked by your supervisor to design a You are asked by your supervisor to design a
MATLAB program for your colleague. This MATLAB program for your colleague. This program should allow the user to calculate the program should allow the user to calculate the total series resistance of a circuit.total series resistance of a circuit.
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1313
4.2 Problem Solving Examples 4.2 Problem Solving Examples (continue)(continue)
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance.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
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1414
4.2 Problem Solving Examples 4.2 Problem Solving Examples (continue)(continue)
Example 4-2: Calculating Series Resistance.Example 4-2: Calculating Series Resistance. Problem Solving Process and Steps:Problem Solving Process and Steps:
1.1. Define the problemDefine the problem
2.2. Formulate a mathematical model - Total R = R1 + R2 Formulate a mathematical model - Total R = R1 + R2 + R3 + … + Rn+ R3 + … + Rn
3.3. Develop an algorithmDevelop an algorithm
Step 3.1: User input R1, R2, R3, … Rn, one at a timeStep 3.1: User input R1, R2, R3, … Rn, one at a time
Step 3.2: Compute subtotal resistanceStep 3.2: Compute subtotal resistance
Step 3.3: Repeat until the end of resistance enteringStep 3.3: Repeat until the end of resistance entering
Step 3.4: Print total resistanceStep 3.4: Print total resistance
4. Write the code for the problem4. Write the code for the problem
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1515
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance – Solution 1Resistance – Solution 1
MATLAB Solution 1MATLAB Solution 1• Understand who users Understand who users
areare• How to input resistance How to input resistance
values?values?• The mathematical The mathematical
model or equationmodel or equation
Total R = R1 + R2 + R3 + Total R = R1 + R2 + R3 + … + Rn… + Rn
• How to display or print How to display or print total resistance value?total resistance value?
>> r1 = 10; r2 = 20;>> r1 = 10; r2 = 20;
>> r3 = 40;>> r3 = 40;
>> r_total = r1 + r2 + r3>> r_total = r1 + r2 + r3
r_total =r_total =
7070
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1616
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance – Solution 2Resistance – Solution 2
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1717
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance – Solution 2Resistance – Solution 2
% Program: n_resistor.m% Program: n_resistor.m% Author: M. Lin% Author: M. Lin% Date: August 20, 2004% Date: August 20, 2004% Description:% Description:% This program does the follwoings:% This program does the follwoings:% 1. Prompts the user to enter the% 1. Prompts the user to enter the% number of resistors in series% number of resistors in series% 2. Prompts the user to enter individual% 2. Prompts the user to enter individual% resistor value, and compute the% resistor value, and compute the% subtotal % subtotal % value.% value.% 3. Updates the loop counter until all % 3. Updates the loop counter until all % resistance are entered% resistance are entered% 4. Compute and print the total % 4. Compute and print the total
resistanceresistance% Variables used:% Variables used:% r_total -- Total Resistance% r_total -- Total Resistance% r -- Input Resistance% r -- Input Resistance% n -- Number of resistors% n -- Number of resistors
r_total = 0;r_total = 0;n = input('Number of Resistors: n = input('Number of Resistors: ‘ )‘ )while n > 0while 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;endendfprintf('Total Resistance = %f', fprintf('Total Resistance = %f', r_total)r_total)
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1818
Example 4-2: Calculating Series Example 4-2: Calculating Series Resistance – Solution 2 Resistance – Solution 2 (continue)(continue)
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 1919
Example 4-2: Run n_resistor.m at Example 4-2: Run n_resistor.m at the Command Windowthe Command Window
Current Directory
n_resistor.m3 hit Enter key
10 hit Enter key
30 hit Enter key
20 hit Enter key
Total Resistance
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2020
4.2 Problem Solving Examples4.2 Problem Solving Examples
Example 4-3: Problem Statement: Design a Example 4-3: Problem Statement: Design a MATLAB program that reads an input temperature MATLAB program that reads an input temperature in degree Fahrenheit, converts it to an in degree Fahrenheit, converts it to an temperature in degree Kelvin and degree Celsius.temperature in degree Kelvin and degree Celsius.
Analysis of the ProblemAnalysis of the Problem• ReferencesReferences• National Institute of Standards, National Institute of Standards,
http://physics.nist.gov/cuu/Units/index.htmlhttp://physics.nist.gov/cuu/Units/index.html• Conversion of Temperatures, Conversion of Temperatures,
http://www.chemie.fu-berlin.de/chemistry/general/units_enhttp://www.chemie.fu-berlin.de/chemistry/general/units_en.html#temp.html#temp
• http://www.bipm.org/en/si/http://www.bipm.org/en/si/
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2121
Example 4.3 Temperature Conversion - Example 4.3 Temperature Conversion - Analysis of the Problem Analysis of the Problem (cont.)(cont.)
The degree FahrenheitThe degree Fahrenheit (°F)(°F) - - non-metric non-metric temperature scaletemperature scale
Freezing temperature of water - 32Freezing temperature of water - 32°°FF Boiling temperature of water - 212°FBoiling temperature of water - 212°F
• Formula for C to F ConversionFormula for C to F Conversion Degree Degree F = C (9/5) + 32F = C (9/5) + 32
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2222
Example 4-3 Temperature Conversion Example 4-3 Temperature Conversion – Analysis of the Problem – Analysis of the Problem (cont.)(cont.)
The degree Celsius (°C)The degree Celsius (°C) scale - dividing the scale - dividing the range of temperature between the freezing range of temperature between the freezing and boiling temperatures of pure water at and boiling temperatures of pure water at standard atmospheric conditions (sea level standard atmospheric conditions (sea level pressure) into 100 equal parts. pressure) into 100 equal parts.
FormulasFormulasDegree C = kelvin - 273.15Degree C = kelvin - 273.15
Degree C = (F – 32)/1.8Degree C = (F – 32)/1.8
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2323
Example 4-3 Temperature Conversion Example 4-3 Temperature Conversion – Analysis of the Problem – Analysis of the Problem (cont.)(cont.)
The The kelvin (K) kelvin (K) temperature scale is an extension temperature scale is an extension of the degree Celsius scale down to of the degree Celsius scale down to absolute zeroabsolute zero, , a hypothetical temperature characterized by a a hypothetical temperature characterized by a complete absence of heat energy. complete absence of heat energy.
Temperatures on this scale are called Temperatures on this scale are called kelvinkelvin, NOT , NOT degrees kelvin, kelvin is not capitalized, and the degrees kelvin, kelvin is not capitalized, and the symbol (capital K) stands alone with no degree symbol (capital K) stands alone with no degree symbol. symbol.
FormulasFormulaskelvin = C + 273.15kelvin = C + 273.15 degree F = C (9/5) + 32degree F = C (9/5) + 32
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2424
Example 4-3 Temperature Example 4-3 Temperature Conversion – Program DesignConversion – Program Design
We use the following values for validating the We use the following values for validating the computes answers:computes answers:The boiling point temperature of water The boiling point temperature of water • 212°F 212°F 100°C 100°C 373.15 K373.15 K
The sublimation point of dry iceThe sublimation point of dry ice• -110°F-110°F -77.89°C-77.89°C 195.26 K195.26 K
Formulas for computationFormulas for computationTTCC in degree C = (T in degree C = (TFF – 32.0)* (9/5) – 32.0)* (9/5) kelvin = Tkelvin = TCC + 273.15 + 273.15
User input will be from keyboard using input( ) User input will be from keyboard using input( ) functionfunction
Display output using fprintf() functionDisplay output using fprintf() function
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2525
Example 4-3 Temperature Conversion Example 4-3 Temperature Conversion – Coding MATLAB Program– Coding MATLAB Program
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2626
Example 4-3 Temperature Conversion Example 4-3 Temperature Conversion – Coding MATLAB Program– Coding MATLAB Program
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2727
Example 4-3 Temperature Example 4-3 Temperature Conversion – Testing the ProgramConversion – Testing the Program
>>temp_convert>>temp_convert
Please enter the temperature in F: 212Please enter the temperature in F: 212
Temperature in F 212.00Temperature in F 212.00
Temperature in C 100.00Temperature in C 100.00
Temperature in kelvin 373.15Temperature in kelvin 373.15
>> temp_convert>> temp_convert
Please enter the temperature in F: -110Please enter the temperature in F: -110
Temperature in F -110.00Temperature in F -110.00
Temperature in C -78.89Temperature in C -78.89
Temperature in kelvin 194.26Temperature in kelvin 194.26
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2828
SummarySummary
MATLAB Variables and ArraysMATLAB Variables and Arrays
• VariableVariable
• Variable InitializationVariable Initialization
• Arrays and examplesArrays and examples Problem Solving ExamplesProblem Solving Examples
• Calculating series resistanceCalculating series resistance
• Temperature conversionTemperature conversion
September 13, 2005September 13, 2005 Lecture 4 - By P. LinLecture 4 - By P. Lin 2929
Question?Question?
AnswersAnswers
Email: [email protected]: [email protected]