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

4 次查看(过去 30 天)
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in step (line 18) dy(3) = 0.1e1 / l ^ 2 / (-cos(y(2)) ^ ......
%% the main first function function xc = totalmotion (q1_0, q2_0, dq1_0, dq2_0) global_constants; global M; % hip mass global m; % foot mass global l; % leg length global r; % the slop angle
global g; % gravity acceleration global delta_t; % compute interval step global rad; rad = 180/pi; global Kp; % the proportional coefficient global Kd; % the differential coefficient global q2d; % the desired value of q2 tspan = [0:delta_t:2]; options = odeset('abstol',1e-13,'reltol',1e-13,'events',@collision); y0 = [q1_0 q2_0 dq1_0 dq2_0]; %%% initial state p(1) = M; p(2) = m; p(3) = r; p(4) = l; [t,y]=ode45(@step,tspan,y0,options,p);
n=length(t);
for i =1:n
q1 = y(i,1); q2 = y(i,2);
x_0 = 0;
y_0 = 0;
x1 = x_0;%%%%calculate the spacial positions of the links.
y1 = y_0;
xh = x1 + l * sin(-q1 + r);
yh = y1 + l * cos(-q1 + r);
x2 = double(-l * sin(-q2 - q1 + r) + x1 + l * sin(-q1 + r));
y2 = double(-l * cos(-q2 - q1 + r) + y1 + l * cos(-q1 + r));
xc = (M * (x1 + l * sin(-q1 + r)) + m * (-l * sin(-q2 - q1 + r) + x1 + l * sin(-q1 + r))) / (M + 0.2e1 * m);
end
end
%%% the second function function dy = step(t,y,p) M = p(1); m = p(2); r = p(3); l = p(4);
dy = zeros(4,1);
dy(1) = y(3);
dy(2) = y(4);
dy(3) = 0.1e1 / l ^ 2 / (-cos(y(2)) ^ 2 * m + M + m) * (-0.2e1 * sin(y(2)) * y(3) * y(4) * l ^ 2 * m - y(4) ^ 2 * sin(y(2)) * l ^ 2 * m - M * sin(-y(1) + r) * g * l - sin(-y(1) + r) * g * l * m + sin(-y(2) - y(1) + r) * m * g * l) + 0.1e1 / l ^ 2 * (cos(y(2)) - 0.1e1) / (-cos(y(2)) ^ 2 * m + M + m) * (Kp * (q2d - y(2)) - Kd * y(4) + sin(y(2)) * y(3) ^ 2 * l ^ 2 * m + sin(-y(2) - y(1) + r) * m * g * l);
dy(4) = 0.1e1 / l ^ 2 * (cos(y(2)) - 0.1e1) / (-cos(y(2)) ^ 2 * m + M + m) * (-0.2e1 * sin(y(2)) * y(3) * y(4) * l ^ 2 * m - y(4) ^ 2 * sin(y(2)) * l ^ 2 * m - M * sin(-y(1) + r) * g * l - sin(-y(1) + r) * g * l * m + sin(-y(2) - y(1) + r) * m * g * l) + 0.1e1 / l ^ 2 * (-0.2e1 * cos(y(2)) * m + M + 0.2e1 * m) / m / (-cos(y(2)) ^ 2 * m + M + m) * (Kp * (q2d - y(2)) - Kd * y(4) + sin(y(2)) * y(3) ^ 2 * l ^ 2 * m + sin(-y(2) - y(1) + r) * m * g * l);
end Thanks for your help.

采纳的回答

Star Strider
Star Strider 2017-12-20
I cannot follow what you are doing, and I cannot run your code.
However, the most likely problem is that you need to vectorize your code, since otherwise MATLAB will interpret multiply, divide, and exponentiation operations as matrix rather than element-wise array operations. That means using the ‘dot operator’, substituting ‘.*’ for ‘*’, ‘./’ for ‘/’, and ‘./’ for ‘^’.
Use the vectorize (link) fiunction, and see the documentation on Array vs. Matrix Operations (link) for details.
  2 个评论
Qiaoli Ji
Qiaoli Ji 2017-12-20
Thanks for answering. I want to compute motion equation using ode45.
[t,y]=ode45(@step,tspan,y0,options,p);
% y0 is row vector,p is constant,step function is motion equation
function dy = step(t,y,p)
  1. dy = zeros(4,1);
  2. dy(1) = y(3);
  3. dy(2) = y(4);
  4. dy(3) = 0.1e1 / l ^ 2 / (-cos(y(2)) ^ 2 * m + M + m) * (-0.2e1 * sin(y(2)) * y(3) * y(4) * l ^ 2 * m - y(4) ^ 2 * sin(y(2)) * l ^ 2 * m - M * sin(-y(1) + r) * g * l - sin(-y(1) + r) * g * l * m + sin(-y(2) - y(1) + r) * m * g * l) + 0.1e1 / l ^ 2 * (cos(y(2)) - 0.1e1) / (-cos(y(2)) ^ 2 * m + M + m) * (Kp * (q2d - y(2)) - Kd * y(4) + sin(y(2)) * y(3) ^ 2 * l ^ 2 * m + sin(-y(2) - y(1) + r) * m * g * l);
  5. dy(4) = 0.1e1 / l ^ 2 * (cos(y(2)) - 0.1e1) / (-cos(y(2)) ^ 2 * m + M + m) * (-0.2e1 * sin(y(2)) * y(3) * y(4) * l ^ 2 * m - y(4) ^ 2 * sin(y(2)) * l ^ 2 * m - M * sin(-y(1) + r) * g * l - sin(-y(1) + r) * g * l * m + sin(-y(2) - y(1) + r) * m * g * l) + 0.1e1 / l ^ 2 * (-0.2e1 * cos(y(2)) * m + M + 0.2e1 * m) / m / (-cos(y(2)) ^ 2 * m + M + m) * (Kp * (q2d - y(2)) - Kd * y(4) + sin(y(2)) * y(3) ^ 2 * l ^ 2 * m + sin(-y(2) - y(1) + r) * m * g * l);
Star Strider
Star Strider 2017-12-20
My pleasure.
You have to vectorize your equations, as I demonstrated. I cannot do this for you.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-12-20
You probably need to use the "dot" versions of * / ^ etc.
Put a dot/period before each mathematical operator, to do element-by-element operation instead of a matrix operation, and see how that goes.
  2 个评论
Qiaoli Ji
Qiaoli Ji 2017-12-20
sorry, I do not understand your meaning that 'put dot'. Could you show me an example? Thanks.
Image Analyst
Image Analyst 2017-12-20
Instead of
dy(3) = 0.1e1 / l ^ 2 / (-cos(y(2)) ^ 2 * m + M + m) * (-0.2e1 * sin(y(2)) * y(3) * y(4) * l ^ 2 * m - y(4) ^ 2 * sin(y(2)) * l ^ 2 * m - M * sin(-y(1) + r) * g * l - sin(-y(1) + r) * g * l * m + sin(-y(2) - y(1) + r) * m * g * l) + 0.1e1 / l ^ 2 * (cos(y(2)) - 0.1e1) / (-cos(y(2)) ^ 2 * m + M + m) * (Kp * (q2d - y(2)) - Kd * y(4) + sin(y(2)) * y(3) ^ 2 * l ^ 2 * m + sin(-y(2) - y(1) + r) * m * g * l);
try
dy(3) = 0.1e1 ./ l .^ 2 ./ (-cos(y(2)) .^ 2 .* m + M + m) .* (-0.2e1 .* sin(y(2)) .* y(3) .* y(4) .* l .^ 2 .* m - y(4) .^ 2 * sin(y(2)) .* l .^ 2 .........
etc.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assembly 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by