clear; clc; close ; format ; all

advertisement
clear; clc; close all; format shortg;
%Today we're going to learn to use matlab to work with polynomials
%In Matlab a polynomial is a vector of the coefficients of the polynomial
%Always start with the highest degree as the first entry, then work your
%way down to the constant as the last entry. Don't forget to add 0 for any
%power of x not shown.
%Example: p(x) = x^2 - 2
p = [1 0 -2]; %note that I included the '0' which is the coefficient
%of the x term.
%The command we'll use most today is the polyval command
%The polyval command evaluates a polynomail at a point or at set of points
x = [1:5];
y1 = polyval(p,2)
y2 = polyval(p,x)
%We can add and subtract polynomials by using adding their vectors
%But we need to be careful because when the degree of the polynomials
%are different then their vectors will have different lengths.
%We'll need to add 0's as place holders.
%Example: p1(x) = x^2 - 2 and p2(x) = x^3 + x^2 - x + 1
%p(x) = p1 + p2 = x^3 + 2x^2 - x - 1
p1 = [1 0 -2]; p2 = [1 1 -1 1];
p = [0 p1] + p2 %Here you can see since p1 was 1 degree smaller
%I had to add an extra 0 in 'x^3' place of the vector
%Multiplying polynomials has it's own command, conv(p1,p2)
%Example: p1(x) = x^2 - 2 and p2(x) = x^3 + x^2 - x + 1
%p(x) = p1*p2 = x^5 + x^4 - 3x^3 - x^2 + 2x -2
p = conv(p1,p2)
%Note I can only multiply 2 polynomials at a time.
%If I need to multiply more than 2 polynomials, I'll have to 'nest' the
%conv function
%Example: p1(x) = x^2 - 2, p2(x) = x^3 + x^2 - x + 1, p3(x) = x
%p(x) = p1*p2*p3 = x^6 + x^5 - 3x^4 - x^3 + 2x^2 -2x + 0
p3 = [1 0];
p = conv(p1,conv(p2,p3))
q = conv(conv(p1,p2),p3)
r1 = conv(p1,p2); r = conv(r1,p3)
%One of the most useful functions is the roots function
%The roots function creates a column vector of the real roots of a poly
%Example: p = x^2 - 2, the roots are 1.4142 and -1.4142
p = [1 0 -2];
x = roots(p)
%I can also calculate the derivative of a poly using the polyder command
%Example: p(x) = x^2 - 2
p1 = polyder(p)
1
%The other focus of this chapter is curve fitting.
%We'll work mostly with the polyfit command.
%The polyfit command allows us to fit a set of data with a polynomial of
%any degree smaller the length of the list.
%This means if I have 10 data points, I can use the polyfit command to give
%me the best fit polynomial of any degree between 1 and 9 for that data set
%Example: x = [1 2 3] and y = [1 3.9 9.1];
x = [1 2 3]; y = [1 3.9 9.1];
p1 = polyfit(x,y,1); p2 = polyfit(x,y,2);
%Then I can plot the two fits to compare them along with the original data
%points
t = [0:.1:4]; s1 = polyval(p1,t); s2 = polyval(p2,t);
plot(x,y,'*',t,s1,t,s2);
%A not so obvious use of the polyfit command is to determine the parameters
%in a non-polynomial fit such as fitting an exponential or reciprocal
%See page 271 and 272 for details on how to use a linear fit to calculate
%the constants needed.
%Lastly we'll look at interpolations
%An interpolation is an approximation of values inbetween data points
%Instead of trying to use every single point in a data set to approximate
%values, an interpolation only uses the closest points to determine the
%fit.
%The interp1 command has the #1 at the end of the command. It might look
%like an 'l' but it is the numeral 1.
%interp1(x,y,x0,'---') is the correct format of the command.
%x is the vector of x data points
%y is the vector of y data points
%x0 is the value we want approximated.
%x0 is can be a vector (i.e. we approximate at multiple points at once)
%'---' is the type of interpolation such as 'linear' or 'spline'
%See pages 275-276 for more information
%Example
x = [0:5]; y = [1 -.6242 -1.4707 3.2406 -.7366 -6.3717];
x0 = [0:.1:6];
y1 = interp1(x,y,x0,'linear');
y2 = interp1(x,y,x0,'spline');
figure;
plot(x,y,'*',x0,y1,x0,y2)
%Hints and other notes
%Look at the sample problems, they give really good hints to the problems
%today. Specifically look at sample problems 8-1 8-2 and 8-3.
%Look at the pages I referenced. If I didn't do something that you need to
%do it's probably got an easy to follow example in those pages.
%For problem 12 you might get too many roots. Ask yourself if all the roots
%given make sense for the box you're building.
%Use polyder and calc 1 ideas to find the max volume
2
y1 =
2
y2 =
-1
2
7
14
23
1
2
-1
-1
1
1
-3
-1
2
-2
1
1
-3
-1
2
-2
0
1
1
-3
-1
2
-2
0
1
1
-3
-1
2
-2
0
p =
p =
p =
q =
r =
x =
1.4142
-1.4142
p1 =
2
0
3
4
Published with MATLAB® R2014a
5
Download