Asked by SARNENDU JATI
on 12 Jun 2018

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);

Answer by 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.

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*

Sign in to comment.

Answer by Walter Roberson
on 14 Jun 2018

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 .

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/405238-how-to-resolve-interp1-error#comment_577728

One thing (but probably not the reason for the error) is I suspect you want

instead of 4452 there. Better yet would be

Sign in to comment.