Question

In: Advanced Math

Write a MATLAB code for discrete least squares trigonometric polynomial S3(x), using m = 4 for...

Write a MATLAB code for discrete least squares trigonometric polynomial S3(x), using m = 4 for f(x) = e^x * cos(2x) on the interval [-pi, pi]. Compute the error E(S3).

Solutions

Expert Solution


%%Matlab code for least square trigonometric polynomial
clear all
close all

%function for actual data
fun1=@(x) exp(x).*cos(2*x);
%function for trigonometric least square
fun2=@(x,a0,a1,a2,a3,a4,a5,a6,a7,a8) a0+a1.*cos(x)+a2.*sin(x)+a3.*cos(2*x)+a4.*sin(2*x)+a5.*cos(3*x)+a6.*sin(3*x)+a7.*cos(4*x)+a8.*sin(4*x);

%all data points
xx=linspace(-pi,pi);
yy=fun1(xx);
%plotting actual data
plot(xx,yy)
xlabel('x')
ylabel('f(x)')

%creating matrix for least square
for i=1:length(xx)
    A(i,1)=1;
    A(i,2)=cos(xx(i));
    A(i,3)=sin(xx(i));
    A(i,4)=cos(2*xx(i));
    A(i,5)=sin(2*xx(i));
    A(i,6)=cos(3*xx(i));
    A(i,7)=sin(3*xx(i));
    A(i,8)=cos(4*xx(i));
    A(i,9)=sin(4*xx(i));
    b(i,1)=yy(i);
end
  
%least square solution
cc=A\b;

fprintf('The coefficients are for least square solution\n')
for i=1:9
    fprintf('\t a%d=%f\n',i-1,cc(i))
end

%fitted data
yy1=fun2(xx,cc(1),cc(2),cc(3),cc(4),cc(5),cc(6),cc(7),cc(8),cc(9));
hold on
plot(xx,yy1)
legend('Actual data','fitted data','location','best')
fprintf('Error in Trigonometric least square solution is %f\n', norm(yy-yy1))
box on
%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%


Related Solutions

Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds), takes Fourier transform of the signal (probably FFT).
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
Prove that the solution of the discrete least squares problem is given by the orthogonal projection.
Prove that the solution of the discrete least squares problem is given by the orthogonal projection.
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
1. The coefficients of the least squares regression line, Y = M*X + B, are determined...
1. The coefficients of the least squares regression line, Y = M*X + B, are determined by minimizing the sum of the squares of the a) x‐coordinates. b) y‐coordinates. c) residuals ----------------------- 2 . Which of the following statements are true about overfitting and underfitting? a) Models that do not do well on training or test data are said to underfit the data. b) They lack enough independent variables to predict the response variable. c) A model’s generalization ability refers...
Consider the polynomial f(x) = 3x 3 + 5x 2 − 58x − 40. Using MATLAB....
Consider the polynomial f(x) = 3x 3 + 5x 2 − 58x − 40. Using MATLAB. Find the three roots of the polynomial, i.e, x where f(x) = 0, using Newton’s method. Report the number of iterations taken by each algorithm using a tolerance of 10−8 .
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
3) Derive the matrix equation used to solve for the coefficients for least-squares polynomial regression for...
3) Derive the matrix equation used to solve for the coefficients for least-squares polynomial regression for a quadratic model. 4) Derive the matrix equation used to solve for the coefficients for least-squares multiple linear regression for a function of 2 variables.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT