LFD Book Forum

LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Create New Homework Problems (http://book.caltech.edu/bookforum/forumdisplay.php?f=139)
-   -   Piecewise Linear Sine Function (http://book.caltech.edu/bookforum/showthread.php?t=1659)

Keith 09-21-2012 07:52 AM

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 x_{c},y_{c} 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

f(\bold x)= b_{1} \sin \bold x \approx g(\bold x)=\left\{\begin{matrix}
m_{1} & 0  & \textup{for} \; \; 0 \leq  x \leq  x_{c} \\ 
m_{2} & a  & \textup{for} \; \; x_{c} < x  \leq 1 
\end{matrix}\right.


\textup{where} \; \; m_{1} = \frac{y_{c}}{x_{c}} ,\; \; m_{2} = \frac{1-y_{c}}{1-x_{c}},\; \; a = y_{c}-\frac{1-y_{c}}{1-x_{c}}x_{c}


\textup{and}\; \; b_{1} = \frac{8}{\pi^2 x_{c}(x_{c}-1)} \left [  x_{c} \sin\frac{\pi}{2}x_{c}-y_{c}\sin\frac{\pi}{2}x_{c}+x_{c}y_{c}-x_{c}\right ]


\textup{minimize} \;\; e(h(\bold x),f(\bold x))=\sqrt{[b_{1}\sin \bold x-h( \bold x)]^2}/\sqrt{(b_{1}\sin \bold x)^2}

http://ivyriver.net/images/pwl.png
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');


yaser 09-23-2012 12:28 AM

Re: Piecewise Linear Sine Function
 
Interesting problem! Thank you.


All times are GMT -7. The time now is 03:17 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.