259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,

Slides:



Advertisements
Similar presentations
MathCAD Data.
Advertisements

Lecture 5.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Fixed point iterations and solution of non-linear functions
1 Chapter 13 Curve Fitting and Correlation This chapter will be concerned primarily with two separate but closely interrelated processes: (1) the fitting.
Introduction to Matlab
1 Eng. Mohamed El-Taher Eng. Ahmed Ibrahim. 2 1.FUNCTION SUMMARY polyfun  Polynomial functions are located in the MATLAB polyfun directory. For a complete.
Solving Quadratic Equations – Graphing Method
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
EGN 1006 – Introduction to Engineering Engineering Problem Solving and Excel.
E ngineering College of San Jose State University Engr.10 1 JKA & KY.
Polynomial Functions and Models Lesson 4.2. Review General polynomial formula a 0, a 1, …,a n are constant coefficients n is the degree of the polynomial.
ELECTRONIC SPREADSHEATS ELECTRONIC SPREADSHEATS Chapter 14 Dr. Bahaa Al-Sheikh & Eng. Mohammed AlSumady Intoduction to Engineering BME152.
Basics Of Spreadsheets Chapter Spreadsheet spreadsheet: grid of cells, each of which can contain text data or numeric data.
MATLAB - Basics Centro de Informática Universidade Federal de Pernambuco Aprendizagem de Máquina – IN1102 Arley Ristar –
Section 5.7 Numerical Integration. Approximations for integrals: Riemann Sums, Trapezoidal Rule, Simpson's Rule Riemann Sum: Trapezoidal Rule: Simpson’s.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Matlab Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you might find useful.
Part 4 Chapter 13 Linear Regression
Exam #3 Review: Comprehensive Exam Class 14.2 Palm Matlab Book Ch. 1-5.
NUMERICAL METHODS WITH C++ PROGRAMMING
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Excel Web App By: Ms. Fatima Shannag.
Unit 3: Lesson 1: 3.1 Polynomial Basics (4-1)
259 Lecture 16 Numerical Differentiation and Integration in MATLAB; Function M-files.
CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
159 Lecture 1 – Fall 2009 Introduction to Excel.
VOCAB REVIEW. letters at the top of the worksheet window that identify the vertical information in a worksheet column headings Click for the answer Next.
Ranjeet Department of Physics & Astrophysics University of Delhi Working with Origin.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
Homework questions thus far??? Section 4.10? 5.1? 5.2?
Curve Fitting and Regression EEE 244. Descriptive Statistics in MATLAB MATLAB has several built-in commands to compute and display descriptive statistics.
Advanced Topics- Polynomials
Integration Copyright © Cengage Learning. All rights reserved.
Learning Objectives for Section 13.4 The Definite Integral
ES 240: Scientific and Engineering Computation. Chapter 13: Linear Regression 13. 1: Statistical Review Uchechukwu Ofoegbu Temple University.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Scientific Computing General Least Squares. Polynomial Least Squares Polynomial Least Squares: We assume that the class of functions is the class of all.
Excel Web App By: Ms. Fatima Shannag.
Date: 2.4 Real Zeros of Polynomial Functions
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ICT Training Session #4 10 th February 2011 Using Microsoft Excel 2007  Exploring the home screen  Entering data  Formatting & sorting  Equations.
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Chapter 15 General Least Squares and Non- Linear.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Definite Integral df. f continuous function on [a,b]. Divide [a,b] into n equal subintervals of width Let be a sample point. Then the definite integral.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
259 Lecture 5 Spring 2016 Mathematical Functions in Excel.
 Polynomial – A monomial or a sum of monomials The standard form of a polynomial lists the terms in descending order of degree.
Announcements Topics: -sections 7.3 (definite integrals), 7.4 (FTC), and 7.5 (additional techniques of integration) * Read these sections and study solved.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Numerical Differentiation and Integration in MATLAB; Function M-files
EEE 244-3: MATRICES AND EQUATION SOLVING
Mathematical Functions in Excel
Working with Data in MATLAB
Salinity Calibration fit with MATLAB
259 Lecture 1 – Spring 2017 Introduction to Excel.
Lesson 2 Tables and Charts
MATLAB DENC 2533 ECADD LAB 9.
Copyright © Cengage Learning. All rights reserved.
Discrete Least Squares Approximation
Numerical Integration
EEE 244-3: MATRICES AND EQUATION SOLVING
Presentation transcript:

259 Lecture 17 Working with Data in MATLAB

Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum, min, max, length, mean, median, std, and sort  polyval, polyfit, polyder, polyint 2

fzero  One of MATLAB’s built-in functions is “fzero”.  fzero uses the Bisection Method to find a zero of a function f(x).  Syntax: fzero(‘f’,x0) where f is a function defined in MATLAB and x0 is either a starting value or an interval of the form [x1 x2].  In the second case, f(x1) and f(x2) must differ in sign. 3

fzero  Example 1: Try each of the following commands:  fzero('cos',1.5)  z1 = fzero('cos(x.^2)', [0 sqrt(pi)]) For Octave, to use this command, create a function M-file for cos(x.^2), such as “Sample2.m”. 4

fzero  Example 2: Plot the function y=Sample1(x) on the interval [-2,2].  Use the graph to pick a starting value x0 for fzero to locate the positive zero.  Then try z2 = fzero('Sample1',x0).  In MATLAB, the following should produce the same result:  z3 = fzero('x+x.^2- x.^4',x0). 5

fzero  Example 3: Create an M-file called Example3.m with this script:  x = -2:0.01:2;  plot(x, Sample1(x));  z = fzero('Sample1', [1 2]);  title('The positive zero of y = x+x^2-x^4');  grid on;  set(gca, 'Xtick', [-2:1 z 2]);  %This last command specifies tick-marks on the x- axis.  %Note that tick marks must be specified in ascending or descending order. 6

Working with Data  MATLAB has several functions that can be used to work with sets of data, including:  sum, min, max, length, mean, median, std, and sort  Use the Help file to learn more about each! 7

Working with Data  Example 4: Let x = [ ]. Enter vector x and find each of the following:  sum(x)  min(x)  [mx, indx] = min(x)  max(x)  [Mx, indx] = max(x)  length(x)  mean(x)  sum(x)/length(x)  std(x)  sort(x) 8

Working with Data  Example 5: Let A = [ ; ]. Enter matrix A and find each of the following:  sum(A)  min(A)  [mx, indx] = min(A)  max(A)  [Mx, indx] = max(A)  length(A)  mean(A)  sum(A)/length(A)  sum(A)/length(A(:,1))  std(A)  sort(A) 9

Working with Data  Example 5: Use the following MATLAB commands to locate the minimum of the function y = 2 cos(2x) – 3 cos(x) on the interval [0, 2]. x = linspace(0, 2*pi, 700); y = 2*cos(2*x)-3*cos(x); m = min(y)  Plot this function and see if the “MATLAB solution” agrees with the graph.  How else could we use MATLAB to solve this problem? 10

Working with Data  Example 5 (cont.)  One possible solution:  Find where the derivative of y = 2 cos(2x) – 3 cos(x) is equal to zero and use fzero! plot(x, -4*sin(2*x)+3*sin(x)) x1 = fzero('-4*sin(2*x)+3*sin(x)',[1 2]) y1 = 2*cos(2*x1)-3*cos(x1)  Each method yields a minimum of m =

Polynomials  Recall that a polynomial is a function of the form p(x) = a 0 x n + a 1 x n-1 + … + a n-1 x + a n where n is a non-negative integer and the a i are constants called coefficients. The degree of polynomial p(x) is n.  In MATLAB, a polynomial can be represented as a row vector containing the coefficients.  For example, the polynomial p(x) = x 5 +2x 4 -3x 2 +7x+12 is saved in MATLAB as p = [ ]  Note that missing powers of x are included as 0’s in the vector.  What polynomial would the vector r = [ ] represent in MATLAB?  Answer: r(x) = x 4 + 2x 3 -3x 2 +7x

Polynomials  To evaluate a polynomial, we use “polyval”.  Example 6: Try the following: p = [ ] polyval(p,3) %You should get 411. x = linspace(-2,2); y = polyval(p,x); plot(x,y) 13

Polynomials  Example 7: Estimate the definite integral  -2 2 x 5 +2x 4 -3x 2 +7x+12)dx.  Choose equal subinterval widths x = (b-a)/n and sample points x i * to be each subinterval’s midpoint, i.e. x i * =a+(2*i-1)*(b-a)/2n, for a=-2, b=2, and n=100.  Do this first with the polynomial’s formula. a=-2; b=2; n=100; dx = (b-a)/n; xstar = a+dx/2:dx:b-dx/2; Rn = sum(xstar.^5+2*xstar.^4-3*xstar.^2+7*xstar+12)*dx  Repeat with “polyval”. Rn = sum(polyval(p,xstar))*dx  In each case, you should get the approximation to be about  What is the actual value of this integral?  Answer: 288/5. 14

Polynomials – polyder  Using the command “polyder”, we can differentiate a polynomial, polynonial product, or polynomial quotient!  Syntax: “polyder(p)” returns the derivative of the polynomial whose coefficients are the elements of vector p. “[K] = polyder(a,b)” returns the derivative of polynomial a*b. “[Q,D]” = polyder(b,a) returns the derivative of the polynomial ratio b/a, represented as Q/D.  Try this command with  p = [ ]  a = [1 1]  b = [1 2 1]  Do your results agree with calculations done by hand? 15

Polynomials – polyint  Using the command “polyint”, we can integrate a polynomial!  Syntax: “polyint(p,K)” returns a polynomial representing the integral of polynomial p, using a scalar constant of integration K. “polyint(p)” assumes a constant of integration K=0.  Try this command with  p = [ ]  K = 7  Do your results agree with calculations done by hand? 16

Fitting Polynomials to Data  We can use the command “polyfit” to fit a polynomial to a set of data via a least- squares fit.  Syntax: p = polyfit(x, y, n) will fit a polynomial of degree n to data given as ordered pairs (x i,y i ) for i = 1, 2, …, m. 17

Fitting Polynomials to Data  Example 8: Let’s try with the Toad Data and a polynomial of degree 3!!!  Note: The Import Wizard feature that follows only works in MATLAB – for Octave create two vectors, Year and Area with the appropriate toad data as entries – we will see a way to do this in a couple slides!.  From our class web page, save the toaddata.txt file to your Desktop.  Within this file, rename the second column Area.  Next, in MATLAB, from the File menu, choose File-> Import Data and select the toaddata.txt file.  In the Import Wizard window that appears, click on comma for the Column Separator.  Click on Next. 18

Fitting Polynomials to Data  Example 8 (cont):  Click on Create vectors from each column using column names.  Click on Finish.  Two new vectors have been created – Year and Area. 19

Fitting Polynomials to Data  Another way to import data from a text file is via “load”.  Remove the headings “Year” and “Area” from the first line of the toaddata.txt file.  Then use the commands load(‘toadata.txt’) Year = toaddata(:,1)’ Area = toaddata(:,2)’ 20

Fitting Polynomials to Data  Example 8 (cont.)  Fit our cubic polynomial to the toad data with polyfit: p = polyfit(Year, Area, 3)  To show more decimal places for our coefficients, use format long  Plot the toad data along with the polynomial! plot(Year,Area,'r*',Year, polyval(p,Year),'b')  Compare to Mathematica’s Fit command or Excel’s trendline for a cubic polynomial.  We should get the same coefficients! 21

22 References  Using MATLAB in Calculus by Gary Jenson 22