Download presentation
Presentation is loading. Please wait.
1
Dr. Jie Zou PHY 33201 Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 3.
2
Dr. Jie Zou PHY 33202 Outline M-files Script files* Function files Interactive Input and output Enter information Display result Structured programming Decisions (or Selection) Loops (or Repetition)
3
Dr. Jie Zou PHY 33203 M-files M-files: An M-file is a text file stored with a.m extension. To create a new M-file: Go to File, New, M-file. Type in the commands line by line in the edit window. Save the file as filename.m. To execute an M-file: Type the filename without.m in the command window. Two types of M-files: Script files: A script file is a set of MATLAB commands that are saved on a file – we consider script files mainly in this course. Function files: Read 3.1.2 and 3.5 of the hand-out.
4
Dr. Jie Zou PHY 33204 Example: bungee- jumper-1.m t=[0:2:20]’; %Time(s) g=9.81; %Acceleration due to gravity(m/s^2) m=68.1; %Mass (kg) cd=0.25; %Drag coefficient (kg/m) v=sqrt(g*m/cd)*tanh(sqrt(g*cd/ m)*t); figure; %To open a figure window plot(t, v) title(‘Plot of v versus t’) xlabel(‘Values of t’) ylabel(‘Values of v’) Develop a script file to compute and plot the velocity v of the bungee jumper as a function of time t. Set t = 0 to 20 s in steps of 2. Save it as bungee- jumper-1.m Execute the file. What are the values of v at t = 0, 10, and 20 s?
5
Dr. Jie Zou PHY 33205 Interactive Input and output The input command: n = input('promptstring’): Prompt the user to enter a value from the keyboard n = input('promptstring','s'): Prompt the user to enter a string from the keyboard Examples: >> m = input(‘Mass (kg): ‘); >> myname = input(‘My name is: ‘,’s’); The display command: display(value) : value = a numeric value, a variable, or a string enclosed in ‘ ‘. Examples: >> display(m)>> display(myname)
6
Dr. Jie Zou PHY 33206 Example: bungee- jumper-2.m g=9.81; %Acceleration due to gravity(m/s^2) m=input(‘Mass (kg): ‘); cd=input(‘Drag coefficient (kg/m): ‘); t=input (‘Time (s): ‘); v=sqrt(g*m/cd)*tanh(sqrt(g*cd/ m)*t); disp(‘ ‘) disp(‘Velocity (m/s):’) disp(v) In the bungee-jumper example, create a script file that prompts the user to enter the values for the mass, m, in kg, the drag coefficient, c d, in kg/m, and the time, t, in s. Also, compute and display the value for the velocity, v, at the specified time instant. Save the file as bungee- jumper-2.m. Run the file.
7
Dr. Jie Zou PHY 33207 Structured programming: Decisions (1) The if Structure: Syntax: if condition statements end Example: Write an M- file (Grade.m) to evaluate whether a grade is passing. See the flowchart. grade >= 60 T Output: displayed message (‘passing grade’) Input: grade = numerical value (0 -100) F
8
Dr. Jie Zou PHY 33208 Structured programming: Decisions (cont.) (2) The if…else Structure: Syntax: if condition statements 1 else statements 2 end Example: Modify Grade.m to evaluate whether a grade is passing or not passing. See the flowchart. grade >= 60 T Output: displayed message (‘passing grade’) Input: grade = numerical value (0 -100) F Output: displayed message (‘Not passing grade’)
9
Dr. Jie Zou PHY 33209 Structured programming: Decisions (cont.) (3) The if…elseif Structure: Syntax if condition 1 statements 1 elseif condition 2 statements 2. else statements else end Example: Write an M-file (mysign.m) to do the following: Input x; If x > 0, display ‘sign = 1’; if x < 0, display ‘sign = -1’; otherwise, display ‘sign = 0’.
10
Dr. Jie Zou PHY 332010 Relational operators in MATLAB ExampleOperatorRelationship x == 0== Equal unit ~= ‘m’~= Not equal a < 0< Less than s > t> Greater than 3.9 <= a/3<= Less than or equal to r >= 0>= Greater than or equal to
11
Dr. Jie Zou PHY 332011 Logical operators in MATLAB xy~x (Not)x & y (And)x | y (Or) TTFTT TFFFT FTTFT FFTFF Priority of the operators: Highest Lowest
12
Dr. Jie Zou PHY 332012 Structured programming: Loops (1) The for…end Structure: Syntax: for index = start:step:finish statements end Note: If an increment of 1 is desired, the step can be dropped. step can be any numeric value, integer or non-integer, positive or negative. Example: Write your own M-file (mysum.m) to compute the following sum using a for loop: 1 + 2 + 3 + … 100 Also, check the MATLAB built-in function sum.
13
Dr. Jie Zou PHY 332013 Structured programming: Loops (cont.) (2) The while structure: A while loop repeats as long as a logical condition is true. Syntax: while condition statements end Example: What will be displayed? x = 8 while x > 0 x = x – 3; disp(x) end
14
Dr. Jie Zou PHY 332014 Structured programming: Loops (cont.) (3) The while…break loop: It allows loop termination on a true condition anywhere in the loop. Syntax: while (1) statements if condition, break, end statements end Example: What will be the final x value? x = 8; while (1) x = x – 5; if x < 0, break, end end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.