Error in executing a simple matlab function

I've encountered a serious problem whan compiling my math work on matlab so can someone help me with this error so this is the matlab code:
% 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
U = MSx(T2+1,1);
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,'-')
So when it ask me to give an input like: "Donner le nombre des points de discretisation dans le temp N=" I give it a number like 50000 so it gives me that error : Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in third (line 75) MSz(n+1,1) = W;

3 commentaires

Steven Lord
Steven Lord le 2 Mai 2017
What is the FULL text (all the text appearing in red) of the error message you receive when you execute that code?
Arfaoui Hamza
Arfaoui Hamza le 2 Mai 2017
this is the full error message colored in red that the console gives me :
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in third (line 76) MSz(n+1,1) = W;
Rik
Rik le 2 Mai 2017
What sizes are MSz, W and n? Googling for this error (to get a general description of what it means), I came across this answer by Walter Roberson. It explains what this error means and when you can expect them (an thus how you can look to solve them).
You can also use breakpoints to got through your code line by line. This really helps with debugging code.
Oh, and please use the {}Code button. This code is very difficult to make sense of with this lay-out.

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 3 Mai 2017
Perhpas all that is missing is the continuation operator:
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))] ;
This is far too long for the forum's interface and for the editor also. Roughly this is:
W = W + k*[a
b] ;
When a linebreak occurres inside a vector, Matlab inserts a row break. So your code is equivalent to:
W = W + k*[a; ...
b] ;
Then W is a [2 x 1] vector and the assignment to a scalar must fail:
MSz(n+1,1) = W;
Scalar on the left, vector on the right. Perhaps you want:
W = W + k*[a ...
b] ;
or explicitely:
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)));
Note: The missing of comments makes it impossible to debug your code. Perhaps you can guess currently, what you want to achieve, but this will change in 3 month and nobody else can decipher this code to find out the intention, if it contains any bug.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by