39
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)

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,

Embed Size (px)

Citation preview

Page 1: 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,

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)

Page 2: 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,

Quiz on Thursday – meet in 112 Kirk.

Page 3: 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,

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.

Page 4: 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,

Answer: MATLAB’s “while” loop:

while expression

{commands to be repeated as long

as expression is true}

end

Page 5: 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,

Prior example – compounded interest untilthe amount doubles:

value = 1000;

for year = 1:1000

value = value * 1.08;

disp([num2str(year),' years: $ ',num2str(value) ])

if value > 2000

break

end

end

for loop

terminated with break

Page 6: 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,

Expected output:

Page 7: 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,

while version

format bank

value = 1000;

while value < 2000

value = value * 1.08;

disp(value)

end

Page 8: 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,

Example – Collecting and storing data until a zero is entered:

x = [ ];

new = 1;

while new ~= 0

new = input('enter value ');

x = [ x, new ];

end

x = x(1:end–1)

empty array

to drop the zero

initialize

Page 9: 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,

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

Page 10: 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,

or:

x = input('enter value ');

while (x<0)|(x>10)

disp('invalid choice');

x = input('enter value ');

end

disp('finally!');

Page 11: 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,

Example – computing pi:

...13

4

11

4

9

4

7

4

5

4

3

44

Page 12: 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,

Example – “infinite” Hi-Lo:

numb = round (10*rand(1)); done = 0;while ~done

guess = input('guess');if guess = = numb disp( 'You got it !!!' ); done = 1;elseif guess > numb disp('too high')else disp('too low')end

end

initialization

single guess

loopcontrol

Page 13: 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,

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

Page 14: 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,

Example of Programming: tic-tac-toe

Play tic-tac-toe, human against human:– Track moves– Show board– Recognize end of game

Page 15: 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,

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

Page 16: 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,

1. Initialization, including graphics

2. Get X’s first move

3. Loop 4 times:– Get O’s move, check for win– Get X’s move, check for win– Break on victory

4. Check for draw

Program flow:

Page 17: 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,

Flowchart:

Initialization

Get X move Get O moveO

wins?

Get X move

Xwins?

GameOverDraw?

Yes

YesYes

No

No

No

GameOver

GameOver

Page 18: 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,

Program Outline:

Page 19: 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,

Program Details: Initialization

Page 20: 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,

Board Graphic

Page 21: 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,

Get and Show First Move (X)

Page 22: 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,

Start Loop with the Second Player

Page 23: 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,

Check for Victory by O

Page 24: 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,

Finish Loop with the First Player

Page 25: 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,

Check for a Draw

Page 26: 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,

Typical Output

Page 27: 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,

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.

Page 28: 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,

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

Page 29: 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,

Stress Strain Curves

The load extension data can be transformedinto Stress Strain data by normalizingwith respect to material dimensions.The stress is the load divided by the original cross sectional area.

= L/As – stress , units MPa, or psi or ksiL – load appliedA – original cross sectional area

The strain is the increase in normalized by the original length.

e = l/le – strain – dimensionless (in/in)l – increase in lengthl – original lengthStrain is often given in percent so x100As the normalizations are by constantsthe shapes of the curves stays the sameBuilds on mechanical behavior from EGR 105which was elastic deformation

Stress

Strain

Failure Point

EGR 105 Range

Page 30: 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,

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.

Page 31: 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,

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.

Page 32: 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,

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)

Page 33: 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,

Non Steady State Ficks 2nd Law

Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni

c/t=D(2C/x2) 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

Page 34: 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,

Diffusion Rates

Solute Solvent D at 500 C Dat1000 C Carbon BCC iron 5x10-12 2x10-9

Carbon FCC iron 5x10-15 3x10-11

Iron BCC iron 10-20 3x10-14

Nickel FCC iron 10-23 2x10-16

Silver Silver Xtal 10-17

Silver Silver Grain Bound 10-11

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.

Page 35: 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,

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

Page 36: 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,

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.

Page 37: 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,

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.

Page 38: 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,

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.

Page 39: 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,

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.