Clear Filters
Clear Filters

Error : '' In an assignment A(I) = B, the number of elements in B and I must be the same ''

2 views (last 30 days)
I want to draw the phase portrait of pendulum but its giving an error. code is like this
function derivative_x = myfunc(t,x)
derivative_x=zeros(2,1);
% prameters
g=10;
m= 2;
M= 8;
a= 1/10;
l=1/2;
K1 = [- 429.6222 -122.8607] ;
K2 = [- 913.7748 - 284.1638];
derivative_x(1)= x(2);
error ==>> derivative_x(2)= (g .* sin(x(1)) - 0.5.*a.*m.*l.*(x(2)^2).*sin(2*x(1))- (a.*cos(x(1)).*(K1.*x(1) +K2.*x(2))))/(1.33.*l-a.*m.*l.*(cos(x(1))^2));
Initial_Time=0;
Final_Time=20;
[x1,x2] = meshgrid(-2.5:0.9:4);
for i = 1:length(x1)
for j = 1:length(x1)
x10 = x1(i,j);
x20 = x2(i,j);
[t,x] = ode45(@myfunc, [Initial_Time Final_Time], [x10 x20]);
plot(x(:,1),x(:,2))
axis([-2.5 4 -2.5 4])
xlabel('x1','fontsize',30,'fontweight','bold')
ylabel('x2','fontsize',30,'fontweight','bold')
hold on
end
end
but its giving an error In an assignment A(I) = B, the number of elements in B and I must be the same. kindly help....!
  2 Comments
pfb
pfb on 13 Apr 2015
that means exactly what it says. You're "equalling" two objects that have a different number of elements.
The error message should come with a line number, which should help you (and us) to pinpoint it.
Ahmad Sheikh
Ahmad Sheikh on 13 Apr 2015
error is at this line
derivative_x(2)= (g .* sin(x(1)) - 0.5.*a.*m.*l.*(x(2)^2).*sin(2*x(1))-(a.*cos(x(1)).*(K1.*x(1) +K2.*x(2))))/(1.33.*l-a.*m.*l.*(cos(x(1))^2));

Sign in to comment.

Accepted Answer

Jan
Jan on 13 Apr 2015
This is a scalar:
derivative_x(2)
This is scalar also:
(g .* sin(x(1)) - 0.5.*a.*m.*l.*(x(2)^2).*sin(2*x(1))- ...
This is a [1 x 2] vector:
(a.*cos(x(1)).*(K1.*x(1) +K2.*x(2))))/ ...
and the rest is scalar again:
(1.33.*l-a.*m.*l.*(cos(x(1))^2));
Therefore the right hand side is a [1 x 2] vector and cannot be assigned to a scalar.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!