Problem Solving With MATLAB: CPET 190
Problem Solving With MATLAB: CPET 190
Lin 1
CPET 190
Lecture 4
Problem Solving with MATLAB
http://www.etcs.ipfw.edu/~lin
September 13, 2005 Lecture 4 - By P. Lin 2
Lecture 4: MATLAB Array Basics
4.1 MATLAB Variables and Arrays
Variables
Variable Initialization
Assignment
input()
Variable Value Display
disp() function
fprintf() function
Example 4.1.a Row Vector
Example 4.1.b Column Vector
Example 4.1.c Square Matrix or Array
Example 4.1.d A 2-by-3 Array Example
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
September 13, 2005 Lecture 4 - By P. Lin 4
4.1 MATLAB Variables and Arrays
Variables
Variable 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
digits
String variables 16-bit
value characters,
enclosed by a pair of
single quotes
>> n = 1e-308
n =
1.0000e-308
>> n = 1e-400
n =
0
>> name = MATLAB
name =
MATLAB
September 13, 2005 Lecture 4 - By P. Lin 5
4.1 MATLAB Variables and Arrays
Variable Initialization
Using assignment
statement
Using keyboard input
function input( )
Variable Value Display
disp( )
fprintf( )
>> x = 10;
>> y = x
y =
10
>> x1 = input('Please enter a
number: ')
Please enter a number: 100
x1 = 100
>> disp(x1)
100
>> fprintf('%f',x1)
100.000000
>> fprintf('%e',x1)
1.000000e+002
September 13, 2005 Lecture 4 - By P. Lin 6
4.1 MATLAB Variables and Arrays
(continue)
Arrays
Definition 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 programs
Scalars are treated as arrays with
one column and one row
A MATALB Example: scalar
variable n
>> n = 100;
Row1
Col 1
A Scalar
n
100
September 13, 2005 Lecture 4 - By P. Lin 7
4.1 MATLAB Variables and Arrays
(continue)
Arrays
An array can be single dimension or
multidimensional
The dimension of the MATLAB array can
be defined implicitly by assignment
statement or by size( ) function
Array Initialization Examples
1-D row vector
1-D column vector
2-D array or matrix
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
>> 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
40 30 20 10
Col 1 Col 2 Col 3 Col 4
Row1
product
An array of 1 x 4
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
>> column_vector = [10; 20; 30]
column_vector =
10
20
30
Row1
Row 2
10
20
Row 3
30
Col 1
A Column Vector of 3 x 1
column_vector
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
>>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
Row1
Row 2
40 30 20 10
80 70 60 50
Row 3
Row 4
160 150 140 130
120 110 100 90
Col
1
Col
2
Col
3
Col
4
An Array of 4 x 4
items
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
>> num = [10 20; 30 40; 50 60]
num =
10 20
30 40
50 60
Row1
Row 2
20 10
40 30
Row 3
60 50
Col
1
Col
2
An Array (matrix) of 3 x 2
num
September 13, 2005 Lecture 4 - By P. Lin 12
4.2 Problem Solving Examples
Example 4-2: Calculating Series
Resistance.
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.
September 13, 2005 Lecture 4 - By P. Lin 13
4.2 Problem Solving Examples (continue)
Example 4-2: Calculating Series
Resistance.
Problem Solving Process and Steps:
Define the problem
Formulate a mathematical model
Develop an algorithm
Write the code for the problem
Test program and verify the solution
Document the program and solution
September 13, 2005 Lecture 4 - By P. Lin 14
4.2 Problem Solving Examples (continue)
Example 4-2: Calculating Series Resistance.
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.2: Compute subtotal resistance
Step 3.3: Repeat until the end of resistance entering
Step 3.4: Print total resistance
4. Write the code for the problem
September 13, 2005 Lecture 4 - By P. Lin 15
Example 4-2: Calculating Series
Resistance Solution 1
MATLAB Solution 1
Understand who users
are
How to input resistance
values?
The mathematical
model or equation
Total R = R1 + R2 + R3 +
+ Rn
How to display or print
total resistance value?
>> r1 = 10; r2 = 20;
>> r3 = 40;
>> r_total = r1 + r2 + r3
r_total =
70
September 13, 2005 Lecture 4 - By P. Lin 16
Example 4-2: Calculating Series
Resistance Solution 2
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_total = r_total + r;
n = n-1;
end
fprintf('Total Resistance = %f',
r_total)
September 13, 2005 Lecture 4 - By P. Lin 18
Example 4-2: Calculating Series
Resistance Solution 2 (continue)
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
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.
Analysis of the Problem
References
National Institute of Standards,
http://physics.nist.gov/cuu/Units/index.html
Conversion of Temperatures, http://www.chemie.fu-
berlin.de/chemistry/general/units_en.html#temp
http://www.bipm.org/en/si/
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
Freezing temperature of water - 32F
Boiling temperature of water - 212F
Formula for C to F Conversion
Degree F = C (9/5) + 32
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.
Formulas
Degree C = kelvin - 273.15
Degree C = (F 32)/1.8
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.
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
kelvin = C + 273.15
degree F = C (9/5) + 32
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:
The boiling point temperature of water
212F 100C 373.15 K
The sublimation point of dry ice
-110F -77.89C 195.26 K
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
Display output using fprintf() function
September 13, 2005 Lecture 4 - By P. Lin 25
Example 4-3 Temperature Conversion
Coding MATLAB Program
September 13, 2005 Lecture 4 - By P. Lin 26
Example 4-3 Temperature Conversion
Coding MATLAB Program
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
September 13, 2005 Lecture 4 - By P. Lin 28
Summary
MATLAB Variables and Arrays
Variable
Variable Initialization
Arrays and examples
Problem Solving Examples
Calculating series resistance
Temperature conversion
September 13, 2005 Lecture 4 - By P. Lin 29
Question?
Answers
Email: [email protected]