Working with Data in MATLAB

Slides:



Advertisements
Similar presentations
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,
Advertisements

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.
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.
Section 5.7 Numerical Integration. Approximations for integrals: Riemann Sums, Trapezoidal Rule, Simpson's Rule Riemann Sum: Trapezoidal Rule: Simpson’s.
ES 240: Scientific and Engineering Computation. InterpolationPolynomial  Definition –a function f(x) that can be written as a finite series of power functions.
An Anthropologist on Matlab. Data Analysis Functions (1) corrcoef(x) cov(x) cplxpair(x) cross(x,y) cumprod(x) cumsum(x) del2(A) diff(x) dot(x,y) gradient(Z,
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
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
Matlab intro The Environment
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.
Chapters 5 and 6: Numerical Integration
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.
Scientific Computing Linear Least Squares. Interpolation vs Approximation Recall: Given a set of (x,y) data points, Interpolation is the process of finding.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
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.
CHAPTER Continuity The Definite Integral animation  i=1 n f (x i * )  x f (x) xx Riemann Sum xi*xi* xixi x i+1.
Scientific Computing General Least Squares. Polynomial Least Squares Polynomial Least Squares: We assume that the class of functions is the class of all.
Math 15 Lecture 9 University of California, Merced Scilab A Short Introduction – No. 3 Today – Quiz #4.
Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und.
Date: 2.4 Real Zeros of Polynomial Functions
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
Real Zeros of Polynomial Functions
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.
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
Introduction to Programming for Mechanical Engineers
Mathematical Functions in Excel
Introduction to MATLAB
Finding Real Roots of Polynomial Equations 6-5
Warm Up Factor completely. 1. 2y3 + 4y2 – 30y 2y(y – 3)(y + 5)
Polynomial Long Division Review
Salinity Calibration fit with MATLAB
EGR 106 – Week 4 – Math on Arrays
MATLAB DENC 2533 ECADD LAB 9.
4.3 Division of Polynomials
Polynomial Functions and Models
Splash Screen.
MATH 493 Introduction to MATLAB
Find all solutions of the polynomial equation by factoring and using the quadratic formula. x = 0 {image}
Copyright © Cengage Learning. All rights reserved.
Code is on the Website Outline Comparison of Excel and R
Array and Matrix Functions
Copyright © Cengage Learning. All rights reserved.
Discrete Least Squares Approximation
Finding Real Roots of Polynomial Equations 3-5
Announcements P3 due today
Numerical Integration
EEE 244-3: MATRICES AND EQUATION SOLVING
Finding Real Roots of Polynomial Equations 6-5
Objectives Approximate a definite integral using the Trapezoidal Rule.
CHAPTER 3 Built-in MATLAB Functions
Presentation transcript:

Working with Data in MATLAB 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

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.

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”.

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).

fzero Example 3: Create an M-file called Example3.m with this script: 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.

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!

Working with Data Example 4: Let x = [5 2 1 6 3]. 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)

Working with Data Example 5: Let A = [5 2 1 6; -1 0 4 -3]. 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)

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?

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 = -2.5625.

Polynomials Recall that a polynomial is a function of the form p(x) = a0xn + a1xn-1 + … + an-1x + an where n is a non-negative integer and the ai 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) = x5+2x4-3x2+7x+12 is saved in MATLAB as p = [1 2 0 -3 7 12] Note that missing powers of x are included as 0’s in the vector. What polynomial would the vector r = [1 2 -3 7 12] represent in MATLAB? Answer: r(x) = x4 + 2x3 -3x2+7x+12.

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

Polynomials Example 7: Estimate the definite integral -22 (x5+2x4-3x2+7x+12)dx. Choose equal subinterval widths x = (b-a)/n and sample points xi* to be each subinterval’s midpoint, i.e. xi*=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 57.5931. What is the actual value of this integral? Answer: 288/5.

Polynomials – polyder Try this command with p = [1 2 0 -3 7 12] 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 = [1 2 0 -3 7 12] a = [1 1] b = [1 2 1] Do your results agree with calculations done by hand?

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 = [1 2 0 -3 7 12] K = 7 Do your results agree with calculations done by hand?

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 (xi,yi) for i = 1, 2, … , m.

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.

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.

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(‘toaddata.txt’) Year = toaddata(:,1)’ Area = toaddata(:,2)’

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!

References Using MATLAB in Calculus by Gary Jenson 22 22