0

I am doing a project where i find an approximation of the Sine function, using the Least Squares method. Also i can use 12 values of my own choice.Since i couldn't figure out how to solve it i thought of using Taylor's series for Sine and then solving it as a polynomial of order 5. Here is my code :

%% Find the sine of the 12 known values
x=[0,pi/8,pi/4,7*pi/2,3*pi/4,pi,4*pi/11,3*pi/2,2*pi,5*pi/4,3*pi/8,12*pi/20];
y=zeros(12,1);
for i=1:12
    y=sin(x);
end
n=12;
j=5;
%% Find the sums to populate the matrix A and matrix B
s1=sum(x);s2=sum(x.^2);
s3=sum(x.^3);s4=sum(x.^4);
s5=sum(x.^5);s6=sum(x.^6);
s7=sum(x.^7);s8=sum(x.^8);
s9=sum(x.^9);s10=sum(x.^10);
sy=sum(y);
sxy=sum(x.*y);
sxy2=sum( (x.^2).*y);
sxy3=sum( (x.^3).*y);
sxy4=sum( (x.^4).*y);
sxy5=sum( (x.^5).*y);
A=[n,s1,s2,s3,s4,s5;s1,s2,s3,s4,s5,s6;s2,s3,s4,s5,s6,s7;
    s3,s4,s5,s6,s7,s8;s4,s5,s6,s7,s8,s9;s5,s6,s7,s8,s9,s10];
B=[sy;sxy;sxy2;sxy3;sxy4;sxy5];

Then at matlab i get this result

>> a=A^-1*B
a =
   -0.0248
    1.2203
   -0.2351
   -0.1408
    0.0364
   -0.0021

However when i try to replace the values of a in the taylor series and solve f.e t=pi/2 i get wrong results

>> t=pi/2;
fun=t-t^3*a(4)+a(6)*t^5
fun =
    2.0967

I am doing something wrong when i replace the values of a matrix in the Taylor series or is my initial thought flawed ?

Note: i can't use any built-in function

M.Achilles
  • 93
  • 1
  • 2
  • 10
  • Can you write down your original thought - it reminds me on a [Vandermonde matrix](http://en.wikipedia.org/wiki/Vandermonde_matrix) but additionally you have all these sums. – bdecaf Jan 22 '14 at 11:32
  • I used this as a guide [link](http://kobus.ca/seminars/ugrad/NM5_curve_s02.pdf) , if that's what you are asking. If not , tell me so i can explain better – M.Achilles Jan 22 '14 at 11:42
  • ah I see. Then your test function is wrong - you need to use all the `a`s. – bdecaf Jan 22 '14 at 11:50
  • Related/helpful: http://stackoverflow.com/questions/19514315/how-can-i-fit-a-cosine-function/19518367#19518367 – Rody Oldenhuis Jan 22 '14 at 11:57
  • @bdecaf How can i use all the `a`s in the test? Can u explain ? @Rody Oldenhuis i'll check that, thx – M.Achilles Jan 22 '14 at 12:16
  • fun is $\sum_{i=0} a_i x^i$ and not just using a(4) and a(6) – bdecaf Jan 22 '14 at 12:18

1 Answers1

0

If you need a least-squares approximation, simply decide on a fixed interval that you want to approximate on and generate some x abscissae on that interval (possibly equally spaced abscissae using linspace - or non-uniformly spaced as you have in your example). Then evaluate your sine function at each point such that you have

y = sin(x)

Then simply use the polyfit function (documented here) to obtain least squares parameters

b = polyfit(x,y,n)

where n is the degree of the polynomial you want to approximate. You can then use polyval (documented here) to obtain the values of your approximation at other values of x.

EDIT: As you can't use polyfit you can generate the Vandermonde matrix for the least-squares approximation directly (the below assumes x is a row vector).

A = ones(length(x),1);
x = x';
for i=1:n
    A = [A x.^i];
end

then simply obtain the least squares parameters using

b = A\y;

You can clearly optimise the clumsy Vandermonde generation loop above I have just written to illustrate the concept. For better numerical stability you would also be better to use a nice orthogonal polynomial system like Chebyshev polynomials of the first kind. If you are not even allowed to use the matrix divide \ function then you will need to code up your own implementation of a QR factorisation and solve the system that way (or some other numerically stable method).

mathematician1975
  • 21,161
  • 6
  • 59
  • 101