Determine a and b for which f(x) = a sin(πx/2) + b cos(πx/2) fits the following data in the least-squares sense:-
(solve using MATLAB with steps).
x = −0.5, −0.19, 0.02, 0.20, 0.35, 0.50.
y =−3.558 , −2.874 , −1.995 , −1.040, −0.068, 0.677.
x = [-0.5, -0.19, 0.02, 0.20, 0.35, 0.50];
y = [-3.558, -2.874, -1.995 , -1.040, -0.068, 0.677];
ys = sum(y .* sin(pi*x/2));
yc = sum(y .* cos(pi*x/2));
s2 = sum(sin(pi*x/2).^2);
c2 = sum(cos(pi*x/2).^2);
sc = sum(sin(pi*x/2) .* cos(pi*x/2));
a = (ys*c2 - yc*sc) / (s2*c2 - sc^2);
b = (yc*s2 - ys*sc) / (s2*c2 - sc^2);
xx = -0.6:0.01:0.6;
yy = a*sin(pi*xx/2) + b*cos(pi*xx/2);
plot(xx, yy, x, y, 'o')
xlabel('x');
ylabel('y');
title('fit for f(x) = a sin(\pix/2) + b cos(\pix/2)')
str = sprintf('%+6.2f sin(\\pi x/2) %+6.2f cos(\\pi x/2)', a, b);
legend(str, 'data');
Comments
just write ur question, & they'll rely in few hours.
Leave a comment