1. ## MATLAB interpolation

i need to linearly interpolate a function... i have set up two arrays t=0:20 and tt=0:.1:20.... (ps. not allowed to use the matlab interpolation function) ...this is what i have written. obviously it is no good, but can someone further explain what i need to do or fix or point me in the right direciton? Thanks

t=0:20;
y=exp(-t/10).*cos(t);
tt=0:.1:20;
for i=1:floor(tt)
yy(i)=y(i)+((y(i+1)-y(i))./(t(i+1)-t(i))).*(t-t(i))
end

i know that a point t on the curve in this case is equal to floor(tt(i))... how do i index this into y(_)

2. The "find" command might work for what you are trying to do. Below demonstrates finding the two indexes that bracket the value 2.5. Obviously it is a trivial example.

Code:
EDU>> x = [0 1 1 2 3 5 8 13 21]

x =

0     1     1     2     3     5     8    13    21

EDU>> find(x<2.5,1,'last')

ans =

4

EDU>> find(x>2.5,1,'first')

ans =

5

EDU>>
Regards Elbarto

3. Originally Posted by plopony
i need to linearly interpolate a function... i have set up two arrays t=0:20 and tt=0:.1:20.... (ps. not allowed to use the matlab interpolation function) ...this is what i have written. obviously it is no good, but can someone further explain what i need to do or fix or point me in the right direciton? Thanks

t=0:20;
y=exp(-t/10).*cos(t);
tt=0:.1:20;
for i=1:floor(tt)
yy(i)=y(i)+((y(i+1)-y(i))./(t(i+1)-t(i))).*(t-t(i))
end

i know that a point t on the curve in this case is equal to floor(tt(i))... how do i index this into y(_)
Try this (warning - untested):

Code:
t=0:20;
y=exp(-t/10).*cos(t);
tt=0:.1:20;

for i=1:length(tt)
twrk=tt(i);

ihi=max(find(t<=twrk))+1;
if ihi>length(t);ihi=length(t);end
ilo=ihi-1;

yy(i)=y(ilo)+((y(ihi)-y(ilo))./(t(ihi)-t(ilo))).*(twrk-t(ilo))

end
CB