Transformation of a MATLAB Function.

5 次查看(过去 30 天)
I have a function defined as,
f_xw = @(x,w) [3.*x(1) - x(1).^2/7 + w(1);
-2.*x(2) + w(2)];
I want to transform this from x coordinate system to \eta coordinate system which would look like
f_etaw = @(eta,w) [3.*(c1+G1*eta) - (c1+G1*eta).^2/7 + w(1);...
-2.*(c2+G2*eta) + w(2)];
where i define eta as symbolic variables
eta = sym('eta',[2 1]);
and c's are constants numbers (1x1) and G's are constant row vectors (1x2) which i can define globally.
Is there a way to do this transformation using matlabFunction command? And can this transformation be made general for all functions with n states?

采纳的回答

Steven Lord
Steven Lord 2021-3-13
f_xw = @(x,w) [3.*x(1) - x(1).^2/7 + w(1);
-2.*x(2) + w(2)];
f_etaw = @(eta,w) [3.*(c1+G1*eta) - (c1+G1*eta).^2/7 + w(1);...
-2.*(c2+G2*eta) + w(2)];
So instead of x(1) you want to use c1+G1*eta and instead of x(2) you want to use c2+G2*eta?
% assuming c1, c2, G1, and G2 already exist
f_etaw = @(eta, w) f_xw([c1+G1*eta, c2+G2*eta], w);
And can this transformation be made general for all functions with n states?
Assuming c and G are vectors that are the same size as the x input with which f_xw expects to be called:
% assuming c and G already exist
f_etaw = @(eta, w) f_xw(c+G*eta, w);
  2 个评论
Fawad Farooq Ashraf
Thank you so much. It works.
I still need a bit of help here.
After I get f_etaw using this. Considering this case, my original function had 2 equations. Can I get two separate function handles for each of them. I'll try to explain this.
I have a function handle which has two equations (the number of equations may vary, thats why I wanted to generalize it)
f_xw = @(x,w) [3.*x(1) - x(1).^2/7 + w(1);
-2.*x(2) + w(2)];
Basically I want separate function handles for each of the equations. I was unable to extract them in this form so I manually transformed them as used a cell array as
f_etaw = {@(eta,w) 3.*(c1+G1*eta) - (c1+G1*eta).^2/7 + w(1);
@(eta,w) -2.*(c2+G2*eta) + w(2)};
This way I can use my eqiuations as f_etaw{1,1} and f_etaw{2,1}. I wanted to generalize this for any example with as many states/equations as required.
Fawad Farooq Ashraf
编辑:Fawad Farooq Ashraf 2021-3-14
I've tried to do this in the following way. Is this correct?
f_xw = @(x,w) [3.*x(1) - x(1).^2/7 + w(1);
-2.*x(2) + w(2)];
c, G and w are defined. Here I removed w from f_etaw. I want to fix w by defining it globally.
eta = sym('eta',[size(G,2) 1]);
f_eta = @(eta) f_xw(c+G*eta,w);
f_eta = f_etaw(eta);
f_etas = cell(1,size(f_eta,1));
for m = 1:size(f_eta,1)
f_etas{m} = matlabFunction(f_eta(m,:),'Vars',{eta});
end
Does this seem alright?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by