Presentation is loading. Please wait.

Presentation is loading. Please wait.

EPSII 59:006 Spring 2004. Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.

Similar presentations


Presentation on theme: "EPSII 59:006 Spring 2004. Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops."— Presentation transcript:

1 EPSII 59:006 Spring 2004

2 Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops While Loops User Functions

3 TextPad TextPad is a Convenient Tool in the CSS Windows Environment for Writing Code It Recognizes Syntax Error Automatically Can Save Files Corrected for the Unix Environment for Importing Check it out! (I know, it’s a little late)

4 Back to Matlab

5 If Statements if expression statements end Expression might be (B>1)  When B is a scalar greater than 1, the statements are executed  When B is an array, each element must be greater than 1 for the statements to be executed.

6 Relational Operators Relational Operator Interpretation <Less than <=Less than or equal to >Greater than >=Greater than or equal to ==Equal to ~=No equal to

7 Using Relational Operators Arrays are considered element by element and the result is an array of the same size with 1 for true and 0 for false A = [ 5 9 2], B = [6 8 3] A > B [0 1 0]

8 Logical Operators Matrices are again compared element by element, non-zero is true, zero is false For example, ~5 = 0 There is also an exclusive or operator: xor(first_matrix, second_matrix) Logical Operator Symbol Not~ And& Or|

9 Nested If Statements Just as in C, you can nest if statements  A = [1 2 3], B = [-1 0 1], C = zeros(1,3) if (A > B) if (A > 0) C = A + B end C = [0 2 4]

10 Else and Elseif Clauses What does this do? A = 3; if (A > 4) C = 4; elseif (A > 3) C = 3 else C = 0 end

11 Logical Functions any(x), returns 1 if any element is nonzero all(x), returns 1 if all elements are nonzero find(x), returns a vector of all nonzero elements isnan(x), returns 1 at any locations of NaN in a matrix finite(x), returns a matrix with 1’s for finite elements, zeros otherwise isempty(x), returns 1 for an empty matrix

12 For Loops Try to avoid using for loops in MATLAB programs, usually you can process information in a matrix arrangement for index = expression statements end If index is a scalar, executes once, if a vector, once for each element in the vector, if a matrix, once for each column (index will be set to the column vector) for k = 1:2:10 will execute 5 times with k = [1 3 5 7 9]

13 While loops while expression statements end Executes as long as the expression is true

14 Scripts We already talked about.m files If you define the.m file this way, it is a function that takes arguments and returns a value function d = dist(x) % Comments here print out when you % type ‘help dist’ d = sqrt(x * x’)

15 Revisit our two tank example Considering two tank example again. If the tanks are initially at steady state and a step change in from 0.05 to 0.06 at t=200 seconds is introduced, how do the tank levels change with time. Plot the tanks level vs. time for both systems. What is the new steady state value?

16 Example Problem The liquid levels in tank1 and tank 2 are given by T=200 q 1 increases

17 Example Continued

18 Example First create the M-file ‘step_tank.m’ contains the differential equation  function hdot = tank_1(t,h)  hdot=0.0;a1=0.05;A1=1.0;  if t<200 q1=0.05;else q1=0.06;end  hdot=(q1-a1*sqrt(h))/A1;(Note hdot is used to represent the derivative of h)

19 Example Next create the M-file ‘example3.m’ contains the code to solve the problem,  t0=0.0;tf=1000;h0=0.0;  [t,h]=ode23(@step_tank,[t0 tf],h0);  plot(t,h)  xlabel('time,seconds')  ylabel ('Liquid level,m')  title('Figure(3):Liquid Level in a tank')  grid on Run example.m

20 Introduction to Simulink The other method MATLAB uses to numerical differentiate (integrate) is via SIMULINK. SIMULINK is a program for simulating dynamic systems. It has two phases of use: model definition and model analysis. Thus you need to first define your model and then use modeling analysis to understand the system’s dynamic behavior.

21 Introduction to Simulink SIMULINK has a new class of windows called block diagram windows. After a model is defined, it can be analyzed either using the SIMULINK menus or using MATLAB's command window (workspace).

22 Introduction to Simulink Defining the model is easiest if one visualizes the loop for calculating the output as an approximate integration solution for small time steps. Use drag and drop to connect ‘blocks’ that represent, constants, integrals, operations, inputs and outputs. Easy to connect with lines.

23 Introduction to Simulink Starting SIMULINK At the MATLAB prompt type simulink or click the simulink button on the tool bar to open the main block library. From the SIMULINK window select New from the File menu. This will open an empty window where you can construct your model. Open one or more libraries, and drag the appropriate blocks (model the above diagram) into the active window. Connect the blocks by drawing lines between then using the mouse. Open the blocks (by double-clicking) and change their internal parameters. Block parameters can be any legal MATLAB expression. Save and re-name the system by choosing Save As from the File menu.

24 Introduction to Simulink Run a simulation by selecting Start from the Simulation menu. While a simulation is running the Start item becomes Stop. Adjust the simulation parameters by selecting Parameters from the Simulation menu. You can monitor the behavior of the system with scope block or you can use to work place block to send data to the MATLAB workspace and perform MATLAB functions on the results.

25 Introduction to Simulink As an example consider a single tank in previous example, where the time rate of change of the liquid level changes according to or

26 Introduction to Simulink Visualize the simulation method using small time steps and approximating the integral so that

27 Introduction to Simulink The block diagram for Simulink might be selected as follows.

28 Introduction to Simulink We obtained this by dragging over the integrator block, constant blocks, function block, etc. and then connecting them so that they result in the approximate integral shown above. Note that although SIMULINK uses the Laplace transform symbol for integration (1/s), SIMULINK is actually only approximating the solution and, thus, the integrator can be used with non-linear problems (such as this one). (Maybe they think 1/s is more beautiful than the integral sign.)

29 Introduction to Simulink The progress of simulation can be viewed while the simulation is running, and the final results can be made available in the MATLAB workspace when a simulation is completed.

30 Printing in Simulink The best way to obtain a hard copy of the graph you have generated or to print the block diagram of your model is to use the print provided by MATLAB. It has the following synopsis: print [-ddevice ] [-options ] [ filename ] Using print alone with no arguments send the contents of the current figure window to the default printer. While print filename saves the current figure to the designated file.

31 Printing in Simulink The following devices are available: -dps PostScript for black and white printers -dpscPostScript for color printers -dps2Level 2 PostScript, these are smaller and render more quickly when printing). -dlaserjetHP LaserJet. The following print options are supported: -append Append the graph to file, rather than overwriting. -S Name of SIMULINK model window to print. -P Specify the printer to use(UNIX only)


Download ppt "EPSII 59:006 Spring 2004. Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops."

Similar presentations


Ads by Google