MATLAB Answers

# creating Lagrange interpolation w.r.t. two arrays

5 views (last 30 days)
sermet on 25 Dec 2017
Edited: sermet on 26 Dec 2017
time=[t1 t2 t3];
data=[y1 y2 y3];
specific_time=tt;
% for specific time tt, computation of data as follows;
data(tt)=((tt-t2)*(tt-t3))/((t1-t2)*(t1-t3))*y1 + ((tt-t1)*(tt-t3))/((t2-t1)*(t2-t3))*y2 + ((tt-t1)*(tt-t2))/((t3-t1)*(t3-t2))*y3
How can I create data(tt) w.r.t. arbitrary number of time and data array?

#### 0 Comments

Sign in to comment.

### Answers (1)

ANKUR KUMAR on 25 Dec 2017
time=[t1 t2 t3];
data=[y1 y2 y3];
tt=[tt1 tt2 tt3 tt4 tt5 tt6 tt7];
for i=1:length(tt)
data(i)=((tt(i)-t2)*(tt(i)-t3))/((t1-t2)*(t1-t3))*y1 +...
((tt(i)-t1)*(tt(i)-t3))/((t2-t1)*(t2-t3))*y2 + ((tt(i)-t1)*(tt(i)-t2))/((t3-t1)*(t3-t2))*y3;
end
You can even replace t1 by time(1) and so on.

#### 3 Comments

sermet on 26 Dec 2017
t1,t2,t3 and y1,y2,y3 should be given as loop. Because their's number are variable.
ANKUR KUMAR on 26 Dec 2017
Is your problem resolved?
sermet on 26 Dec 2017
No. Your codes are just working for above example. When t and y vectors' length are taken into consideration (they are variable, i.e, changes w.r.t. example), they should be represented as t(i), y(i) within loop.

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!