LFD Book Forum Piecewise Linear Sine Function
 Register FAQ Calendar Mark Forums Read

#1
09-21-2012, 08:52 AM
 Keith Member Join Date: Jul 2012 Posts: 16
Piecewise Linear Sine Function

Approximate the sine function in the first quadrant using a piecewise linear continuous function with two segments. Find the slope change point that minimizes the squared error difference between the piecewise linear function and its Fourier series fundamental sine component b1. The minimum is approximately:

a.) 0.014
b.) 0.018
c.) 0.023
d.) 0.027
e.) 0.030

Code:
samples = 100;
X = 0:1/samples:1;
Y = sin(X*pi/2);
xc = 0.6; % initial x value for slope change
yc = 0.8; % inital y value for slope change
% fundamental sine coefficient from Fourier series of PWL function
b1 = 8/(pi^2*(xc*(xc-1)))*(xc*sin(xc*pi/2)-yc*sin(xc*pi/2)+xc*yc-xc);
seg1 = yc/xc*X; % first linear segment samples
change = ceil(xc*samples); % index of slope change
seg1(change+1:samples+1) = 0; % remove seg1 above change point
seg2 = (1-yc)/(1-xc)*X+yc-xc*(1-yc)/(1-xc); % samples above change point
seg2(1:change) = 0; % remove seg2 up to change point
Ypwl = seg1+seg2; % combine pieces
error = (Y*b1-Ypwl).^2;
thd = sqrt(sum(error))/sqrt(sum((Y*b1).^2));
%
plot(X,Y*b1,'0',X,Ypwl,'1',X,error*100);
axis([0 1 0 1], "square");
grid on;
title('Piecewise Linear Sine Approximation');
legend('sin(x)','pwl(x)', '% error','Location', 'Northwest');
xlabel('X');ylabel('Y');
text(xc-0.04, yc+0.03, 'xc, yc');
text(1.01, b1, 'b1');
text(0.5,0.5, cstrcat('THD = ', num2str(thd)));
text(-0.03, yc-xc*(1-yc)/(1-xc),'a');