Every time you make a change to a MATLAB M-file or a Simulink model file, you have to File Save before running the new simulation.

Similar documents
MatLab Basics. Now, press return to see what Matlab has stored as your variable x. You should see:

Module 2 Introduction to SIMULINK

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

MatLab - Systems of Differential Equations

Beginner s Matlab Tutorial

Green = 0,255,0 (Target Color for E.L. Gray Construction) CIELAB RGB Simulation Result for E.L. Gray Match (43,215,35) Equal Luminance Gray for Green

Plotting: Customizing the Graph

Introduction to Simulink

MATLAB. Input and Output

Week 2 Practical Objects and Turtles

MATLAB Workshop 14 - Plotting Data in MATLAB

Excel Tutorial. Bio 150B Excel Tutorial 1

Building a Simulink model for real-time analysis V Copyright g.tec medical engineering GmbH

Simple Programming in MATLAB. Plotting a graph using MATLAB involves three steps:

Updates to Graphing with Excel

This activity will show you how to draw graphs of algebraic functions in Excel.

Lab 3. GIS Data Entry and Editing.

Introduction to the Graphing Calculator

Command-induced Tracking Jitter Study I D. Clark November 24, 2009

The following is an overview of lessons included in the tutorial.

Personal Geodatabase 101

@ MAXSTOR= Note that it s tempting to use the auxiliary variables representing loga, logb or logc since, as we noted last time,

SIMULATIONS OF PARALLEL RESONANT CIRCUIT POWER ELECTRONICS COLORADO STATE UNIVERSITY

CD-ROM Appendix E: Matlab

What is a piper plot?

Tutorial 2 Online and offline Ship Visualization tool Table of Contents

mouse (or the option key on Macintosh) and move the mouse. You should see that you are able to zoom into and out of the scene.

Dealing with Data in Excel 2010

Graphic Objects and Loading Them into TGF2/MMF2

Formulas, Functions and Charts

How to program a Zumo Robot with Simulink

Excel -- Creating Charts

MATLAB Control System Toolbox Root Locus Design GUI

INDUSTRIAL AUTOMATION Interactive Graphical SCADA System INSIGHT AND OVERVIEW. IGSS Online Training. Exercise 8: Creating Templates

WebSphere Business Monitor V7.0 Business space dashboards

KaleidaGraph Quick Start Guide

EXCEL Tutorial: How to use EXCEL for Graphs and Calculations.

Summary of important mathematical operations and formulas (from first tutorial):

Graphing in excel on the Mac

You must have at least Editor access to your own mail database to run archiving.

CREATE A 3D MOVIE IN DIRECTOR

WebSphere Business Monitor V6.2 Business space dashboards

TERMINOLOGY DEFINITION DESCRIPTION. sent directly to employees 90 days prior to their Mid Year Check In and Peer Review

Experiment 1A: Excel Graphing Exercise

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

Lecture 2 Mathcad Basics

MICROSOFT POWERPOINT STEP BY STEP GUIDE

Solving ODEs in Matlab. BP205 M.Tremont

XMGrace Fancy characters and stuff

Describe the Create Profile dialog box. Discuss the Update Profile dialog box.examine the Annotate Profile dialog box.

ECE 495 Project 3: Shocker Actuator Subsystem and Website Design. Group 1: One Awesome Engineering

Calculator Notes for the TI-89, TI-92 Plus, and Voyage 200

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Lab 3: Introduction to Data Acquisition Cards

How to test and debug an ASP.NET application

Tutorial for Tracker and Supporting Software By David Chandler

Microsoft Excel 2010 Part 3: Advanced Excel

Appendix K Introduction to Microsoft Visual C++ 6.0

How to rotoscope in Adobe After Effects

Lesson 1 - Creating a Project

Introduction to LabVIEW for Control Design & Simulation Ricardo Dunia (NI), Eric Dean (NI), and Dr. Thomas Edgar (UT)

5. Tutorial. Starting FlashCut CNC

Working With Animation: Introduction to Flash

Microsoft Excel 2010 Tutorial

Monitor file integrity using MultiHasher

Simulink Getting Started Guide

Task Scheduler. Morgan N. Sandquist Developer: Gary Meyer Reviewer: Lauri Watts

Scicos is a Scilab toolbox included in the Scilab package. The Scicos editor can be opened by the scicos command

Using Microsoft Word. Working With Objects

Chapter 6. Formatting Text with Character Tags

The Center for Teaching, Learning, & Technology

The KaleidaGraph Guide to Curve Fitting

LESSON 7: IMPORTING AND VECTORIZING A BITMAP IMAGE

By Clicking on the Worksheet you are in an active Math Region. In order to insert a text region either go to INSERT -TEXT REGION or simply

CONSTRUCTING SINGLE-SUBJECT REVERSAL DESIGN GRAPHS USING MICROSOFT WORD : A COMPREHENSIVE TUTORIAL

Part 1 Foundations of object orientation

Gestation Period as a function of Lifespan

Scanning in Windows XP: Basics Learning guide

Introduction to MATLAB (Basics) Reference from: Azernikov Sergei

Lab 1. The Fourier Transform

How long is the vector? >> length(x) >> d=size(x) % What are the entries in the matrix d?

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Scatter Plots with Error Bars

Overview of the Adobe Flash Professional CS6 workspace

JCT ON DEMAND User Guide

Curve Fitting, Loglog Plots, and Semilog Plots 1

Real Time Monitor. A Real-Time Windows Operator Interface. DDE Compliant. (for remote data display)

Item Analysis for Key Validation Using MULTILOG. American Board of Internal Medicine Item Response Theory Course

Embroidery Fonts Plus ( EFP ) Tutorial Guide Version

PLOTTING DATA AND INTERPRETING GRAPHS

Publisher 2010 Cheat Sheet

Doing Multiple Regression with SPSS. In this case, we are interested in the Analyze options so we choose that menu. If gives us a number of choices:

50 TUGboat, Volume 31 (2010), No. 1. Plotting experimental data using pgfplots Joseph Wright

Importing from Tab-Delimited Files

File Management in the Catalog Software

In this article, learn how to create and manipulate masks through both the worksheet and graph window.

Data representation and analysis in Excel

5. Correlation. Open HeightWeight.sav. Take a moment to review the data file.

A Guide to Using Excel in Physics Lab

Transcription:

Getting started Lab 2 Tutorial: Running Simulink from a MATLAB M-file Set up a Simulink file to solve the ODE given by 1.5y& + y = 3u, where y(0) = 2 and u(t) is a unit step input. Save the model under the filename first_order.mdl. Your simulation file should look like: Every time you make a change to a MATLAB M-file or a Simulink model file, you have to File Save before running the new simulation.

The solution of the ODE should look like: y ss = 3 IC: y(0) = 2 To run the simulation from Matlab We need to create an M-file. In the MATLAB Command Window, select File New M-file.

Save the file that opens as tutorial_driver1.m. Type the following commands in the M-file. File Save. This file is the MATLAB program you will use to control your simulation, change variables, and gain greater control over plotting results. comment line filename of your Simulink model Run the program by typing its filename tutorial_driver1 (return) in the MATLAB Command Window. Select the Scope Window to see the simulation results. If you get an error message, read the next section.

Undefined function or variable error message (If your simulation ran correctly, continue to the next section.) If you type the filename in the MATLAB Command window, you might get an error message that says Undefined function... (see below):» tutorial_driver1??? Undefined function or variable 'tutorial_driver1'. Or you might get an error regarding the use of the sim command. First check that you spelled the filename correctly. If spelled correctly, you probably have to use the Set Path command to tell MATLAB the directory to look in so it can find your file. Go to File Set Path..., then select Browse to find the directory in which you saved your M- file. Once you ve selected the correct folder in the Path Browser window, select (highlight) the path name in the Current Directory space. Then from the menu, select Path Add to path..., select OK, and close the Path Browser. Repeat for the directory in which your Simulink models are stored. Return to the MATLAB Command window, type the filename of your M-file. Your file should run.

To change variables from the M-file Put variable names in the Simulink simulation diagram. Replace the gain with 1/tau. Change the IC on the integrator to the variable name y0. Change the magnitude of the step input to A. Don t forget to File Save.

Now, let s assign values to these variables in the M-file. Note: We ve selected different parameters than what we used before. File Save. Again, run the program by typing tutorial_driver1 in the MATLAB Command Window. The Scope should show a new plot, with a new IC and a new final value. new y ss = 4 new IC: y(0) = 3

Changing the simulation time span In the M-file, add a new argument to the sim command. The bracketed expression [0 20] tells Simulink to run the simulation for the time interval 0 t 20. The M-file should now look like: % M-file to run Simulink file clear; y0 = -3; tau = 2; A = 4; sim('first_order',[0 20]); % last line The solution should look as follows. Note that time now runs from 0 to 20 sec.

Plotting in MATLAB To bring the variables from the Simulink workspace into the MATLAB workspace for better control of plotting, we have to assign variable names to the output variables. In the Simulink window, delete the Scope block and replace it with a To Workspace block from the Sinks library. In the Block parameters window, change the name of the variable name to yout. Add a Clock from the Sources menu connected to a second To Workspace block. Name this variable time. You have now created two new variables, time and yout, which are available for manipulation in the MATLAB environment. You have to assign a structure-type to these two output variables. In the Block parameters window of both To Workspace blocks, set the Save format selector to Array format. See the example below. (In Matlab version 5, select the format called Matrix.)

Don t forget to File Save the Simulink model. Add a plot command to the M-file as follows. % M-file to run Simulink file clear; y0 = -3; tau = 2; A = 4; sim('first_order',[0 20]); plot(time, yout) % last line Don t forget to File Save the M-file.

Again, run the program by typing tutorial_driver1 in the MATLAB Command Window. The resulting plot is in the Figure window. Add a title and label the axes by adding the following commands to the M-file. % M-file to run Simulink file clear; y0 = -3; tau = 2; A = 4; sim('first_order',[0 20]); plot(time,yout) title('step response of first-order system with IC = -3') xlabel('time (s)') ylabel('output variable y(t) (no units)') % last line The plot is shown below.

Multiple curves on the same figure Suppose we want to compare simulation results of the same system with two different ICs. In case 1, y(0) = 3 (as before) and in case 2, y(0) = +6. Don t forget to File Save the M-file. % M-file to run Simulink file clear; y0 = -3; tau = 2; A = 4; sim('first_order',[0 20]); % run the simulation with IC = -3 t1 = time; % assign time to a new variable name y1 = yout; % assign yout to a new variable name y0 = 6; % assign a new IC sim('first_order',[0 20]); % run the simulation with IC = +6 t2 = time; % assign the new time to a new variable name y2 = yout; % assign the new yout to a new variable name plot(t1,y1,'r-',t2,y2,'b:') title('step response of first-order system with different ICs') xlabel('time (s)') ylabel('output variable y(t) (no units)') % last line

The resulting plot: On this plot the character fields 'r-' and 'b:' in the plot command line plot(t1,y1,'r-',t2,y2,'b:') were used to designate the solid red and dotted blue characteristics of the lines. Other colors, line types, and marker types which could have been specified in these character fields are included on the table below. Data markers Line types Colors Dot. Solid line - Black k Asterisk * Dashed line -- Blue b Cross x Dash-dotted line -. Cyan c Circle o Dotted line : Green g Plus sign + Magenta m Square s Red r Diamond d White w Five-pointed star p Yellow y Either a line type or marker type may be specified in any one character field, but not both. If no characters are specified, the default line type and color will be a solid and blue.

Some additional plotting commands: If you wish to overlay a background grid line structure to the plot use the commands >> grid on and >> grid off It is also useful to include a legend to identify the different lines of a multi-line plot. The legend command adds a dragable legend to the plot that identifies each of the lines. >>legend('line 1 description', 'line 2 description',...) Sometimes it will be useful to control the scale or extent of the axes. Use of the axis command gives control over the size or other aspects of the axes The following command show an example that explicitly define the axes range. >> axis ( [Xmin Xmax Ymin Ymax] ) For more information on each of these commands, refer to the MATLAB help command. Let's rerun the multi-line plot including some of these additional details. % M-file to run Simulink file clear; y0 = -3; tau = 2; A = 4; sim('first_order',[0 20]); % run the simulation with IC = -3 t1 = time; % assign time to a new variable name y1 = yout; % assign yout to a new variable name y0 = 6; % assign a new IC sim('first_order',[0 20]); % run the simulation with IC = +6 t2 = time; % assign the new time to a new variable name y2 = yout; % assign the new yout to a new variable name plot(t1,y1,'r-',t2,y2,'b:') title('step response of first-order system with different ICs') xlabel('time (s)') ylabel('output variable y(t) (no units)') grid on legend('ic = -3','IC = 6') axis([0 15-4 7]) % last line

The resulting plot: More than one figure on a page Here, we use the subplot command to create more than one figure on a page. Replace the plotting section of the M-file with the following. We ve added the subplot command and the axis command. Don t forget to File Save the M-file. subplot(221) plot(t1,y1,'r') title('step response of first-order system with IC = -3') xlabel('time (s)') ylabel('output variable y(t) (no units)') axis([0 20-3 6]) subplot(222) plot(t2,y2,'b') title('step response of first-order system with IC = 6') xlabel('time (s)') ylabel('output variable y(t) (no units)') axis([0 20-3 6])

Again, run the program by typing tutorial_driver1 in the MATLAB Command Window. The resulting plot is in the Figure window. Use the subplot(223) command to put a figure in this quadrant of the page. Use the subplot(224) command to put a figure in this quadrant of the page. The command subplot(221) tells MATLAB to set up a 2x2 grid of figures and to put the next plot in position 1. The command subplot(312) tells MATLAB to set up a 3x1 grid of figures and to put the next plot in position 2. For additional help with plotting in MATLAB, in the Command window, type help plot. Similarly, for help with any MATLAB command name, type help name.

Some simulation stuff Output jaggies If your output looks like the one below, it represents a poor numerical integration and it is not a good representation or prediction of system behavior. To correct the problem, you have to modify the parameters of the numerical integration. The first parameter to change is the maximum step size. In the Simulink window, select the Parameters menu as shown below.

The window that comes up is shown below. Change the Max step size from Auto to something smaller, for example, 0.01, or 1e-2 as shown. Rerun the simulation, and you get a response that looks much smoother (see below), and probably is a much better prediction of system behavior than the first one.

Stiff systems Sometimes (particularly in electromechanical systems) a system has both very fast and very slow time constants. Such systems are called stiff systems. In integrating the equations of motion of stiff systems the simulation results might not appear to be realistic. For example, you might get high-frequency oscillations or unstable behavior that doesn t make physical sense. The solution to your problem might be that you have to select a numerical-solver algorithm specialized for stiff systems. To do this, In the Simulink window, select the Parameters menu, and select one of the stiff solvers as shown below. Welcome to Matlab!