ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Flow Charts, Loop Structures
ECE Application Programming
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Programming with MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB Loops and Branching.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
ENGR-36_Lec-19_Beams-2.pptx 1 Bruce Mayer, PE Engineering-36: Engineering Mechanics - Statics Bruce Mayer, PE Licensed Electrical.
ENGR-25_Linear_Equations-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Licensed Electrical & Mechanical Engineer
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 4 Programming with MATLAB PowerPoint to accompany Copyright © The.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical.
ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MTH55_Lec-65_Fa08_sec_9-5b_Logarithmic_Eqns.ppt 1 Bruce Mayer, PE Chabot College Mathematics Bruce Mayer, PE Licensed Electrical.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENGR-25_MATLAB_OverView-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab Programming for Engineers
ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
Digital Image Processing Introduction to M-function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
ENGR-25_HW-01_Solution.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
MTH15_Lec-17a_sec_3-5_Round_End_Fence_Enclosure.pptx 1 Bruce Mayer, PE MTH15: Applied Calculus I Bruce Mayer, PE Licensed Electrical.
© ENGR-43_Prob_14-32_OpAmp_OutPut_Current.pptx 1 Bruce Mayer, PE Engineering-43 Electrical Circuits & Devices Bruce Mayer, PE.
1 Structured Programming EEN170 Programming in MATLAB.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
ENGR-25_HW-01_Solution.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
ENGR-25_Linear_Regression_Tutorial.ppt 1 Bruce Mayer, PE Engineering-25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer.
Introduction to MATLAB for Engineers, Third Edition
Control Statements in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
More Selections BIS1523 – Lecture 9.
Licensed Electrical & Mechanical Engineer
Chp3 Nodal Analysis & MATLAB
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Using Script Files and Managing Data
Licensed Electrical & Mechanical Engineer
Presentation transcript:

ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp4 MATLAB Programming-3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 ; a nice result ;-) >> Eto3_7_Ltrs = num2str(exp(3.7), 11) Eto3_7_Ltrs = >> disp(['e to 3.7 pwr = ', Eto3_7_Ltrs]) e to 3.7 pwr =

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

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

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

ENGR-25_Programming-3.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today A Nice Spring Platform

ENGR-25_Programming-3.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Appendix

ENGR-25_Programming-3.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Prob 4-20: spring_plat.m

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 =  Case-2 → W = 2 kN >> spring_plat(2e3,10e3,15e3,0.1) The Compression distance in m, x =

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 = ');

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