LKAS simulink model error array out of bound - simulink

Index exceeds matrix dimensions. The array 'right_base' is empty and therefore has no valid indices.
Error in 'LKAS/Perception Unit/MATLAB Function3' (line 17)

Related

Error in Matlab Function Block: Index exceeds array dimensions

I have some problem during running the code below and it give me the error
Index exceeds array dimensions. Index value 3 exceeds valid range [1-2] for array 'a'. Error in 'TEST/TEST' (line 18) if a(i)> 0
This code is the code for computing the parameter k for the scalar reference governor. and Hx and Hv are the maximal admissible output sets(MAS) with the matrices A,B,C,D hope can get some help to fix this code from your all.
function v = SRG_new(v_previous, r)
A=[0 1;-275.5 -21.22];
B=[0;1];
C=[11.02 275.5];
D=0;
I=eye(2);
Hx=(C*A);
Hv= C*((I-A)*((I-A)^-1)*B+D);
s=350; %s=max_output
a=Hx*(r-v_previous);
b=s-Hx-Hv*v_previous;
k=1;
for i=1:100
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
k=max(k,0);
v=v_previous + k*(r-v_previous);
end
Well this depends mainly on the size your inputs v_previous and r (As The error specifies the range "1-2" i guess, "r" and "v_previous" are scalar values).
The error occurs, because you want to iterate through 100 elements of "a", but as Hx=(C*A); only creates a 1x2 Matrix, a = Hx*(r-v_previous); will result in a Matrix "a" with fewer than 100 entries (Exactly two entries if only multiplied with a scalar value). And this must lead to the "Out of range - Error".
To get rid of the error, simply use
for i=1:numel(a)
if a(i)> 0
k=min(k, b(i)/a(i));
end
end
This way, it will only iterate through the available array elements.

How can matrix be filled with symbolic expressions in Matlab?

I want to fill the rows of the matrix with symbolic expressions using for-loop.
The code is given below.
for r=1:N
%dL/dfidot
frst(r)=diff(L,fidot(r));
%d/dt*dL/dfidot
dfrst(r)=diff(frst(r),fi(r))*fidot(r)+diff(frst(r),fidot(r))*fiddot(r);
%dL/dfi
scnd(r)=diff(L,fi(r));
%EQ of Motion
EqofMotion(r)=dfrst(r)-scnd(r)==0;
acc(r)=solve(EqofMotion(r),fiddot(r));
C=zeros(N,1);
C(r,1)=acc(r);
end
acc is symbolic array, C is matrix. The idea is to fill r-th row of C matrix with acc(r) using loop. Program gives me an error as following:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in Trying (line 56)
C(r,1)=acc(r);
How can I fix this problem?
You are trying to assign a symbolic value to a double array element. This is illegal because symbolic objects are not implicitly convertible to double.
To solve this problem, you can make the C array an array of symbolic objects:
C = sym(zeros(N,1)); % now C is an array containing the symbolic expressions 'zero'
C(r,1) = acc(r);

adding syms expression in some elements of numeric matrix in matlab

I have a matrix of numeric data;
(let's say 4*4 )
how can I add a syms expression (e.g 3*s) in some elements of it???
I've tried to add it but I get this Error:
The following error occurred converting
from sym to double:
Error using mupadmex
Error in MuPAD command: DOUBLE cannot
convert the input expression into a
double array.

Using ismember for symbolic variables in Matlab

When using ismember for small symbolic arrays I get the expected result.
For example:
syms a b c d e f
ismember([a,b,c],[e,f,a])
ans =
1 0 0
However for larger arrays I get an error
ismember([a,b,c,d,e,f],[f,e,b,b,c,a])
Undefined function 'cumsum' for input arguments of type 'sym'.
Error in unique>uniquelegacy (line 411)
pos = cumsum([1;d]); % Lists position, starting at 1.
Error in unique (line 95)
[varargout{1:nlhs}] = uniquelegacy(varargin{1});
Error in ismember>ismemberlegacy (line 377)
[au,~,an] = unique(a(:));
Error in ismember (line 76)
[varargout{1:nlhs}] = ismemberlegacy(varargin{:});
Can you explain why this is? Is there a way around this for the larger symbolic arrays? Thanks!

Matlab error while creating a matrix

I want to insert 'double' type values in a matrix. For that I am creating a matrix with following lines of Matlab code:
dpitchcnt=(N/256); %N is total number of byte
pitchvec(1:int64(dpitchcnt)); %creating a matrix 'pitchvec' with 1 row and int64(dpitchcnt)' columns
size(pitchvec) %Trying to display the size.
I am getting the following error while carrying out the above operation:
Undefined function or method '_colonobj' for input arguments of type
'int64'. Error in ==> sample at 31 pitchevec(1:int64(dpitchcnt));
What am I doing wrong?
The syntax varName(1:10) will get the first 10 values of varName, not create the variable varName;
To create a matrix you can use
pitchvec = zeros(1,int64(dpitchcnt)); %A zero-matrix
matrixSize = size(pitchvec);
You can also use ones(n,m);%Create a n times m matrix with 1 all over.