Week 9 - Programming III
Today:
– Another loop option
– A programming example: tic-tac-toe
Textbook chapter 7, pages 213-216, 219-226
(sections 7.4.2, 7.7)
Quiz on Thursday – meet in 112 Kirk.
Longer Running Loops
for loops repeat a fixed number of times:
for variable = {array of length n}
{commands}
end
and break can be used to stop earlier.
Question: How about repeating “until done”? Run as
long as is needed.
Answer: MATLAB’s “while” loop:
while expression
{commands to be repeated as long
as expression is true}
end
Prior example – compounded interest until
the amount doubles:
for loop
value = 1000;
for year = 1:1000
value = value * 1.08;
disp([num2str(year),' years: $ ',num2str(value) ])
if value > 2000
break
end
terminated
end
with break
Expected output:
while version
format bank
value = 1000;
while value < 2000
value = value * 1.08;
disp(value)
end
Example – Collecting and storing data until a
zero is entered:
empty array
x = [ ];
initialize
new = 1;
while new ~= 0
new = input('enter value ');
x = [ x, new ];
end
x = x(1:end–1)
to drop the zero
Example – Getting valid keyboard input:
E.g. forcing the user’s input to be between 0 and 10:
x = –3;
while ( x < 0 ) | ( x > 10 )
x = input( 'type a value ' );
end
or:
x = input('enter value ');
while (x<0)|(x>10)
disp('invalid choice');
x = input('enter value ');
end
disp('finally!');
Example – computing pi:
4 4 4 4 4 4
4 ...
3 5 7 9 11 13
Example – “infinite” Hi-Lo:
loop
control
numb = round (10*rand(1));
initialization
done = 0;
while ~done
guess = input('guess');
if guess = = numb
disp( 'You got it !!!' ); done = 1;
elseif guess > numb
single
disp('too high')
guess
else
disp('too low')
end
end
Nesting of while loops
while expression1
{outer loop commands}
while expression2
{inner loop commands}
end
{more outer loop commands}
end
these can also be more
than 2 levels deep
Example of Programming: tic-tac-toe
Play tic-tac-toe, human against human:
–
–
–
Track moves
Show board
Recognize end of game
3-by-3 array for the board:
–
–
–
Empty cell = 0
X = +1
O=–1
Game end:
–
–
Winner if row, col, or diag sum = +3 or –3
Draw is no zeros left
Loop for game:
–
–
Initial move (X) plus 4 pairs of moves
Break if winner
Program flow:
1.
2.
3.
4.
Initialization, including graphics
Get X’s first move
Loop 4 times:
– Get O’s move, check for win
– Get X’s move, check for win
– Break on victory
Check for draw
Flowchart:
Initialization
Get X move
O
wins?
Get O move
Yes
Game
Over
No
Get X move
No
Game
Over
Yes
Draw?
No
X
wins?
Yes
Game
Over
Program Outline:
Program Details: Initialization
Board Graphic
Get and Show First Move (X)
Start Loop with the Second Player
Check for Victory by O
Finish Loop with the First Player
Check for a Draw
Typical Output
Semester Project Intro
CARBURIZING GEARS
–
–
Diffusion of atomic carbon into steel allows very
hard iron carbides to form near surface. These
“cases” are hard and very wear resistant so are
used to make gears.
Project is to model this for a variety of different
conditions.
Example of Diffusion Leading to
Failure
Steel plate with a hydrogen blister in it.
H+H H2
Atomic hydrogen from surface moves through steel
until it combines to form molecular hydrogen which
cannot move in steel. Pressure builds forming blister
Stress Strain Curves
Stress
Failure Point
The load extension data can be transformed
into Stress Strain data by normalizing
with respect to material dimensions.
The stress is the load divided by the
original cross sectional area.
s = L/A
s – stress , units MPa, or psi or ksi
L – load applied
A – original cross sectional area
EGR 105 Range
The strain is the increase in
normalized by the original length.
e = Dl/l
e – strain – dimensionless (in/in)
Dl – increase in length
l – original length
Strain is often given in percent so x100
As the normalizations are by constants
the shapes of the curves stays the same
Builds on mechanical behavior from EGR 105
which was elastic deformation
Strain
De-Carburization
Decarburization at 1200F after quench crack in material. The crack left enough
open surface for the carbon to diffuse out and leave a ferrite layer either side
of the crack.
Diffusion – Basics.
Atomic carbon is diffusing through steel
Diffusion follows Arhenius equation so activation energy
controlled
D = Do exp (–Q/RT) –create as a function?
D – diffusion coefficient (m2/s)
Do - temp independent pre-exponent (m2/s)
Q – activation energy (kJ/mol )
R – gas constant 8.31 J/mol-k
T – absolute temp (K)
Temperature dependent.
DIFFUSION- Units
R - 8.31 J/mol-K; 1.987 cal/mol-K; 8.62
ev/atom-K
Q – J/mol; cal/mol; ev/atom. (1ev/atom =
23kcal/mol)
Non Steady State Ficks 2nd Law
Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni
dc/dt=D(d2C/dx2) Ficks 2nd Law
Solution to this :Cx-Co/Cs-Co= 1- erf(x/2((Dt)-1/2))
Cx – concentration at depth x at time t, wt%
Co – concentration in average in bulk, wt %
Cs – concentration at surface, fixed with time t, wt%
Co- concentration in average in bulk, wt%
Erf – error function – look up in tables.
x – distance below surface, m
D – diffusion coefficient, m2/s
t – time in seconds
Diffusion Rates
Solute
Solvent
Carbon
BCC iron
Carbon
FCC iron
Iron BCC iron
NickelFCC iron
Silver Silver Xtal
Silver Silver Grain Bound
D at 500 C
5x10-12
5x10-15
10-20
10-23
10-17
10-11
Dat1000 C
2x10-9
3x10-11
3x10-14
2x10-16
Interested in carbon in iron.
First assignment – need to be able to find D at any temperature to use in
Fick’s second law . Thursday lab assignment - use Arhenius equation as a
function? Look at week 5 notes for plot of D v1/T.
Example
Time for the carbon concentration at 500C to reach half way between the steel
composition level and the external level at 0.5mm below the surface.
Using Fick’s second law
Cx-Co/Cs-Co= 1- erf(x/2((Dt)-1/2))
The left hand side is 0.5.
0.5= 1- erf(x/2((Dt)-1/2))
Rearranging
0.5 = erf(x/2((Dt)-1/2))
0.5 = erf(0.5205)
So
0.5=(x/2 ((Dt)-1/2))
Dt = x2
t=x2/D
=(5x10-4) 2/(5x10-12)
t= 25x10-8/5x10-12
=5x104sec
=13.8 hours
Overall Project
A program that will take input such as temperature,
steel composition, and carbon concentration in the
surrounding environment then as output be able to
graphically show carbon concentration profiles in the
steel as a function of time and treatment
temperature. In between the input and output is the
program in “Matlab”
An oral report using Powerpoint.
A two page written report per team.
Teams
Vertical – have a team leader responsible for
all organization of team and output. The
leader assigns work to team members. Team
members report only to team leader. Team
leader must ensure assigned tasks are
completed by team member. Allows for
individual meetings.
Teams
Horizontal – all members responsible for
organization and output. Work assignment is
a group activity. Task completion could be
group as well. All meetings team meetings.
Which team depends on personnel. You
should decide which type of team.
STRATEGIC PLANNING?
Once you decide on team type, develop a
“Strategic Plan” – What are the teams objectives?
How are you going to achieve them? Write it out so
the team can follow it.
Companies and other organizations have “Vision
and Mission Statements” as well a strategic plans.
Meet next Tuesday with strategy and first
assignment from Thursday lab complete.
© Copyright 2026 Paperzz