How to use indexed function output as a function of original inputs?

3 vues (au cours des 30 derniers jours)
Hello everyone,
I have a big code that pin down to a system of nonlinear equations which I want to solve using fsolve. I am facing an error message: "Dot indexing is not supported for variables of this type". A simplified version of the code is:
function out = equilibrium_F(par_n,tax_n,tar_n,init_n,...
par_m,tax_m,tar_m,init_m,...
par_z,tax_z,tar_z,init_z)
cons_n = @(Q) pd_cons(I(Q(115:129) , Q(160:174) , Q(205:207) , Q(214:217) ,...
Q(13) , Q(14) , Q(15) , Q(16) , Q(17) , Q(18) ,...
Q(10) , Q(1) , init_n) ,...
Q(7) , ...
Q(19:33) , Q(34:48) , Q(49:63) ,...
Q(64) , ...
par_n , tax_n , tar_n ,...
par_m , tax_m , tar_m ,...
par_z , tax_z , tar_z);
XA_i_n = @(Q) cons_n.XA_i(Q);
INV_i_n = @(Q) cons_n.INV_i(Q);
XMT_i_nm = @(Q) cons_n.XMT_i_nm(Q);
XMT_i_nz = @(Q) cons_n.XMT_i_nz(Q);
Tax_rev_cons_n = @(Q) cons_n.Tax_rev_cons(Q);
F1 = @(Q) (XA_i_n(Q),INV_i_n(Q), ...)
F2 = @(Q) (XMT_i_nm(Q), XMT_i_nz(Q), ...)
.
.
F42
F =@(Q) [F1(Q);F2(Q);F3(Q);F4(Q);F5(Q);F6(Q);F7(Q);F8(Q);F9(Q);F10(Q);...
F11(Q);F12(Q);F13(Q);F14(Q);F15(Q);F16(Q);F17(Q);F18(Q);F19(Q);F20(Q);...
F21(Q);F22(Q);F23(Q);F24(Q);F25(Q);F26(Q);F27(Q);F28(Q);F29(Q);F30(Q);...
F31(Q);F32(Q);F33(Q);F34(Q);F35(Q);F36(Q);F37(Q);F38(Q);F39(Q);F40(Q);...
F41(Q);F42(Q)];
options = optimoptions('fsolve','Algorithm','trust-region');
x0 = ones(1,225);
x0(1,1:3) = 0;
x0(1,7:9) = 0;
[out,fval,exitflag,output] = fsolve(F,x0,options);
end
I get this error message:
Dot indexing is not supported for variables of this type.
Error in equilibrium_F>@(Q)cons_n.Tax_rev_cons(Q) (line 129)
Tax_rev_cons_n = @(Q) cons_n.Tax_rev_cons(Q);
I have have two questions with regard to this:
  1. How can I solve the issue of using an indexed function output as a function of original inputs.
  2. Using fsolve needs initial values but the number of variables and the system complexity is very high to be able to provides these. Is there a way to get these somehow generically? or is there another way to sovle such large system of nonlinear equations other than fsolve?
Many thanks in advance for your help.

Réponse acceptée

Steven Lord
Steven Lord le 25 Fév 2021
Dot notation is indeed not defined for function handles. You can't do:
f = @sin;
% Commenting this code that would error so the block of code later in this answer can run
%{
g = f.x
%}
If your function handle returns a struct array and you're using a fairly recent release of MATLAB (I don't remember which one introduced this behavior) you could index directly into the result of evaluating the function handle.
q = @whos;
names = {q().name}
names = 1x2 cell array
{'f'} {'q'}
But that would require your cons_n to return a struct array and for you to call it then index into the output.
%{
XA_i_n = @(Q) cons_n(Q).XA_i;
%}
  2 commentaires
Steven Lord
Steven Lord le 25 Fév 2021
I went to the Release Notes to check and this functionality was introduced in release R2019b.
Moutaz Altaghlibi
Moutaz Altaghlibi le 25 Fév 2021
I updated my matlab to 2020b release and it worked. Thank you so much Steven!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by