asked 69.8k views
1 vote
IN MATLab Write a function that, given a data set (xi, yi i = 1 . . . n), outputs the coefficients of a polynomial of order m = n−1 that interpolates the data. Test the function against two data sets of size n = 4 and n = 14, respectively. Let the x arrays be [1, 2, · · · , 4] and [1, 2, · · · , 14], respectively. You can randomly generate the y arrays using

asked
User Kkamenev
by
7.9k points

1 Answer

4 votes

Final answer:

To interpolate a polynomial in MATLAB, we can create a function that uses polyfit to obtain the coefficients for a polynomial of a specific order based on the given data points. This function can then be tested with two different data sets of specific sizes with randomly generated y values.

Step-by-step explanation:

To write a MATLAB function that outputs the coefficients of a polynomial of order m = n-1 that interpolates given data points (xi, yi i = 1 ... n), we can use the built-in polyfit function. Here is an example of such a function:

function coeffs = interpolatePolynomial(x, y) m = length(x) - 1; coeffs = polyfit(x, y, m); end

You can then test this function with two data sets. For example:

x1 = 1:4; y1 = rand(1, 4); x2 = 1:14; y2 = rand(1, 14); coeffs1 = interpolatePolynomial(x1, y1); coeffs2 = interpolatePolynomial(x2, y2);

Note that the y arrays are generated randomly, so residuals or errors would typically be calculated in a real-world scenario to evaluate the fit of the polynomial.

answered
User Joseph Turian
by
7.9k points