Fall 2006AE6382 Design Computing1 2D Plotting in Matlab Learning Objectives Discover how Matlab can be used to construct 2D plots. Topics Structure of.

Slides:



Advertisements
Similar presentations
Boyce/DiPrima 9th ed, Ch 2.4: Differences Between Linear and Nonlinear Equations Elementary Differential Equations and Boundary Value Problems, 9th edition,
Advertisements

Lecture 5.
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
1 Chapter 4 Curve Plotting with MATLAB MATLAB provides some very powerful features for plotting and labeling curves. These operations can be performed.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. All Rights Reserved. Lecture Slides Elementary Statistics Eleventh Edition and the Triola.
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
MATLAB’s extensive, device-independent plotting capabilities are one of its most powerful features. They make it very easy to plot any data at any time.
By Hrishikesh Gadre Session II Department of Mechanical Engineering Louisiana State University Engineering Equation Solver Tutorials.
MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:
1 Committed to Shaping the Next Generation of IT Experts. Chapter 3 – Graphs and Charts: Delivering a Message Robert Grauer and Maryann Barber Exploring.
1 Introduction to MatLab MatLab stands for Matrix Laboratory. As the name suggests most of the programming operations have as input or output a matrix.
EGR106 Week 4 Two Dimensional Plots Multiple Plots Plot Formatting Homework.
HossamTalaat - MATLAB Course - KSU - 21/1/24 1 IEEE Student Branch - College of Engineering - KSU Getting started with Simulink By Prof. Hossam Talaat.
Matlab intro The Environment
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5 Advanced.
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
CMPS 1371 Introduction to Computing for Engineers PLOTTING.
1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs,
„  1999 BG Mobasseri1 9/18/2015 June 2 GRAPHICS IN MATLAB- PART I BASIC PLOTTING.
MATLAB Plotting With MATLAB 1. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Lecture 10 2D plotting & curve fitting Subplots Other 2-D Plots Other 2-D Plots Curve fitting © 2007 Daniel Valentine. All rights reserved. Published by.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
1. Overview 2. plot in 2D 3. Plot in 3D 4. Other possible charts 5. Engineers: label your plots! 6. Plots & Polynomial Plotting 11.
Sept. 26, 2005 Lecture 6 - By Paul Lin 1 CPET 190 Lecture 6 Problem Solving with MATLAB
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Graphing Most people at one time or another during their careers will have to interpret data presented in graphical form. This means of presenting.
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Basis of Mathematical Modeling LECTURE 3 Numerical Analysis with MATLAB Dr. N.K. Sakhnenko, PhD, Professor Associate.
Program design and algorithm development We will consider the design of your own toolbox to be included among the toolboxes already available with your.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 2 in MATLAB Topics Covered: 1.More about 2-D plots  Placing multiple.
Introduction to Engineering MATLAB – 9 Plotting - 2 Agenda Formatting plots.
STROUD Worked examples and exercises are in the text 1 STROUD Worked examples and exercises are in the text Programme F4: Graphs PROGRAMME F4 GRAPHS.
MATLAB ® for Engineers, Holly Moore Fourth Edition, Global Edition © Pearson Education Limited 2015 All rights reserved. Figure 5.1 Simple Plot of Time.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Lab 2 : potting to Matlab Networks.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Fall 2006AE6382 Design Computing1 Scripts and Functions Matlab’s Command prompt is where you can enter commands to be executed immediately… –Just like.
EGR 106 – Week 5 – 2-D Plots Question : Why do engineers use plots? Answer : To analyze, visualize, and present data. Matlab has many useful plotting options.
Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials.
Creating a Plot The plot function can take one or more inputs; e.g. plot (x, y) or plot (y) Note that in the two-argument version, the vectors x and.
MATLAB LABORATORY SESSION
Lecture 25.
Lecture 25: Exploring data
Scripts and Functions Matlab’s Command prompt is where you can enter commands to be executed immediately… Just like a calculator You can see what you’ve.
Net 222: Communications and networks fundamentals (Practical Part)
Net 222: Communications and networks fundamentals (Practical Part)
Lecture 10 2D plotting & curve fitting
Warmup NO CALCULATORS Convert into radians or degrees.
MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some.
Plotting Signals in MATLAB
Presentation transcript:

Fall 2006AE6382 Design Computing1 2D Plotting in Matlab Learning Objectives Discover how Matlab can be used to construct 2D plots. Topics Structure of a 2D plot Command syntax Types of 2D plots Applications… Summary

Fall 2006AE6382 Design Computing2 Two Dimensional Plotting Learning Objectives –Understand the anatomy of a 2D plot –How to choose different plot types for best effects Topics –Construction of 2D plots using plot() –Modification and enhancements to the plot –Different types of 2D plots you can create

Fall 2006AE6382 Design Computing3 Background While numerical methods are the heart (and origin) of Matlab, graphics has become the major component since the release of Version 4. Version 6 adds to this legacy with refinements and new functions. Professional Matlab comes with two thick manuals: (a) basic commands & programming and (b) graphics The graphics capabilities are so broadly defined that we will be able to cover only a small part –we'll focus on graphics you will find immediately useful –we will point to some of the areas where you will find powerful new capabilities when you need them later Like all of us, you will find yourselves frequently looking up "help" or checking the manuals for graphics!

Fall 2006AE6382 Design Computing4 >> x=0:0.05:10*pi; >> y=exp(-.1.*x).*sin(x); >> plot(x,y) >> xlabel('X axis description') >> ylabel('Y axis description') >> title('Title for plot goes here') >> legend('Legend for graph') >> grid on Basic 2D Plotting The simplest kind of plot is a cartesian plot of (x,y) pairs defined by symbols or connected with lines NOTE #1: Reversing the x,y order (y,x) simply rotates the plot 90 degrees! NOTE #2: line(x,y) is similar to plot(x,y) but does not have additional options

Fall 2006AE6382 Design Computing5 Supporting Commands Several functions let you control the plot appearance –axis(): determines the axis scaling (see help for options) –hold on/off: controls whether or not the plot is erased before another plot is drawn (toggles if no argument given) >> x=0:0.1:2*pi; >> plot(x,sin(x)) >> axis ans = >> axis([ ]) >> x=0:0.1:2*pi; >> plot(x,sin(x)) >> hold on >> plot(x,cos(x))

Fall 2006AE6382 Design Computing6 Using Lines or Markers or Both… Plots must follow the following logic: –Lines: whenever plotting analytical functions like sin(x) where you can compute y for any value of x –Markers: whenever plotting discrete experimental data or whenever the data are known only discretely –Both: connecting markers with straight lines is appropriate when you want to show a sequence >> plot(x,cos(x),'r:') >> hold on >> plot(x,sin(x),'b--') >> x=0:.02:1; >> y=x.^1.5; >> yr=randn(size(x)); >> yy=y+0.1.*yr; >> plot(x,yy,'ro',x,yy) >> x=0:0.02:1; >> y=x.^1.5; >> yr=randn(size(x)); >> yy=y+0.1.*yr; >> plot(x,yy,'rx')

Fall 2006AE6382 Design Computing7 Using Both Markers & Lines Use lines to show analytical fit through discrete data >> x=0:.02:1; >> y=x.^1.5; >> yr=randn(size(x)); >> yy=y+0.1.*yr; >> plot(x,yy,'x') >> p=polyfit(x,yy,1) p = >> hold on >> plot(x,polyval(p,x),'r') >> x=0:0.2:2.*pi; >> y=sin(x); >> yr=randn(size(x)); >> plot(x,10.*y+yr,'ro') >> hold on >> plot(x,10.*y)

Fall 2006AE6382 Design Computing8 Plotting Multiple Curves Problem: How can you compare several curves? Let’s start with the following: We could plot these using: >> X = 0.0:pi/100:2*pi; >> Y1 = cos(X); >> Y2 = 3*cos(X); >> Y3 = cos(2*X); >> Y4 = sin(X); >> plot(X,Y1) >> hold on >> plot(X,Y2) >> plot(X,Y3) >> plot(X,Y4)

Fall 2006AE6382 Design Computing9 Plotting Multiple Curves (cont’d) Or we could do: Or we could do this: What if we did this? Do a “help plot” for more markers. How could we see the data points more distinctly? >> plot(X,Y1,X,Y2,X,Y3,X,Y4) >> Z = [Y1;Y2;Y3;Y4]; >> plot(X,Z) >> plot(X, Z, 'o')

Fall 2006AE6382 Design Computing10 Using 2 Y-axis Scales Sometimes it is useful to plot two curves with widely different y-axis scales >> x=0:0.1:3.*pi; >> y1=sin(x+0.5); >> y2=90.*sin(x-0.5); >> plotyy(x,y1,x,y2) >> x=0:0.1:3.*pi; >> y1=sin(x+0.5); >> y2=90.*sin(x-0.5); >> plot(x,y1,x,y2) NOTE: it is complicated to label the 2nd axis…

Fall 2006AE6382 Design Computing11 Basic Plot Commands axis - freezes current axis scaling axis([xmin, xmax, ymin, ymax]) – sets axis limit values (note use of [ ] ) axis off – turns off display of axes (plot unchanged) axis on – turns on display of axes grid on/off – turns on/off display of a grid text(x,y,‘string’) - places horizontal text starting at (x,y) gtext(‘string’) – places horizontal text starting wherever user clicks with mouse line(x,y) – adds line specified by x & y vectors

Fall 2006AE6382 Design Computing12 Example of Log Plots Using a log scale can reveal large dynamic ranges >> x=linspace(.1,10,1000); >> damp=0.05; >> y=1./sqrt((1-x.^2).^2 + (2.*damp.*x).^2); >> plot(x,y) >> semilogx(x,y) >> loglog(x,y) Describes the behavior of vibrating systems

Fall 2006AE6382 Design Computing13 Subplot Command There are times when it is better to create several smaller plots arranged in some kind of grid; subplot(m,n,k) does this… –m=rows, n=columns in the grid –k=current focus (numbered row-wise) Let’s define a 2x3 subplot grid for: subplot(2,3,1) with the focus on the first plot

Fall 2006AE6382 Design Computing14 On Your Own: Putting it all together… What does grid do? What’s the quickest way to execute this code? X=0:0.5:50; Y=5*x.^2; subplot(2,2,1), plot(X,Y), title(‘Polynomial – Linear/Linear’),... ylabel(‘y’), grid subplot(2,2,2), semilogx(X,Y), title(‘Polynomial – Log/Linear’),... ylabel(‘y’), grid subplot(2,2,3), semilogy(X,Y), title(‘Polynomial – Linear/Log’),... ylabel(‘y’), grid subplot(2,2,4), loglog(X,Y), title(‘Polynomial – Log/Log’),... ylabel(‘y’), grid

Fall 2006AE6382 Design Computing15 Specialized 2D Plots There are a number of other specialized 2D plots –area(x,y): builds a stacked area plot –pie(): creates a pie chart (with options) –bar(x,y): creates a vertical bar chart (with many options) –stairs(x,y): similar to bar() but shows only outline –errorbar(x,y,e): plots x vs y with error bars defined by e –scatter(x,y): creates a scatter plot with options for markers –semilogx(x,y): plots x vs y with x using a log scaling –semilogy(x,y): plots x vs y with y using a log scaling –loglog(x,y): plots x vs y using log scale for both axes –And many others… (explore these yourself; you may find a good use in a later course) –Chapter 25 in Mastering Matlab is a good starting point.

Fall 2006AE6382 Design Computing16 A Special M-function: feval Sometimes you may want to pass along the NAME of a function that you want another function to use… –Suppose you have an M-function called, fmax, and it finds the maximum of a given function, like the one shown below. –Suppose you invoke fmax as: [f, x]=fmax(x0, x1) where f is the max value which occurs at x in the range x0  x1 –How do you tell fmax to use myfunc(x) which defines f(x)? f(x) x f(x)=myfunc(x)

Fall 2006AE6382 Design Computing17 feval - continued Use the feval function: a = feval(‘myfunc’, x) Is equivalent to: a = myfun(x) » y=sin(pi/4) y = » yy=feval('sin', pi/4) yy = NOTE: feval shouldn't be used to replace normal function calls…

Fall 2006AE6382 Design Computing18 Use of feval in an M-function Let's create an M-function to evaluate an arbitrary function that we will define with another M-function –call it " evaluate " –see listing below (or help) for calling syntax To use this, we will need to write another M-file to define our own special function. –myfun will contain the function name as a string (or explicit call) –we must specify the start, stop values and number of points

Fall 2006AE6382 Design Computing19 Using Your New Function: evaluate() Here is a particular user- defined function to evaluate over the specified range of x values Executing: And here is the graphical result… >> [x,y]=evaluate('trig',0,5.*pi,100); >> plot(x,y), xlabel('x'), ylabel('y') >>

Fall 2006AE6382 Design Computing20 A More Complicated Example Suppose we want to find the minimum of a function –Let's use the "humps" function in Matlab (rather than writing one) –Let's use Matlab's powerful fminbnd function >> x=-0.5:0.05:2; >> y=humps(x); >> plot(x,y), xlabel('x'), ylabel('y') Minimum

Fall 2006AE6382 Design Computing21 Using fminbnd() See help fminbnd and the textbook for many more details about this function… On your own: figure out how to find zeros (roots) >> x=-0.5:0.05:2; >> y=humps(x); >> plot(x,y), xlabel('x'), ylabel('y') >> >> [x,y]=fminbnd('humps', 0.4, 1.0) x = y = >> hold on >> plot(x,y,'ro') >> NOTE: maxima of f(x) are minima of -f(x)

Fall 2006AE6382 Design Computing22 A Note on How Arguments are Passed When arguments are passed into a function: –only the reference to the argument variable is provided the function –the values themselves remain in their home workspace –this avoids moving large amounts of data ( Slow !) If your function modifies an argument… –Matlab will immediately copy the argument variable into the function workspace (the whole array) –HOWEVER: any changes you make will NEVER be seen in the calling workspace because it is never copied back there! SOLUTION: if you need to make a change in an argument variable, you MUST include it in the list of returned variables to make it available to the calling workspace! input copy into function workspace and modify return

Fall 2006AE6382 Design Computing23 Application: Solving ODE’s Matlab includes a number of functions to solve Ordinary Differential Equations (ODE’s), including Initial Value Problems (IVP’s), Boundary Value Problems (BVP’s) and Partial Differential Equations (PDE’) Let’s consider a simple IVP in the form of a familiar ODE (an sdof vibration problem): Matlab’s ode23() and ode(45) functions use the Runge-Kutta-Fehlberg method to solve ODE’s expressed as: This is advanced material you will cover in your next math classes. Matlab will be there when you need it!

Fall 2006AE6382 Design Computing24 Solving an ODE: setup We can convert an N th order ODE into N first order ODE’s using a simple algorithm: In more compact forms: or

Fall 2006AE6382 Design Computing25 Matlab ode45( ) Syntax [T,Y] are the returned values and each row defines a value of t where the solution is computed along with the corresponding solutions, y i, in successive columns. But we need to provide a function to compute f(t,y) whenever ode45( ) needs it… We also need to specify the start and end times and initial conditions in the arguments to ode45( ) >> help ode45 ODE45 Solve non-stiff differential equations, medium order method. [T,Y] = ODE45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates the system of differential equations y' = f(t,y) from time T0 to TFINAL with initial conditions Y0. Function ODEFUN(T,Y) must return a column vector corresponding to f(t,y). Each row in the solution array Y corresponds to a time returned in the column vector T. To obtain solutions at specific times T0,T1,...,TFINAL (all increasing or all decreasing), use TSPAN = [T0 T1... TFINAL]. (truncated)

Fall 2006AE6382 Design Computing26 rhs( ) function for ode45( ) m-function will compute f(t,y) for ode45( ) returns the RHS column vector NOTE: If you need to pass parameter values to compute the RHS (e.g, zeta or g(t)), these can be added to the ode45( ) function call (see help ode45 )

Fall 2006AE6382 Design Computing27 Solving the Problem… Note the sizes of the returned variables You can plot either column of yy as needed How would you construct a phase plane plot (e.g., y versus y’)? Note the sizes of the returned variables You can plot either column of yy as needed How would you construct a phase plane plot (e.g., y versus y’)? >> [tt,yy]=ode45('rhs', [0 35],[1 0]'); >> whos Name Size Bytes Class tt 205x double array yy 205x double array Grand total is 615 elements using 4920 bytes >> plot(tt,yy(:,1)) See help ode45 for more options You supply this m-function…

Fall 2006AE6382 Design Computing28 A More Interesting RHS… Note how g(t) is formed here Result is familiar square pulse with ringing oscillations

Fall 2006AE6382 Design Computing29 Summary Review questions –Describe 2D plotting in MATLAB, –What is a figure? What is an axis? –How do you create a plot of two curves? More than 2 curves? –What is a legend and how is it created? –What kinds of 2D plots are available? Action Items –Review the lecture and run all demos –Look over the help for the plot, axis, label and figure m-functions –Try constructing a polar plot