MATLAB PolynomialMATLAB performs, polynomials as row vectors, including coefficients ordered by descending powers. For example, the equation P(x) = x^{4} + 7x^{3}  5x + 9 can be represented as  p = [1 7 0 5 9]; Polynomial FunctionspolyfitGiven two vector x and y, the command a=polyfit (x, y, n) fits a polynomial of order n through the data points (x_{i},y_{i}) and returns (n+1) coefficients of the power of x in the row vector a. The coefficients are arranged in the decreasing order of the power of x, i.e.,a=[a _{n} a _{n1}…a _{1} a _{0} ]. polyvalGiven a data vector x and the coefficients of a polynomial in a row vector a the command y=polyval(a, x) evaluates the polynomials at the data points x_{i} and generates the values y_{i} such that y_{i}=a(1) x_{n} ^{i}+a(2) x_{i} ^{(n1)}+…+a(n)x+a(n+1). Hence, the length of the vector a is n+1, and, consequently, the order of the evaluated polynomial is n. Thus if a is 5 elements long, the polynomial to be evaluated is automatically ascertained to be of the fourthorder. Both polyfit and polyval use an optimal argument if you need error estimates. Example: Straightline (linear) fitThe following data is collected from an experiment aimed at measuring the spring constant of a given spring. Different masses m are hung from the spring, and the corresponding deflections δ of the spring from its unstretched configuration are measured. From physics, we have that F=kδ and here F=mg. Thus, we can find k from the relationship k=mg/δ. Here, we are going to find k by plotting the experimental data, fitting the best straight line (we know that the relationship between δ and F is linear) through the data, and then measuring the slope of the bestfit line.
Fitting a straight line through the data means we want to find the polynomial coefficients a_{1} and a_{0}(a firstorder polynomial) such that a_{1} x_{i}+a_{0}gives the "best" estimate of y_{i}. In steps, we need to the following: Step1: Find the coefficients a_{k}' s: a=polyfit(x, y, 1) Step2: Evaluate y at finer (more closely spaced) x_{j}' s using the fitted polynomial: y_fitted=polyval(a, x_fine) Step3: Plot and see. Plot the given input as points and fitted data as a line: plot(x, y, 'o',x_fine, y_fitted); Following script file shows all the steps contained in making a straightline fit through the given data for the spring experiment and finding the spring constant. And plots the following graph: Least squares curve fittingThe procedure of least square curve fit can simply be implemented in MATLAB, because the technique results in a set of linear equations that need to be solved. Most of the curve fits are polynomial curve fits or exponential curve fits (including power laws, e.g., y=ax^{b}). Two most commonly used functions are:
1.ln (y)=ln (a) +bx or y =a_{0}+a_{1} x,where y =ln(y), a_{1}=b,and a_{0}=ln?(a). 2. ln(y)=ln(c) +d ln (x) or y = a_{0}+a_{1} x, where y = ln (y), a_{1}=d,and a_{0}=ln?(c). Now we can use polyfit in both methods with just firstorder polynomials to determine the unknown constants. The steps involved are the following: Step1: Develop new input: Develop new input vectors y and x, as allocate, by taking the log of the original input. For example, to fit the curve of the type y=ae^{bx}, create ybar=log(y) and leave x and to fit a curve of the type y=cx^{d}, create ybar=log(y) and xbar=log(x). Step2: Do a linear fit: Use polyfit to found the coefficients a_{0} and a_{1}for a linear curve fit. Step3: Plot the curve: From the curve fit coefficients, calculates the values of the original constants (e.g., a, b). Recomputed the values of y at the given x's according to the relationship obtained and plot the curve along with the original data. The following are the table shows the time versus pressure variation readings from a vacuum pump. We will fit a curve, P(t)=P_{0} e^{t/τ}, through the data, and determine the unknown constants P_{0} and τ.
By taking the log of both sides of the relationships, we have where P=ln(P), a_{1}=and a_{0}=ln?(P_{0} ). Thus, we can easily computed P_{0} and τ once we have a_{1} and a_{0}. Example: Create a script file And plots the following graph:
Next TopicMATLAB Interpolation
