Download presentation
Presentation is loading. Please wait.
Published byLucy Sutton Modified over 9 years ago
1
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Chp4 MATLAB Programming-3
2
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Learning Goals Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output Write Programs that Employ LOOPing Processes For → No. Loops know a priori while → Loop Terminates based on Logic Criteria
3
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods if Decision Statement The if Statement Basic Form if logical expression statements end Every if statement must have an accompanying end statement. The end statement marks the terminous of the statements that are to be executed if the logical expression is true. Start if Logical Expression Statements end Statements True False
4
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods else Decision Statement else Statement Basic Form if logical expression Statement Group-1 else Statement Group-2 end Start if Logical Expression Statements-1 end Statements True False Statements-2
5
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Logical Expression on Arrays When the test, if logical expression, is performed, where the logical expression may be an ARRAY, the test returns a value of true only if ALL the elements of the logical expression evaluated as true “Logic Gate” equivalent statement (“and”) –ALL High → Output High –ELSE Output Low
6
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Logical Array Example For example, if we fail to recognize how the test works, the following statements may not perform as expected x = [4,-9,25]; if x < 0 disp(’Some elements of x are negative.’) else y = sqrt(x) end Because the test if x < 0 is false, when this program is run it gives the result y = 2.0000 0 + 3.0000i 5.0000
7
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Logical Array Example cont Now Fine-Tune to Test for Positive x x = [4,-9,25]; if x >= 0 y = sqrt(x) else disp('Some elements of x are negative.') end This File Produces Only the Message Some elements of x are negative. The test if x = 0 also returns a false value because x >= 0 returns the vector [1,0,1]. ENTIRE Statement is FALSE since ONE element is False
8
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Combine Decisions & Logicals The following statements if logical expression 1 if logical expression 2 statements end end Can be written more Concisely with if logical expression 1 & logical expression 2 statements end
9
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example: Decisions & Logicals The Code r = 7; s = -11; % Double if if r < 13 if s > -17 t = log(abs(r)+abs(s)) end end % if-n-& if (r < 13)&(s > -17) q = log(abs(r)+abs(s)) end The OutPut t = 2.8904 q = 2.8904
10
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The elseif Statement The elseif Statement Basic Form if logical expression 1 statement group 1 elseif logical expression 2 statement group 2 else statement group 3 end The else statement must come AFTER the elseif statement to since the else yields the DEFAULT result Default
11
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods elseif FlowChart Start if Logical Expression Statements-1 end Statements elseif Logical Expr True else Statements-2 True Statements-3 else
12
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods elseif Example Given y = log(x) for x > 10 y = sqrt(x) for 0 <= x <= 10, y = exp(x) - 1 for x < 0 The Following finds y for given user input for x x = input('enter scalar: x = ') if x > 10; z = log(x); elseif x >= 0 z = sqrt(x); else z = exp(x) - 1; end % output to user y = z The InPut & OutPut x = -3.7000 y = -0.9753
13
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Nested if FlowChart Start x < 13? y = sinh(x) Yes y, z out y > 43? y = 7.3x z = -19x + 29 y 17? z = √yz = y 1.89 z = 0 No Yes Nesting if statements is a alternative to the elseif form
14
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Strings A STRING is a variable that contains Characters. Strings are useful for creating input prompts & messages, and for storing & operating on data such as names and addresses To create a string variable, enclose the characters in single quotes. For example, Create string variable NYgiant : >> NYgiant = 'Mel Ott' NYgiant = Mel Ott
15
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Strings cont The following string, digits, is not the same as the variable number created by typing digits = 987. >> digits = '987' digits = 987 >> p = log(digits) ??? Function 'log' is not defined for values of class 'char'. No Indent for String
16
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Strings & the input Statement The prompt program on the next slide uses the isempty(x) function, which returns a 1 if the array x is empty and 0 otherwise The Program also uses the input function, with syntax This function displays the string prompt on the screen, waits for input from the keyboard, and returns the entered value in the string variable x. The function returns an empty matrix if you press the Enter key without typing anything. x = input(’prompt’, ’string’)
17
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Strings and Decisions The following prompt program is a script file that allows the user to answer Yes by typing either Y or y or by pressing the Enter key. Any other response is treated as the answer No. response = input('Want to continue? Y/N:','s') if (isempty(response))|(response==’Y’)| response==’y’) response = ’Y’ else response = ’N’ end
18
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Display Str&Double on SAME Line >> disp(['the SqRt of "e" is ', num2str(exp(1/2), 7), '; a nice result ;-)']) the SqRt of "e" is 1.648721; a nice result ;-) >> Eto3_7_Ltrs = num2str(exp(3.7), 11) Eto3_7_Ltrs =40.44730436 >> disp(['e to 3.7 pwr = ', Eto3_7_Ltrs]) e to 3.7 pwr = 40.44730436
19
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example: Prob 4-20 Given Spring- Loaded Platform In This Case the ENGR36 Force- Balance Model In ENGR36 we Learn that the Spring Force Where –k the “Spring Constant” in N/m –x the Compression Distance in m
20
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Problem 4-20 cont Find or Create a.A Function to find x given W, k 1, k 2, and d b.A Plot of x vs. W for 0 W 3000 N given –k 1 = 10 kN/m = 10 N/mm –k 2 = 15 kN/m = 15 N/mm –d = 100 mm Solve Force-Balance Eqns for x
21
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The Prob 4-20 Plots Before Brk-Pt After Brk-Pt
22
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today A Nice Spring Platform
23
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix
24
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 4-20: spring_plat.m
25
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob-20 Test Session Case-1 → W = 500N >> spring_plat(500,10e3,15e3,0.1) The Compression distance in m, x = 0.0500 Case-2 → W = 2 kN >> spring_plat(2e3,10e3,15e3,0.1) The Compression distance in m, x = 0.1250
26
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods.m-file = Prob4_20_plot.m Documentation & Inputs % Bruce Mayer, PE * 26Sep11 % ENGR25 * Problem 4-20 % file = Prob4_20_plot.m % Plot Displacement vs. Wt for Spring Platform % % INPUT SECTION Wmax = input('Max Weight in N = '); k1 = input('Main-Spring Const in N/m = '); k2 = input('Secondary-Spring Const in N/m = '); d = input('distance to 2nd Springs in m = ');
27
BMayer@ChabotCollege.edu ENGR-25_Programming-3.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods.m-file = Prob4_20_plot.m cont %CALCULATION SECTION % calc Critical-Load for Curve Break-Point Wcrit = k1*d % Plot 3 pts for PieceWise Linear range if Wcrit <= Wmax % All 3 springs engaged W=[0, Wcrit, Wmax]; % 3element row vector % make corresponding 3-pt output vector x = [spring_plat(W(1),k1,k2,d),spring_plat(W(2),k1,k2,d),... spring_plat(W(3),k1,k2,d)]; plot(W,1000*x,'-s'), xlabel('Wt (N)'), ylabel('Compression (mm)'),... title('Above-Critical Load') % Plot 2pts for one Spring Linear Range else W = [0,Wmax] x = [spring_plat(W(1),k1,k2,d), spring_plat(W(2),k1,k2,d)]; plot(W,1000*x,'-s'), xlabel('Wt (N)'), ylabel('Compression (mm)'),... title('Below-Critical Load') end
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.