Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

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, 7.7)

2 Quiz on Thursday – meet in 112 Kirk.

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

4 Answer: M ATLAB ’s “while” loop: while expression {commands to be repeated as long as expression is true} end

5 Prior example – compounded interest until the amount doubles: value = 1000; for year = 1:1000 value = value * 1.08; disp([num2str(year),' years: $ ',num2str(value) ]) if value > 2000 break end for loop terminated with break

6 Expected output:

7 while version format bank value = 1000; while value < 2000 value = value * 1.08; disp(value) end

8 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

9 Example – Getting valid keyboard input: E.g. forcing the user’s input to be between 0 and 10: x = –3; while ( x 10 ) x = input( 'type a value ' ); end

10 or: x = input('enter value '); while (x 10) disp('invalid choice'); x = input('enter value '); end disp('finally!');

11 Example – computing pi:

12 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 initialization single guess loop control

13 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

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

15 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

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

17 Flowchart:

18 Program Outline:

19 Program Details: Initialization

20 Board Graphic

21 Get and Show First Move (X)

22 Start Loop with the Second Player

23 Check for Victory by O

24 Finish Loop with the First Player

25 Check for a Draw

26 Typical Output

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

28 Example of Diffusion Leading to Failure Steel plate with a hydrogen blister in it. H+H  H 2 Atomic hydrogen from surface moves through steel until it combines to form molecular hydrogen which cannot move in steel. Pressure builds forming blister

29 Stress Strain Curves 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.  = L/A s – stress, units MPa, or psi or ksi L – load applied A – original cross sectional area The strain is the increase in normalized by the original length. e =  l/l e – strain – dimensionless (in/in)  l – 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 Stress Strain Failure Point EGR 105 Range

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

31 Diffusion – Basics. Atomic carbon is diffusing through steel Diffusion follows Arhenius equation so activation energy controlled D = D o exp (–Q/RT) –create as a function? D – diffusion coefficient (m 2 /s) D o - temp independent pre-exponent (m 2 /s) Q – activation energy (kJ/mol ) R – gas constant 8.31 J/mol-k T – absolute temp (K) Temperature dependent.

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

33 Non Steady State Ficks 2 nd Law Non Steady State – Concentration changes at position x as a function of time, eg Cu Ni  c/  t=D(  2 C/  x 2 ) Ficks 2 nd 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, m 2 /s t – time in seconds

34 Diffusion Rates SoluteSolventD at 500 CDat1000 C CarbonBCC iron5x10 -12 2x10 -9 CarbonFCC iron5x10 -15 3x10 -11 IronBCC iron10 -20 3x10 -14 NickelFCC iron10 -23 2x10 -16 Silver Silver Xtal10 -17 SilverSilver Grain Bound10 -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.

35 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 = x 2 t=x 2 /D =(5x10 -4 ) 2 /(5x10 -12 ) t= 25x10 -8 /5x10 -12 =5x10 4 sec =13.8 hours

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

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

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

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


Download ppt "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,"

Similar presentations


Ads by Google