How to resolve interp1 error?
Show older comments
function xinit = xinit_fcn(t)
t_opt=linspace(0,4452*0.001,4452)';
Tf=4.4520;
x1_opt= -(pi/Tf)*t + pi;
x2_opt=[0,0];
x3_opt=[0,0];
x4_opt=[0,0];
xinit = [interp1(t_opt,x1_opt,t);
interp1(t_opt,x2_opt,t);
interp1(t_opt,x3_opt,t);
interp1(t_opt,x4_opt,t);] ;
I'm getting the error:
Error using interp1>reshapeAndSortXandV (line 423)
X and V must be of the same length.
Error in interp1 (line 92)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in void>xinit_fcn (line 51)
xinit=[interp1(t_opt,x1_opt,t);
1 Comment
Walter Roberson
on 12 Jun 2018
One thing (but probably not the reason for the error) is I suspect you want
t_opt=linspace(0,4452*0.001,4453)';
instead of 4452 there. Better yet would be
t_opt = (0:4452)/1000;
Answers (2)
Jan
on 12 Jun 2018
% Failing:
t_opt = linspace(0,4452*0.001,4452)';
x1_opt = -(pi/Tf)*t + pi;
xinit = interp1(t_opt, x1_opt, t);
Now x1_opt has the same number of elements as t. As the error message tells, interp1 needs the 1st and 2nd input to have the same number of elements, not the 2nd and the 3rd. So maybe you want:
xinit = interp1(t, x1_opt, t_opt);
Or
x1_opt = -(pi/Tf) * t_opt + pi;
Perhaps the hard coded limit of 4452 is a bad idea and you need to let the limits depend on the length of the input t.
The solution is hard to guess, because you did not mention the intention of the code.
9 Comments
SARNENDU JATI
on 13 Jun 2018
Jan
on 13 Jun 2018
Use the debugger to find out, which variables have which size. See:
t = 0:0.001:4.452;
size(t)
t_opt = linspace(0,4452*0.001,4452)'
size(t_opt)
I do not understand why you want to interpolate at all here. But the actual error seems to be simple. The message
X and V must be of the same length.
is clear. So care for the sizes of these two inputs.
SARNENDU JATI
on 14 Jun 2018
Walter Roberson
on 14 Jun 2018
length(t) is 4453, not 4452.... just as I suggested might be the problem.
Jan
on 14 Jun 2018
@SARNENDU JATI: You mentioned, that you cared for X and V having the same size. In the code you have posted this is not the fact and the error message tells you clearly that they have different sizes. So please explain, why you think, that these vectors have matching sizes?
Post the output of these commands:
t = 0:0.001:4.452;
size(t)
t_opt = linspace(0,4452*0.001,4452)'
size(t_opt)
Use the debugger also. Type e.g. this in the command window:
dbstop if error
Now run the code again until Matlab stops at the error. Now check the sizes of the inputs of interp1.
SARNENDU JATI
on 14 Jun 2018
Walter Roberson
on 14 Jun 2018
(I am away from my desk for a few hours and will not be able to test until later)
Jan
on 14 Jun 2018
@SARNENDU JATI: Does it work now?
Walter Roberson
on 14 Jun 2018
You have
x2_opt=0;
x3_opt=0;
x4_opt=0;
xinit=[interp1(t_opt,x1_opt,t); %x1*
interp1(t_opt,x2_opt,t); %x2*
interp1(t_opt,x3_opt,t); %x3*
interp1(t_opt,x4_opt,t);] ; %x4*
Your x2_opt, x3_opt, and x4_opt are each scalar 0, not zeros the same size as t_opt. And with them being constant 0, there is little point doing interpolation: just output 0 without interpolation.
x2_opt=0;
x3_opt=0;
x4_opt=0;
xinit=[interp1(t_opt,x1_opt,t); %x1*
x2_opt; %x2*
x3_opt; %x3*
x4_opt;] ; %x4*
Walter Roberson
on 14 Jun 2018
0 votes
Your xinit_fcn is invoking interp1 with an x1_opt value derived from the t value that is passed into the function. However, bvpinit invokes the function with individual t values, not with the entire list of t values, so x1_opt will be a scalar. You are then trying to interp1(1 x 4452, 1 x 1, 1 x 1) . That fails because the second parameter must be the same length as the first parameter.
If you did have the entire original list of time values passed in, and were calculating x1_opt= -(pi/Tf)*All_t + pi and wanting to interpolate at the particular time, t, that was passed in to the xinit_fcn, then the result would always be the same as -(pi/Tf)*t + pi where t is the individual t. So it is not clear why you do not just have your xinit_fcn return -(pi/Tf)*t + pi .
Categories
Find more on Bartlett in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!