Info

此问题已关闭。 请重新打开它进行编辑或回答。

How do I return a matrix in a function? I keep getting an error that says "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"

1 次查看(过去 30 天)

Here is my code I wrote,

function [out] = two_link_bot(theta1, theta1_dot, theta1_ddot, beta, beta_dot, beta_ddot)
m1 = 2;
m2 = 1;
L1 = 2;
L2 = 1;
g = 9.81;
I1 = 0.667;
I2 = 0.083;
c1 = 0.005;
c2 = 0.005;
mee = 0.1;
mm2 = 0.25;
% Tf1 = 1.6;
% Tf2 = 0;
% x
% C1 = (m1*L1^2/4) + I1 + (m2*L1^2) + (mm2*L1^2) + (mee*L1^2);
% C2 = ((m2/2)+mee)*L1*L2;
% C3 = (m1*g*L1/2) + (m2*g*L1) + (mm2*g*L1) + (mee*g*L1);
% C4 = I2 + ((m2*L2^2)/4) + (mee*L2^2);
% C5 = (m2/2 + mee)*g*L2;
% C6 = (m1/2) + mm2;
% Will create a matrix A multiplied by matrix x that equals b, 
% X will contain our unkowns, so we can solve for x in the following order
% O1x O1y O2x O2y tau1 tau2
A = zeros(6);
B = zeros(6,1);
A(1,1) = 1;
A(1,3)= 1;
A(2,2) = 1;
A(2,4) = 1;
A(3,1) = (L1/2).*sin(theta1);
A(3,2) = -(L1/2).*cos(theta1);
A(3,3) = -(L1/2).*sin(theta1);
A(3,4) =(L1/2).*cos(theta1);
A(3,5) = 1;
A(3,6) = 1;
A(4,3) = -1;
A(5,4) = -1;
A(6,3) = -(L2/2)*sin(beta);
A(6,4) = (L2/2)*cos(beta);
A(6,6) = 1;
B(1) = -((m1/2) + mm2)*(L1*sin(theta1).*theta1_ddot) - ((m1/2) + mm2)*L1*cos(theta1).*theta1_dot.^2;
B(2) = C6(L1)*cos(theta1).*theta1_ddot + m1*g+mm2*g-C6*L1*sin(theta1).*theta1_dot.^2;
B(3) = (I1+mm2*L1^2/2)*theta1_ddot + c1*theta1_dot - c2*theta1_dot+mm2*g*L1/2*cos(theta1);
B(4) = -(m2+mee)*L1*sin(theta1).*theta1_ddot - (m2/2 + mee)*L2*sin(beta)-(m2+mee)*L1*cos(theta1)*theta1_dot.^2 - (m2/2+mee)*L2*cos(beta)*beta_dot.^2;
B(5) = (m2+mee)*L1*cos(theta1).*theta1_ddot + (m2/2+mee)*L2*cos(beta).*beta_ddot - (m2+mee)*L1*sin(theta1).*theta1_dot.^2 - (m2/2 +mee)*L2*sin(beta)*beta_dot.^2+m2*g+mee*g;
B(6) = mee*(L2/2)*L1*cos(beta-theta1).*theta1_ddot + (I2+mee*(L2^2)/2)*beta_dot + mee*g*(L2/2)*cos(beta) + c2*theta1_dot+mee*(L2/2)*L1*sin(beta-theta1).*theta1_dot.^2;
out = A\B;
end

There is another file that is a .mat file that has the values for the thetas and betas.

I've attached it.

I want to get a graph of tau1 and tau2 profiles as the angles are input.

回答(1 个)

Image Analyst
Image Analyst 2017-10-9
Some of those long expressions on the right hand side must not be a single number - they must be vectors (several numbers). Check the value of theta and see that you have dot star rather than star or vice versa depending on what you want to do.
  2 个评论
Jay Gersten
Jay Gersten 2017-10-9
yea the theta and beta values are matrices 2001 x 1 large. I tried putting .* in front of all the multipliers, but it didnt help. even in sin(theta1) I have a problem.
I get this
Assignment has more non-singleton rhs dimensions
than non-singleton subscripts
Error in two_link_bot (line 34)
A(3,1) = (L1/2).*sin(theta1);
Image Analyst
Image Analyst 2017-10-9
Why do you think it should be a single number instead of a vector? You can't assign a bunch of numbers to a single element of a regular array. You can do that with a cell array, or if you have a 2D array:
B(1,:) = .......
etc.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by