Plot the polynomial y = 50 + 15x – 4x2 + 2x3 – 12x4 – 2x6 for x values between -2.8 and 2.8
Compute polynomial values directly
xx = -2.8 : 0.01 : 2.8; yy1 = 50 + 15*xx - 4*xx.^2 + 2*xx.^3 - 12*xx.^4 - 2*xx.^6;
Compute polynomial values using polyval
Represent polynomial as a vector of coefficients
coefs =
If polynomial has degree N, then length(coefs) is
Use polyval to evaluate the polynomial at desired x values
yy2 =
Given N data points, (xk,yk), find a smooth function f (e.g., to be able to find values between data points).
Interpolation:
for all data points (xk,yk),
Approximation:
for all data points (xk,yk),
Having found the smooth function, we now want to plot data values as distinct points and the interpolating/approximating function as a smooth curve.
x_data = x coordinates of data values y_data = y coordinates of data values xx = vector of closely spaced x values over range to plot yy = vector of y values obtained by evaluating the function at xx
Using one plot command:
Using multiple plot commands:
Given N data points, there is a unique polynomial of degree (at most)
Use polyfit( x_data , y_data , degree )
Example: Given data points (1, 1), (3, 3), and (5, 2), find the interpolating polynomial through these data points.
Estimate the value at x = 4:
Problem: high-degree polynomials
tend to oscillate wildly
Option 1:
Option 2:
yy = spline( x_data , y_data , xx )
Goal: minimize the error between
data points & approximating function
Idea: convert approximating function into something that looks like a polynomial
Example: