does anyone knows how to write vector components inside matlab function?

13 views (last 30 days)
I tried to create a simulink block with embedded code through "matlab function":
function xdot = fcn(m,J,u,x)
%#codegen
R = [cos(x(8))*cos(x(9)) cos(x(8))*sin(x(9)) -sin(x(8));
-cos(x(7))*sin(x(9))+sin(x(7))*sin(x(8))*cos(x(9)) cos(x(7))*cos(x(9))+sin(x(7))*sin(x(8))*sin(x(9)) sin(x(7))*cos(x(8));
sin(x(7))*sin(x(9))+cos(x(7))*sin(x(8))*cos(x(9)) -sin(x(7))*cos(x(9))+cos(x(7))*sin(x(8))*sin(x(9)) cos(x(7))*cos(x(8))];
%
M = [1 sin(x(7))*tan(x(8)) cos(x(7))*tan(x(8));
0 cos(x(7)) -sin(x(7)) ;
0 sin(x(7))*sec(x(8)) cos(x(7))*sec(x(8))];
%
Wskew = [ 0 -x(12) x(11);
x(12) 0 -x(10);
-x(11) x(10) 0 ];
%
Vb = [x(4);
x(5);
x(6)];
%
W = [x(10);
x(11);
x(12)];
%
xdot=[R*Vb;
F-Wskew*Vb;
M*W;
J\(N-Wskew*J*W];
But matlab does not interpret the entries of vector "x" to form the matrices "W", "Vb", "R" and "Wskew".
DOES ANYONE HAVE ANY IDEA THAT I CAN TRY ON THIS PROBLEM?
Thanks.
  3 Comments
Alexandre Masson Vicente
Alexandre Masson Vicente on 21 Jan 2016
Edited: per isakson on 21 Jan 2016
Sorry, guys. I wrote the code with some errors. The program worked!
function xdot = fcn(m,J,u,x)
%#codegen
% atitude da aeronave na sequência 321 (projeta as medidas do INERCIAL no CORPO)
R = [cos(x(8))*cos(x(9)) cos(x(8))*sin(x(9)) -sin(x(8));
-cos(x(7))*sin(x(9))+sin(x(7))*sin(x(8))*cos(x(9)) cos(x(7))*cos(x(9))+sin(x(7))*sin(x(8))*sin(x(9)) sin(x(7))*cos(x(8));
sin(x(7))*sin(x(9))+cos(x(7))*sin(x(8))*cos(x(9)) -sin(x(7))*cos(x(9))+cos(x(7))*sin(x(8))*sin(x(9)) cos(x(7))*cos(x(8))];
%
% transformação das velocidades angulares nas taxas dos ângulos de euler
M = [1 sin(x(7))*tan(x(8)) cos(x(7))*tan(x(8));
0 cos(x(7)) -sin(x(7)) ;
0 sin(x(7))*sec(x(8)) cos(x(7))*sec(x(8))];
%
% matriz anti-simétrica de velocidades angulares (componentes no CORPO)
Wskew = [ 0 -x(12) x(11);
x(12) 0 -x(10);
-x(11) x(10) 0 ];
%
% vetor velocidade do centro de massa da aeronave escrito no CORPO
Vb = [x(4);
x(5);
x(6)];
%
% vetor velocidade angular da aeronave escrito no CORPO
W = [x(10);
x(11);
x(12)];
%
% vetor soma das forças externas aplicadas ao veículo
F = [u(1);
u(2);
u(3)]
%
% vetor soma dos torques externos em torno do baricentro da aeronave
N = [u(4);
u(5);
u(6)];
%
% Dinâmica do Corpo Rígido descrita em ESPAÇO DE ESTADOS, xdot = f(x,u,t)
xdot=[R*Vb; % velocidade escrita no INERCIAL
F-Wskew*Vb; % aceleração escrita no CORPO
M*W; % rapidez com que variam os ângulos de euler
J\(N-Wskew*J*W)]; % aceleração angular escrita no CORPO

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!