In many situations, functions can be approximated as sums of sine and cosine fun
ID: 3863593 • Letter: I
Question
In many situations, functions can be approximated as sums of sine and cosine functions. in this question, you will approximate functions as sums of sine and cosine functions using least-squares linear regression. Given x- and y-data, you will fit a function of the form: f (x) = k + sigma^n_i = 1 a_i sin (ix) + sigma^n_i = 1 b_i cos (ix) where n is an integer that is greater than zero, and where tin* coefficients to fit are k, a_i, i = {1, 2, ellipsis, n}, and b_i, i = {1, 2, ellipsis, n}. Write a function with the following header: function [k, a, b] = my regression_sincos(x_data, y_data, n) where: x data and y data are two m times 1 arrays of class double that represent two-dimensional data. in other words, those column vectors represent a set of points of coordinates (x data (i), y_data (i)), i = {1, 2, ellipsis, m}. You can assume that, m > 1, and that all elements of x data and y data are different from NaN. Inf. and -Inf. n is a scalar of class double that represents n in liquation 1. You can assume that n is an integer such that n > theta. k is a scalar of class double that represents k in Equation 4. a and b are n times 1 arrays of class double such that a (i) represents a_i in Equation 1 and b (i) represents b_i in Equation 4. k, a, and b represent the coefficients of Equation 4, fitted to the x- and y-data represented by x data and y_data (respectively), using least-squares linear regression.Explanation / Answer
% Load the data in x and y variable
function [fitresult, gof] = createF(x, y, n)
[xData, yData] = prepareCurveData( x, y );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Lower = [-Inf -Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf Inf];
%fitlist = {'fourier1', 'fourier2', 'fourier3', 'fourier4', 'fourier5', 'fourier6', 'fourier7', 'fourier8'};
% Set up fittype and options.
if(n ==1)
ft = fittype( 'fourier1' );
elseif(n==2)
ft = fittype( 'fourier2' );
elseif(n==3)
ft = fittype( 'fourier3' );
elseif(n==4)
ft = fittype( 'fourier4' );
elseif(n==5)
ft = fittype( 'fourier5' );
elseif(n==6)
ft = fittype( 'fourier6' );
elseif(n==7)
ft = fittype( 'fourier7' );
elseif(n==8)
ft = fittype( 'fourier8' );
end
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
disp('fitresult')
disp(fitresult)
disp('gof')
disp(gof)
% Plot fit with data.
figure( 'Name', 'fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x' );
ylabel( 'y' );
grid on
end
Sample output for n = 4. Here, k is a0.
General model Fourier4:
ans(x) = a0 + a1*cos(x*w) + b1*sin(x*w) +
a2*cos(2*x*w) + b2*sin(2*x*w) + a3*cos(3*x*w) + b3*sin(3*x*w) +
a4*cos(4*x*w) + b4*sin(4*x*w)
Coefficients (with 95% confidence bounds):
a0 = -1.657e+22 (-1.993e+22, -1.32e+22)
a1 = 2.588e+22 (2.062e+22, 3.113e+22)
b1 = 6.235e+21 (4.966e+21, 7.505e+21)
a2 = -1.2e+22 (-1.443e+22, -9.56e+21)
b2 = -6.134e+21 (-7.383e+21, -4.885e+21)
a3 = 2.986e+21 (2.38e+21, 3.592e+21)
b3 = 2.557e+21 (2.036e+21, 3.077e+21)
a4 = -2.973e+20 (-3.575e+20, -2.37e+20)
b4 = -4.093e+20 (-4.926e+20, -3.26e+20)
w = 0.0006495 (0.0006492, 0.0006498)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.