defining state matrix as anonymous function

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 日

0 投票

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 件のコメント

Nicole
Nicole 2022 年 11 月 29 日
The anonymous function I need should be dealing with the A*x+B*u equation. Because all the variables in my A and B matrix are constants.
Nicole
Nicole 2022 年 11 月 29 日
wold it have to be like this...
myfun = @(x) A.*x+B.*u
if so how would I get x to be a 5 by 1 vector so that the multiplication works
Nicole
Nicole 2022 年 11 月 29 日
Sorry for the spam but would this be how you would do it
myfun = @(t,x) A.*[x(1);x(2);x(3);x(4);x(5)]+B.*u
Walter Roberson
Walter Roberson 2022 年 11 月 29 日
%pre-compute the constants
Bu = B.*u;
myfun = @(x) A.*x(:) + Bu
Nicole
Nicole 2022 年 11 月 29 日
It gives me an error with that saying the arrays have incompatable sizes for this operation, when they should be. A is a 5x5, B is a 5x2, U is a 2x1 and X should be a 5x1.
Torsten
Torsten 2022 年 11 月 29 日
Does u change during integration ? If yes: how is it computed ?
Matt J
Matt J 2022 年 11 月 29 日
編集済み: Matt J 2022 年 11 月 29 日
and X should be a 5x1.
What do you mean "should be"? Did you check if it is 5x1?
Nicole
Nicole 2022 年 11 月 30 日
I did not define the x vector yet, that is what i am solving for. Do I have to define the size of it? How would I check to see if its a 5x1?
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 日

0 投票

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 日

0 投票

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)')

カテゴリ

ヘルプ センター および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

質問済み:

2022 年 11 月 29 日

回答済み:

2022 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by