필터 지우기
필터 지우기

How to resolve interp1 error?

조회 수: 20 (최근 30일)
SARNENDU JATI
SARNENDU JATI 2018년 6월 12일
댓글: Walter Roberson 2018년 6월 14일
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
Walter Roberson
Walter Roberson 2018년 6월 12일
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;

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2018년 6월 12일
% 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
Jan
Jan 2018년 6월 14일
@SARNENDU JATI: Does it work now?
Walter Roberson
Walter Roberson 2018년 6월 14일
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
Walter Roberson 2018년 6월 14일
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 .

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by