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

1 次查看(过去 30 天)
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 个评论
pfb
pfb 2015-4-13
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 2015-4-13
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));

请先登录,再进行评论。

采纳的回答

Jan
Jan 2015-4-13
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by