John Federici NJIT Physics Department

Slides:



Advertisements
Similar presentations
Summary Statistics/Simple Graphs in SAS/EXCEL/JMP.
Advertisements

Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
2D Plots 1 ENGR 1181 MATLAB 12.
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
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.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4.
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:
EGR106 Week 6 MATLAB FILES Two Dimensional Plots Multiple Plots
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.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Review of last session The Weebly Dashboard The Weebly Dashboard Controls your account and your sites Controls your account and your sites From here you.
An introduction to Plotting in MATLAB Rikard Johansson Department of Biomedical Engineering (IMT) Linköping University
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Plotting II: By the end of this class you should be able to: Create a properly formatted engineering graph Create graphs of a function Place multiple plots.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Ranjeet Department of Physics & Astrophysics University of Delhi Working with Origin.
GUI development with Matlab: GUI Front Panel Components 1 GUI front panel components In this section, we will look at -GUI front panel components -Programming.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
MA/CS 3751 Fall 2002 Lecture 24. MA/CS 3752 ginput ginput is a Matlab function which takes one argument input: number of points to select in the image.
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays 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,
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Engineering MATLAB – 9 Plotting - 2 Agenda Formatting plots.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Lab 2 : potting to Matlab Networks.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
EEE 242 Computer Tools for Electrical Engineering
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.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials.
● Preparing data sets ● CTD Data from Tomales Bay ● Clean up ● Binning ● Combined Temperature-Depth plots ● T-S scatter plots ● Multiple plots on a single.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
John Federici NJIT Physics Department
Physics 114: Supplemental
EET 2259 Unit 11 Charts and Graphs
Computer Application in Engineering Design
Trivia Question Who invented LASIK Eye surgery? (a) Sheldon Cooper (b) Gholam Peyman (c) Arthur Schawlow and Charles Townes (d) Nikolay Basov and Alexander.
Lecture 25.
Ch3 Graphics Overview of Plotting Editing Plots
Lecture 25: Exploring data
Two-Dimensional Plots
OPSE 301: Lab13 Data Analysis – Fitting Data to Arbitrary Functions
MATLAB DENC 2533 ECADD LAB 9.
Net 222: Communications and networks fundamentals (Practical Part)
Net 222: Communications and networks fundamentals (Practical Part)
Statistical Analysis with Excel
MATLAB How to use (using M-files) Double click this icon
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
MATLAB How to use (using M-files) Double click this icon
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 Data with MATLAB
MATLAB How to use (using M-files)
Plotting Signals in MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Matlab
Matlab Basics Tutorial
EET 2259 Unit 11 Charts and Graphs
Presentation transcript:

John Federici NJIT Physics Department Physics 114: Lecture 2 More MatLAB Making plots, using functions Using HELP APPLICATION: Write a basic MatLAB program to plot generated data Defining Functions – Homework Submissions of MATLAB CODE John Federici NJIT Physics Department

TRIVIA QUESTION! Who sings the theme song for the “Big Bang Theory”? (a) Numeriklab (b) Ramin Djawadi (c) Justin Bieber (d) Barenaked Ladies (e) Joakim Lindström

TRIVIA QUESTION! Who sings the theme song for the “Big Bang Theory”? (a) NCIS - Numeriklab (b) Ramin Djawadi - Game of Thrones (composer) (c) Justin Bieber – The Karate Kid (d) Barenaked Ladies – BIG BAND THEORY (e) Joakim Lindström – The Walking Dead theme

The Basics of Plotting Data Within MATLAB, lets create some data and plot the data Indexing of Arrays Generating Functions Plot Function Row Column >> A(4,5)=17 A = 16 2 3 13 0 5 11 10 8 0 9 7 6 12 0 4 14 15 1 17 >> NOTE: Arrays are UNITY index based… First element is 1, 2nd element is 2, etc. Row Column

Indexing of Arrays You can select sub elements (or define sub-elements) of an array using the “:” operator as in start:end of a range A “wild card” meaning everything in the available range A = 16 2 3 13 0 5 11 10 8 0 9 7 6 12 0 4 14 15 1 17 >> A(1,1:3) ans = 16 2 3 Row 1, first THROUGH 3nd Column elements >> A(:,1)=0 A = 0 2 3 13 0 0 11 10 8 0 0 7 6 12 0 0 14 15 1 17 Set EVERY element in Column 1 to be zero

Indexing of Arrays You can also combine all elements of a 2D array into a 1D array. This manipulation is helpful later on in the course when we will statistically analyze 2D data. For example, images… Excellent reference for indexing of arrays: https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html >> A(:) ans = 1 2 3 “:” operator converts the multidimentional array into a VECTOR A = 1 2 3

Indexing of Vectors You easily define equally spaced vectors. This is handy for defining an ‘index’ vector or for defining an “x axis” variable of fixed spacing. >> i=1:10 i = 1 2 3 4 5 6 7 8 9 10 Default “step size” of 1 >> time=0:15:100 time = 0 15 30 45 60 75 90 Time values starting at zero, incrementing by 15 For each element upto last element equal to or less than 100

The Basics of Plotting Data Within MATLAB, lets create some data and plot the data Indexing of Arrays Generating Functions Plot Function MATLAB® provides a large number of functions that perform computational tasks. Functions are equivalent to subroutines or methods or Sub-VIs in other programming languages. If you more or less know WHAT function you need to perform, you can use the HELP search documentation within MATLAB. As an example, how do we find the MAXIMUM value in an array?

Submitting ‘max’ in documentation help

Generate a Plot with a Function >> x=-1:.01:1; >> y=sinc(x); >> plot(x,y) >> Define x variable between -1 and 1 with 0.01 step size Sinc function is Sin(x)/x Plot x versus y Matlab will open up a new window to plot the data such as on the left. Under the EDIT tab, you can copy the figure to the clipboard and PASTE into WORD, PPT, etc. You can also save the plot in a variety of formats

What is WRONG with this figure?

What is WRONG with this figure? 2D plots MUST have labels on each axis If the numbers on x or y axis are NOT dimensionless, they MUST have units. Arbitrary Units? Perhaps you need a TITLE… Is this figure for a paper in which there is a figure caption? Is the figure for a presentation? If more than one ‘curve’ of data, is there a legend to tell the data sets apart? Go into Matlab, search on ‘plot’ and see all of the options for changing color, line type (eg. dash-dot), marker symbol through the LineSpec parameter.

Adding labels and legend >> x=-1:.01:1; >> y=sinc(x); >> plot(x,y) >> xlabel('time (s)') >> ylabel('Voltage (mV)') >> legend('fake data by Prof. Federici') >> You can also edit these parameters, as well as others such as limits of axes, fonts, etc. from WITHIN the figure window in MATLAB. Open the EDIT tab and choose Axis Properties…. You can change figures PROGRAMATICALLY through the command window OR via the EDIT tab…. LEFT click on curve to select. Then RIGHT click to EDIT… Then under FILE choose GENERATE CODE

Changing Linestyle and Linewidth Demonstrate using EDIT FIGURE tab of FIGURE window, changing linetype time, color and width. Demonstrate using EDIT AXES tab changing Font size and color for different Axes. Click on MORE PROPERTIES and change LINEWIDTH of axes. Use the following code to change the linewidth of AXES programatically >> ax=gca; % retreives the current Axes style information >> ax.LineWidth = 5; % sets the linewidth of the axes to 5 pts

Command Line programming or Graphical User Interface (GUI)? You can also edit these parameters, as well as others such as limits of axes, fonts, etc. from WITHIN the figure window in MATLAB. Open the EDIT tab and choose Axis Properties…. You can change figures PROGRAMATICALLY through the command window OR via the EDIT tab Class discussion: Under what circumstances would you analyze data via LINE BY LINE processing in the command window OR via the GUI interface?

Cartoon break….

Adding Multiple Curves to a Plot >> x=-1:.01:1; >> y=sinc(x); >> z=sin(x-0.5); >> plot(x,y) >> hold on >> plot(x,z) >> hold off >> Plot x versus y “hold” plot so it does not erase with next plot command Plot x versus z “hold” off, so next plot command will overwrite the graphs. >> xz(1,:)=y; >> xz(2,:)=z; >> plot(x,xz) Alternatively, you can assign each ROW in an array to be the ‘y-axis’ data. Note: x-axis data for both curves must be same.

3D plots 3D plots are similar in syntax to generating 2D plots. Conceptually, you create a ‘grid’ of xi values which are used by function “f” to generate the corresponding yi values. For 3D plots, the same concept applies EXCEPT we need a 2D grid of xi and yj values meshgrid function [X,Y] = meshgrid(xgv,ygv) [X,Y,Z] = meshgrid(xgv,ygv,zgv) [X,Y] = meshgrid(gv) [X,Y,Z] = meshgrid(gv)

3D plots >> [x,y]=meshgrid(-2:.2:2); >> z=exp(-x.^2-y.^2); >> surf(x,y,z) WHY do you need the “.” before the “^” in the formula?

Course Policy on Submitting Matlab Code: If for any homework problem (eg. HW#2) or class project that requires you to submit Matlab code please use one of the following options: Make sure that the location of any input files is included in the file path in which Matlab searches (use PATHTOOL command in Matlab). (b) The submitted code via must be ‘stand-alone’. This means that if your instructor copies and pastes your code into Matlab it should run WITHOUT any errors. To ensure that this is true, when you download any data files from the course web page which will be imported into Matlab, DO NOT change the name(s) of any input files. Examples…..

Example of Standalone Code >> y=sinc(x); >> plot(x,y) >> xlabel('time (s)') >> ylabel('Voltage (mV)') >> legend('fake data by Prof. Federici')

Example of Import Data - Standalone Code >> IMdata=imread('Lecture4-Cartoon.jpg'); >> colormap gray(256) >> grayout=rgb2gray(IMdata); >> image(grayout) >> colorbar Name of file from course webpage As long as the file is in the PATH (use PATHTOOL command to set) of Matlab, this will be a ‘stand-alone’ code. You DO NOT need to include the exact path to the filename in the Matlab code.

Course Policy on Submitting Matlab Code: If you define any Matlab functions which are required for your code to run, you must include the functions as a Matlab file as an attachment to your homework. (d) Prior to submitting your HW assignment or other Matlab assignments, you should VERIFY that your Matlab code will run WITHOUT any execution errors. Example….

Defining a function Output parameters Input parameters function [y]=JffQuad(x,a,b,c) % MAKE SURE YOU ADD COMMENTS TO DOCUMENT YOUR FUnction! % evaluates quadradic polynomial % input parameters % a - coefficient of quadratic term % b - coefficient of linear term % c - constant offset term % % Output parameters % y given by y=a*x^2+b*x+c y=a.*x.^2+b.*x+c; end Save this Matlab code as JffQuad.m

Defining a function Define Input parameters Call the Function >> x=-5:0.02:5; >> a=1; b=2; c=3; >> y=JffQuad(x,a,b,c); >> plot(x,y) Call the Function

Course Policy on Submitting Matlab Code: (d) Prior to submitting your HW assignment or other Matlab assignments, you should VERIFY that your Matlab code will run WITHOUT any execution errors. After you save your scripts, make sure that code will run Clear WORKSPACE variables Make sure that FUNCTION file is in Matlab path Run main code or script. If you generate an “error” in running Matlab code, FIX IT! >> x=-5:0.02:5; >> a=1; b=2; c=3; >> y=JffQuad(x,a,b,c); >> plot(x,y) In submitting your HW, you need to include as attachments ANY functions which you define.

Examples from Student Submissions Student example which RUNS Student example which DOES NOT RUN

Class Exercise: Write a Simple Matlab program Use Matlab HELP or DOCUMENTATION SEARCH as needed Create a MATLAB program which Defines the ‘x’ range from 0 to 20. Divide this range into roughly 100 equally spaced intervals. Create data using the following function: y=sin(x) Create data using the following function: z=cos(x) Create data using the following function: a=sin(x) cos(x) Create a plot of y, z, and a versus x all on the same graph. Each curve should be a different color (say Red, Blue, and Black)