LFD Book Forum  

Go Back   LFD Book Forum > Course Discussions > Online LFD course > Create New Homework Problems

Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2012, 08:52 AM
Keith Keith is offline
Member
 
Join Date: Jul 2012
Posts: 16
Default 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}


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');
Reply With Quote
  #2  
Old 09-23-2012, 01:28 AM
yaser's Avatar
yaser yaser is offline
Caltech
 
Join Date: Aug 2009
Location: Pasadena, California, USA
Posts: 1,477
Default Re: Piecewise Linear Sine Function

Interesting problem! Thank you.
__________________
Where everyone thinks alike, no one thinks very much
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, 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.