defining state matrix as anonymous function

조회 수: 2(최근 30일)
Nicole
Nicole 2022년 11월 29일
답변: Sam Chak 2022년 11월 30일
How do I define my state matricies as an anonymous function?
%variables
m = 18130.59;
k_t = 80363.83655;
c_aero = 4561.755;
c_t = 4561.755;
k_b = 62.86;
R = 1.41;%resistance
L = 0.000644;
I1 = 874897.2;
I2 = 0.000027525;
N = 149;%gear ratio
e= 2.1e11;
d_l = 0.7736;
d_h = 0.007;
l_l = 3.5;
l_h=0.3;
area_l = pi*(d_l/2)^2;
area_h = pi*(d_h/2)^2;
k_l = (e*area_l)/l_l;
k_h = (e*area_h)/l_h;
%state matrix
A = [0 1 0 0 0
(-N*k_h)/(I1) -c_aero/(I1) k_h/(I1) 0 0
0 0 0 1 0
(N*k_h)/(I2) 0 -k_h/I2 -c_t/I2 k_t/I2
0 0 0 -k_b/L -R/L]
B = [0 0
1/(I1) 0
0 0
0 0
0 -1/L]
C = eye(5)
D = [0]
  댓글 수: 1
Matt J
Matt J 2022년 11월 29일
How do I define my state matricies as an anonymous function?
An anonymous function of what variables?

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

답변(3개)

Matt J
Matt J 2022년 11월 29일
For example
B = @(I1,L)[0 0
1/(I1) 0
0 0
0 0
0 -1/L];
B(2,1)
ans = 5×2
0 0 0.5000 0 0 0 0 0 0 -1.0000
  댓글 수: 9
Walter Roberson
Walter Roberson 2022년 11월 30일
%B is 5x2
%u is 2 x 1
%so Bu should be 5 x 1
Bu = B*u;
%A is 5 x 5
%x is made 5 x 1
%so A*x(:) will be 5x1, which can be added to the 5x1 Bu
myfun = @(x) A*x(:) + Bu

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


Star Strider
Star Strider 2022년 11월 30일
Referring to your other post: ode45 is running an infinite loop (it isn’t actually, since switching from ode45 to ode15s solves that problem, as my solution demonstrated), I doubt if you can put that entire function in an anonymous function, at least efficiently. I would just leave it as it is.
However, if you want to define those functions to use them with the Control System Toolbox, ‘A’ for example would be:
A = @(N, k_h, c_aero, I1, I2, c_t, k_t, k_b, R, L) [0 1 0 0 0
(-N*k_h)/(I1) -c_aero/(I1) k_h/(I1) 0 0
0 0 0 1 0
(N*k_h)/(I2) 0 -k_h/I2 -c_t/I2 k_t/I2
0 0 0 -k_b/L -R/L];
You could do the same sort of operation for the others, if you want to. (Be sure all the variables in the function are accounted for in the argument list.)
Your function runs and integrates appropriately with ode15s, so I would be tempted to just leave it as it is unless you want to do something else with its components (such as use them with the Control System Toolbox). All the necessary variables would need to be present in your calling script workspace.
.

Sam Chak
Sam Chak 2022년 11월 30일
The input should be defined for in order for the anonymous function to work properly in MATLAB.
% parameters
m = 18130.59;
k_t = 80363.83655;
c_aero = 4561.755;
c_t = 4561.755;
k_b = 62.86;
R = 1.41; % resistance
L = 0.000644;
I1 = 874897.2;
I2 = 0.000027525;
N = 149; % gear ratio
e = 2.1e11;
d_l = 0.7736;
d_h = 0.007;
l_l = 3.5;
l_h = 0.3;
area_l = pi*(d_l/2)^2;
area_h = pi*(d_h/2)^2;
k_l = (e*area_l)/l_l;
k_h = (e*area_h)/l_h;
% state matrix 5-by-5
A = [0 1 0 0 0;
(-N*k_h)/(I1) -c_aero/(I1) k_h/(I1) 0 0;
0 0 0 1 0;
(N*k_h)/(I2) 0 -k_h/I2 -c_t/I2 k_t/I2;
0 0 0 -k_b/L -R/L];
% input matrix 5-by-2
B = [0 0;
1/I1 0;
0 0;
0 0;
0 -1/L];
% output matrix 5-by-5
C = eye(5);
% feedforward matrix (same size as B)
D = zeros(size(B));
Since the definition for is not provided, I presume that it can be a function of
where the size of the gain matrix is so that the size of is the same as the size of .
% gain matrix
K = [0.0335527521 3.9252586331 0.0122303086 6.8198765826e-12 1.1375510282e-7;
-5087204.592585 -154.54142082 34110.68921577 -0.004473181866 -132.094787];
% anonymous function for A*x + B*u
myfun = @(t, x) A*[x(1); x(2); x(3); x(4); x(5)] + B*(-K*[x(1); x(2); x(3); x(4); x(5)]);
% Test if the anonymous function works
[t, x] = ode15s(myfun, [0 0.5], [1, 0, 0, 0, 0]);
plot(t, x(:,1), 'linewidth', 1.5), grid on, xlabel('t'), ylabel('x_{1}(t)')

범주

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by