- 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
- 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)
Do'stlaringiz bilan baham: |