在matlab上执行函数时出错

时间:2017-05-02 22:17:51

标签: matlab

我在matlab上编译数学工作时遇到了一个严重的问题,所以有人可以帮我解决这个错误,所以这是matlab代码:

%  Main program for solving the system F C 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear X;
clear x;
clear y;
clear z;
clear U;
clear V;
clear W;
clear MSx;
clear MSy;
clear MSz;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Données du problème
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = input('Donner le nombre des points de discretisation dans le temp N=');
a = 0;
b = 50;
T = b-a;
k = T/N;
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x = 1;
y = 1.5;
z = 0.3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t = zeros(N+1,1);
for n = 1:N+1
    t(n) = (n-1)*k;
end
MSx = zeros(N+1,1);
MSy = zeros(N+1,1);
MSz = zeros(N+1,1);

%
MSx(1,1) = x;
MSy(1,1) = y;
MSz(1,1) = z;

%
U = x;
V = y;
W = z;
%
X = zeros(3,1);
X = [U; V; W];
%
T1 = 0.2*N/T;
T2 = 0.5*N/T;
%

for n = 1:T1
    t(n) = (n-1)*k;
    %
    Un = U;
    Vn = V;
    Wn = W;
    %
    X = [Un; Vn; Wn];

   %

   U = U + k*MSy(n,1)-k*8*MSx(n,1);   
   %
   V = V+k*MSz(n,1)-k*1.02*MSy(n,1);  
   %
   W = W+ k*[cos(sqrt(2)*t(n))+cos(sqrt(3)*t(n))+1/(1+t(n)^2)-(1/(1+t(n)^2)-3.02)*MSz(n,1)+(9.02*(1/(1+t(n)^2)-1)-1.02)*MSy(n,1)- 64*(1/(1+t(n)^2)-1)*MSx(n,1)-0.0004*sin(sqrt(2)*t(n))*(MSy(n,1)-8*MSx(n,1))

   -0.0001*cos(sqrt(3)*t(n))*(abs(MSx(1,1)+1)-abs(MSx(1,1)-1)+abs(MSx(1,1)+1)-abs(MSx(1,1)-1))];  
   %

   MSx(n+1,1) = U;
   MSy(n+1,1) = V;
   MSz(n+1,1) = W;

end
%U = MSx(T1+1,1);
%V = MSy(T1+1,1);
%W = MSz(T1+1,1);
% 
 for n = T1+1:T2
    t(n) = (n-1)*k;
    %
    Un = U;
    Vn = V;
    Wn = W;
    %
    X = [Un; Vn; Wn];
   %
   U = U+k*(MSy(n,1)-8*MSx(n,1)); 

   V = V+k*(MSz(n,1)-1.02*MSy(n,1));

   W = W+ k*[cos(sqrt(2)*t(n))+cos(sqrt(3)*t(n))+1/(1+t(n)^2)-(1/(1+t(n)^2)-3.02)*MSz(n,1)+(9.02*(1/(1+t(n)^2)-1)-1.02)*MSy(n,1)- 64*(1/(1+t(n)^2)-1)*MSx(n,1)-0.0004*sin(sqrt(2)*t(n))*(MSy(n,1)-8*MSx(n,1))
       -0.0001*cos(sqrt(3)*t(n))*(abs(MSx(1,1)+1)-abs(MSx(1,1)-1)+abs(MSx(n-T1,1)+1)-abs(MSx(n-T1,1)-1))] ; 
   %

   %
   MSx(n+1,1) = U;
   MSy(n+1,1) = V;
   MSz(n+1,1) = W;
 end
%V = MSy(T2+1,1);
%W = MSz(T2+1,1);
%
 for n = T2+1:N
    t(n) = (n-1)*k;
    %
    Un = U;
    Vn = V;
    Wn = W;
    %
    X = [Un;Vn;Wn];
   %
   U = U+k*(MSy(n,1)-8*MSx(n,1)); 
   %
   V = V+k*(MSz(n,1)-1.02*MSy(n,1));
   %
   W = W+ k*[cos(sqrt(2)*t(n))+cos(sqrt(3)*t(n))+1/(1+t(n)^2)-(1/(1+t(n)^2)-3.02)*MSz(n,1)+(9.02*(1/(1+t(n)^2)-1)-1.02)*MSy(n,1)- 64*(1/(1+t(n)^2)-1)*MSx(n,1)-0.0004*sin(sqrt(2)*t(n))*(MSy(n,1)-8*MSx(n,1))
       -0.0001*cos(sqrt(3)*t(n))*(abs(MSx(n-T2,1)+1)-abs(MSx(n-T2,1)-1)+abs(MSx(n-T1,1)+1)-abs(MSx(n-T1,1)-1))];
   %

   MSx(n+1,1) = U;
   MSy(n+1,1) = V;
   MSz(n+1,1) = W;
 end
%
tv = t(1:1:N+1,1);
tv1 = t(1:1:N+1,1);
Su = MSx(1:1:N+1,1);
Sv = MSy(1:1:N+1,1);
Sw = MSz(1:1:N+1,1);
plot(tv,Su,'-')
hold all
plot(tv,Sv,'-')
hold all
plot (tv, Sw,'-')

所以当它要求我提供如下输入时: " Donner le nombre des points de discretisation dans le temp N =" 我给它一个像50000的数字所以它给了我这个错误: 赋值具有比非单例下标更多的非单例rhs维度

第三次出错(第75行)    MSz(n + 1,1)= W;

1 个答案:

答案 0 :(得分:0)

这是因为你的W不是单身,这意味着在这种情况下是一个标量。

您将W写为

   W = W+ k*[cos(sqrt(2)*t(n))+cos(sqrt(3)*t(n))+1/(1+t(n)^2)-(1/(1+t(n)^2)-3.02)*MSz(n,1)+(9.02*(1/(1+t(n)^2)-1)-1.02)*MSy(n,1)- 64*(1/(1+t(n)^2)-1)*MSx(n,1)-0.0004*sin(sqrt(2)*t(n))*(MSy(n,1)-8*MSx(n,1))

   -0.0001*cos(sqrt(3)*t(n))*(abs(MSx(1,1)+1)-abs(MSx(1,1)-1)+abs(MSx(1,1)+1)-abs(MSx(1,1)-1))];  

如果要更改表达式中间的线条,则需要在该行的末尾添加三个点,即...

所以你需要用以下代码替换:

   W = W+ k*[cos(sqrt(2)*t(n))+cos(sqrt(3)*t(n))+1/(1+t(n)^2)-(1/(1+t(n)^2)-3.02)*MSz(n,1)+(9.02*(1/(1+t(n)^2)-1)-1.02)*MSy(n,1)- 64*(1/(1+t(n)^2)-1)*MSx(n,1)-0.0004*sin(sqrt(2)*t(n))*(MSy(n,1)-8*MSx(n,1))...

   -0.0001*cos(sqrt(3)*t(n))*(abs(MSx(1,1)+1)-abs(MSx(1,1)-1)+abs(MSx(1,1)+1)-abs(MSx(1,1)-1))]; 

你还有很多这个问题。