试图访问sym(67);由于numel(sym)= 2,因此索引越界

时间:2013-12-01 21:41:18

标签: matlab

我收到以下错误消息:

  

试图访问sym(67);因为numel(sym)= 2,所以索引越界。

我已经为此工作了三天。我寻找类似的错误,但它没有帮助。我的代码如下:

filename='DriveCyclesCP.xlsx';
V=xlsread('DriveCyclesCP.xlsx',2,'C9:C774'); % Get the velocity values, they are in an   array V.
N=length(V);                  % Find out how many readings
mass = 1700 ;                 % Vehicle mass+ two 70 kg passengers.
area_Cd = 0.75;               % Frontal area in square metres
Crr=0.009;                    %rolling resistance
g=9.8;                        % gravity acceleration
T=774;                        %UDDS cycle time duration
V_ave = 21.5;                 % UDDS avearage speed im m/s
rd=0.3;                       % Effective tire radius
Qhv =12.22;                   % E85 low Heating value in kWh/kg
Vd = 2.189;                     % engine size in L 
md=0.801;                     %  mass density of Ethanol
mf =Vd*md;                    % mf is the fuel mass consumed per cycle

Per = zeros(1,N);             % engine power for each point of the drive cycle
a = zeros(1,N);               % acceleration 
SFC = zeros(1,N);             % specific fuel consumption
Wc = zeros (1,N);            % mass flow rate
nf = zeros (1,N);             %fuel efficiency
Pm = zeros (1,N);            % motor power
Pt = zeros (1,N);
Te =zeros (1,N);              % Engine Troque
Tt = zeros (1,N);
Tm =zeros (1,N);
we =zeros (1,N);              % Engine rot speed
wt = zeros (1,N);
wm =zeros (1,N);
S =zeros (1,8);
int (sym ('C'));

for C=1:N
    a(C)=V(C+1)-V(C);
    Pt(C)= V(C)*(mass*g*Crr + (0.5*area_Cd*1.202*(V(C))^2) + mass*a(C))/1000; 
    Per(C)=(mass*g*Crr +0.5*area_Cd*1.202*(V(C))^2 +mass*g*0.03)/1000*0.85;% e

    syms Te(C) Tt(C) Tm(C) wt(C) we(C) wm(C) k1 k2

    S = solve( Pm(C)==Pt(C) - Per(C), Tt(C)*wt(C)== Pt(C), Tt(C)*wt(C)==  Te(C)*we(C) +      Tm(C)*wm(C), wt(C)==we(C)/k1,  wt(C)==wm(C)/k2, Pm(C)==wm(C) *Tm(C), Per(C)==we(C) *Te(C), Tt == k1*Te + k2*Tm );

end

1 个答案:

答案 0 :(得分:2)

问题在于

int (sym ('C'));

您已将sym定义为某个地方有2个条目的矩阵(在代码的前面或前一个mfile中),因此它将sym视为矩阵而不是函数。因此,当Matlab到达语句sym('C')时,它首先将字符'C'转换为其ASCII整数表示(这恰好是数字67),然后它尝试计算{{1这是不可能的,因为sym(67)只有2个元素。

因此,你必须阻止sym成为一个矩阵(变量)并让它再次成为一个函数。有两种方法可以解决这个问题,要么你可以使用语句sym启动文件,这将删除内存中的所有变量,这可能不是你想要的;或者您可以使用function instead of script,因为这会隐藏之前定义的所有变量并防止出现此类错误。

注意,行clear;是一种衡量numel(X)中元素数量的方法。因此X表示numel(sym)=2有2个元素。

P.S。这些行中有一个错误(注意我只采用了你的一些代码行)

sym

N=length(V); % Find out how many readings for C=1:N a(C)=V(C+1)-V(C); end 等于C时,N会产生错误。