%=========================================================================
%Description: In class example, how to LERP, and how to write a matlab script to do so
%             (what kinds of problems might you see, how to solve them)
%
%Author: Alex Simpkins, written with the cogsci 109 Fall 2006 class during
%        class
%=========================================================================

%we start by clearing all the variables
clear all

%then we create some data to interpolate...
%(this is arbitrary, we just made this data up)
x=0:2:10;
y=sin(x*pi/10);

%plot the data to see what it looks like, and format as red stars...
plot(x,y,'*r')


%first we tried it this way in class but had problems...
% for i=1:(length(x)-1),
%     for t=1:20,
%         xp(t)=(1-t)*x(i) + t*x(i+1);
%     end
% end

%we want to interpolate with an arbitrary number of points in between our
%real data, so we create the variable n, and if we wanted to fit 20 points
%between each pair of data points...
n=20;

%we create a parameter which goes from 0 to 1.  Recall we're going to
%create a parametric curve between each pair of points (piecewise
%continuous) that has the following equation:
%
% P(t) = (1-t)*P_(i) + t*P_(i+1)
%
% where P_i are the actual data points, and P(t) is our approximated curve
% Recall also that while creating the interval, we need to make it n-1 to
% create the right number of points (experiment with this in the command
% window to convince yourself)...
t=0:1/(n-1):1;

%we first create a loop to loop over all data points up to (1 - last
%point)...
for i=1:(length(x)-1),
    
    %within that loop we create each interpolated set of points, and store
    %them into a new variable for each dimension of P.  In this case we
    %have a 2D data set, so we have xp and yp.  Also we want n points
    %per pair of data points.  So if we have m data points, we'll have n*m
    %interpolated data points...
    for j=1:n,
        
        %The way we continually create more data points every time we loop
        %over i is that we create a small equation inside the variable
        %reference: [(i-1)*n+j]. 
        xp((i-1)*n+j)=(1-t(j))*x(i) + t(j)*x(i+1);
        yp((i-1)*n+j)=(1-t(j))*y(i) + t(j)*y(i+1);
        
    end

end

%now that we have our interpolated data, let's plot it over the original
%data set...

%this keeps the other data...
hold on;

%and plots our new data as green circles...
plot(xp,yp,'go')

