# spline interpolation error , index in line 1

22 views (last 30 days)
Karolina Nowobilska on 27 Feb 2021
Commented: Walter Roberson on 27 Feb 2021
c0 = 47000000 ;
e0 = 6110000 ;
dt = 0.5 ;
tmax = 5;
t = 0:dt:tmax ;
gamma = 0.08;
u = 0.05;
v = 0.05;
e = 0.02;
a = 1;
b = 1;
a1 = 4;
b1 = 9;
phi = gamma+u+v+e;
xi = u + v;
e_t = e0 + b.*t - 0.5.*(xi.*a-e.*b).*t.^2;
e_t_1 = e0 + b1.*t - 0.5.*(xi.*a1-e.*b1).*t.^2;
spline = spline(t,e_t);
spline1 = spline(t,e_t_1);
plot(t, spline)
hold on
plot(t,e_t_1)
hi i'm fairly new on matlab and would like to interpolate my results to get an accurate graph. Im getting an error which says " Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in line 28 spline = spline(t,e_t); ". Would you be able to help me solve this issue? Also would it be more beneficial to use lagrange interpolation or spline? Thanks!

Walter Roberson on 27 Feb 2021
spline = spline(t,e_t);
After that line spline refers to an array not the function. Use a different variable name.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 27 Feb 2021
variable = spline(t,e_t);
That form of spline() returns a piecewise polynomial. In order to plot it, you need to use ppval() indicating the places you wish to plot at.
In your case you could probably use
variable = spline(t,e_t, t);
which will return the interpolated values.

R2018b

### Community Treasure Hunt

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

Start Hunting!

Translated by