week 9 - programming iii today: – another loop option – a programming example: tic-tac-toe...

Post on 18-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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:

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

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:

...13

4

11

4

9

4

7

4

5

4

3

44

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

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

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:

Flowchart:

Initialization

Get X move Get O moveO

wins?

Get X move

Xwins?

GameOverDraw?

Yes

YesYes

No

No

No

GameOver

GameOver

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

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

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

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

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.

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.

top related