Lecture on data values - or whatever algorithm you take, there is probably data that says you are handling it incorrectly. Let's open Matlab Type: x=[0:6]*pi/3; plot(x,sin(x)) y=sin(x); hold on; xmany=[0:200]*pi/3; ymany=sin(xmany); plot(xmany,ymany,'r'); See the difference: Could we have done better than "connect the dots': With 3 points one can fit a quadratic. So let's take first 3 points and see what happens at 1.5. We create the matrix- A(The Vander Monde matrix) whose columns have x^2 x and 1 with the first 3 points. Let y3= the first 3 points in y. We want to solve Ac =y. and then determine y(1.5) =x(1)^2*c(1)+x(1)*c(2) +c(3) in Matlab: A = Vander([ x(1) x(2) x(3)]) c=A\[y(1) y(2) y(3)]' y15=c(1)*1.5^2+c(2)*1.5+c(3) plot (1.5, y15,'ro') seems to be getting closer. look at sin(1.5) How about all 7 points: A7=Vander(x); Then set c7=A\y' calculate y715=c7(1)*1.5^6+ c7(2)*1.5^5 +c7(3)*1.5^4+ c7(4)*1.5^3 + c7(5)*1.5^2 + c7(6)*1.5 + c7(7) which gets y715 = 1.0024 >> sin(1.5) ans = 0.9975 >> sin(1.5)-y715 ans = -0.0049 Does taking more points always work?- If it did I would not be giving this lecture. Go to http://www.mste.uiuc.edu/exner/ncsa/runge/ and see it depends where you sampled function Thus polynomial interpolation is not even bitter than a linear fit. Another possibility- use splines Splines are (1) locally cubic (2) Given any 3 points x1, x2, x3 and data values y1, y2, y3. let p1 = cubic polynomial on (x1,x2] and p2 = cubic polynomial on [x2, x3) then p1(x2)=p2(x2)=y2 (3)p1'(x2)=p2'(x2) continuous derivative p1''(x2)=p2''(x2) continuous second derivative In matlab xx= 0:.25:6.28 yy=spline(x,y,xx) plot(xx,yy,'r') plot(xx,sin(xx),'g*') >> z=yy-sin(xx); >> max(z) ans = 0.0284 Look at www.mathworks.com/moler/interp.pdf p.14 On to taking derivatives: We know the derivative of sin(x) = cos(x) cos(x(2)) ans = 0.5000 (sin(x(3))-sin(x(2)))/(x(3)-x(2)) ans = 1.0602e-016 (sin(x(1))-sin(x(2)))/(x(1)-x(2)) ans = 0.8270 df=(y(3)-y(1))/(2*(x(3)-x(1))) df = 0.2067 All wrong- ok big steps. Is it better with spline data xx(5) ans = 1 >> (yy(5)-y(2))/(xx(5)-x(2)) ans = 0.4665 So a bit better. Does differences always work with sin(x) Consider h=.5; cos1=cos(1); while h >1.e-15 approx=(sin(1.+h)-sin(1.))/h; error=approx-cos1; disp([log(h) log(abs(error))]) h=h*.1; end which gives derivsin -0.6931 -1.4773 -2.9957 -3.8510 -5.2983 -6.1630 -7.6009 -8.4665 -9.9035 -10.7692 -12.2061 -13.0718 -14.5087 -15.3744 -16.8112 -17.6915 -19.1138 -18.0791 -21.4164 -15.6261 -23.7190 -13.7651 -26.0216 -13.6596 -28.3242 -8.7769 -30.6268 -7.2171 -32.9293 -4.2125 differentiation is an art not a science Integration is easier: divide intervals into trapezoidal regions and add the trapezoids: int1=x(2)*(y(1)+2*y(2)+2*y(3)+y(4))/2 int1 = 1.8138 true solution is 2. working with spline data: let's go until xx(13) = 3. Integral from 0 to 13 is 1+cos(xx(13)) = 1.99 Doing trapezoids gives us sum=yy(1)+yy(13) for i=2:12 sum=sum+2*yy(i); end integral1=sum*xx(2)/2 yielding sum=yy(1)+yy(13) for i=2:12 sum=sum+2*yy(i); end 1.9909