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

조회 수: 2 (최근 30일)
Hi everyone, Here is the code, that gives me the error that I put in my title. Please correct my code, Also, I am new in MATLAB, please advice me Thanks in advance. When i was created it, its working nicely, but, after one day i faced the above problem. my initial values are; y0=[q1 q2 q3 q4 w1 w2 w3]=[0 0 0 1 0 0 0]; t=0:0.3:50; i used ode45 to solve the following problem. Error coming from 18th line. Please fix the following problem
function xdot=controller1(t,y)
global u1 u2 u3 ue1 ue2 ue3 Ix Iy Iz q1c q2c q3c q4c Kv Kp
dy=zeros(7,1); % a column vector
% External torque
ue1=0;
ue2=0;
ue3=0;
Kv=370;
Kp=315;
% Quaternions
dy(1)=((y(7)*y(2)-y(6)*y(3)+y(5)*y(4))*.5);
dy(2)=((-y(7)*y(1)+y(5)*y(3)+y(6)*y(4))*.5);
dy(3)=((y(6)*y(1)-y(5)*y(2)+y(7)*y(4))*.5);
dy(4)=((-y(5)*y(1)-y(6)*y(2)+y(7)*y(3))*.5);
%Omegas
dy(5)=(u1+ue1+(Iy-Iz)*y(6)*y(7))/Ix;
dy(6)=(u2+ue2+(Iz-Ix)*y(7)*y(5))/Iy;
dy(7)=(u3+ue3+(Ix-Iy)*y(5)*y(6))/Iz;
% Error Quaternion
qe1=(q4c*y(1)+q3c*y(2)-q2c*y(3)-q1c*y(4));
qe2=(-q3c*y(1)+q4c*y(2)-q1c*y(3)-q2c*y(4));
qe3=(q2c*y(1)-q1c*y(2)+q4c*y(3)-q3c*y(4));
% Control torque
u1=-Kp*qe1-Kv*y(5);
u2=-Kp*qe2-Kv*y(6);
u3=-Kp*qe3-Kv*y(7);
xdot=[dy(1);dy(2);dy(3);dy(4);dy(5);dy(6);dy(7)];
end
  댓글 수: 5
C.J. Harris
C.J. Harris 2011년 12월 22일
Then debug your program and make sure the following variables also are of the intended size (single values probably):
u1 u2 u3 Ix Iy Iz
Also, why are you setting ue1, ue2, ue3, Kv and Kp as global variables, as they dont change between iterations?
Dipak giri
Dipak giri 2011년 12월 22일
Thank you lot sir,
Yes sir, you are right. according to you, i edit the program. Now m running the program but still problem. Sir, in the above problem i have 7 differential equations, 4 are for quaternion and other 3 for angular rates. You may think they are 7 differential equations. u1 , u2, u3 are the control inputs. I have to look in the omegas part based on quaternion are changing.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Tearle
Matt Tearle 2011년 12월 22일
The only likely culprit is that your global variables are not what you think they are. The right-hand side of line 18 is probably coming out non-scalar; likely cause: u1, Ix, Iy, or Iz is not a scalar.
To diagnose this, enter
dbstop if error
then run your code. It will enter debug mode when the error occurs. Examine the dimensions of your variables. Also try just entering the RHS of line 18 at the command prompt and see what the result is
K>> (u1+ue1+(Iy-Iz)*y(6)*y(7))/Ix
Additional things:
  1. Why do you have xdot = ...? Why not just change your function declaration to return dy instead of xdot?
  2. Do you intend to change u1, u2, and u3 each time? (They're global. I assume that's the point, but maybe not.)
EDIT TO ADD: why do you need u1, u2, and u3 to be global? What are you doing with them outside of this function? Given that they can be calculated from y and the (global) parameters, why not make a function to calculate them, then call it from here and outside (as needed). Then you can do away with global variables altogether. If you need to pass parameters to an ODE function, do this:
function dy = odefun(t,y,a,b,c)
% don't use global a b c
dy = ...function of a,b,c...
Then in the main code
a = pi;
b = 42;
c = 0;
fode = @(t,y) odefun(t,y,a,b,c);
[t,y] = ode45(fode,...);
This way, the parameters a, b, c are embedded into the definition of the function handle fode. No globals needed.
  댓글 수: 2
Dipak giri
Dipak giri 2011년 12월 22일
My system is connected to each other. the following steps m using ,
1. finding the 'omegas' based on the control input u1,u2,u3.
In the control input, m applying the my desired quaternion q1c,q2c,q3c,q4c.
2. Based on omegas , finding the 'quaternion'.
I did what you said, and i got the following result,
K>> dbstop if error
K>> (u1+ue1+(Iy-Iz)*y(6)*y(7))/Ix
ans =
[]
,,,,,,,,,,,,,,,
the code i used like this
function dy=controller1(t,y)
global u1 u2 u3 q1c q2c q3c q4c Kv Kp
dy=zeros(7,1); % a column vector
%q=[y(1);y(2);y(3);y(4)];
%w=[y(5);y(6);y(7)];
% External torque
ue1=0;
ue2=0;
ue3=0;
% Values of Kv and Kp
Kv=370;
Kp=315;
% Momnets of inertia
Ix=60;
Iy=70;
Iz=80;
% Quaternions
dy(1)=((y(7)*y(2)-y(6)*y(3)+y(5)*y(4))*.5);
dy(2)=((-y(7)*y(1)+y(5)*y(3)+y(6)*y(4))*.5);
dy(3)=((y(6)*y(1)-y(5)*y(2)+y(7)*y(4))*.5);
dy(4)=((-y(5)*y(1)-y(6)*y(2)+y(7)*y(3))*.5);
%Omegas
dy(5)=(u1+ue1+(Iy-Iz)*y(6)*y(7))/Ix;
dy(6)=(u2+ue2+(Iz-Ix)*y(7)*y(5))/Iy;
dy(7)=(u3+ue3+(Ix-Iy)*y(5)*y(6))/Iz;
% Error Quaternion
qe1=(q4c*y(1)+q3c*y(2)-q2c*y(3)-q1c*y(4));
qe2=(-q3c*y(1)+q4c*y(2)-q1c*y(3)-q2c*y(4));
qe3=(q2c*y(1)-q1c*y(2)+q4c*y(3)-q3c*y(4));
% Control torque
u1=-Kp*qe1-Kv*y(5);
u2=-Kp*qe2-Kv*y(6);
u3=-Kp*qe3-Kv*y(7);
dy=[dy(1);dy(2);dy(3);dy(4);dy(5);dy(6);dy(7)];
end
Sir, if you can plz help me wht shud b the exact code.
I am using the the intial value
y0=[0 0 0 1 0 0 0];
and
t=0:0.3:50
Matt Tearle
Matt Tearle 2011년 12월 22일
I don't know what the code should be. I don't know the application, nor do I know how your global variables are being defined, nor what you're doing with them outside of this function.
I'd recommend doing K>> whos to find out why the calculation is returning an empty array.
I'd also recommend doing away with global variables as much as possible.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Analog Filters에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by