Answer to Question #172833 in MatLAB for isak.

Question #172833

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.


1
Expert's answer
2021-03-18T10:51:44-0400
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');

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

isak
20.03.21, 19:31

just write ur question, & they'll rely in few hours.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS