Download presentation

Presentation is loading. Please wait.

Published byLacey Else Modified over 4 years ago

1
259 Lecture 17 Working with Data in MATLAB

2
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

3
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

4
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

5
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

6
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

7
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

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

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

10
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

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

12
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 = [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) = x 4 + 2x 3 -3x 2 +7x+12. 12

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

14
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 57.5931. What is the actual value of this integral? Answer: 288/5. 14

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

16
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? 16

17
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

18
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

19
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

20
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

21
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
22 References Using MATLAB in Calculus by Gary Jenson 22

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google