How to use fsolve to solve matrix equations involving matrices of different dimensions?
15 次查看(过去 30 天)
显示 更早的评论
I have the following equation to solve using fsolve: (all elements in matrices A0, A1, A2, A3, B1, B2 are given parameter values)
A0 = [1 0 sigma; -kappa 1 0; -phi_x -phi_pi 1];
A1 = [1 sigma 0; 0 beta 0; 0 0 0];
A2 = [1 0; 0 1; 0 0];
A3 = [0 0 0; 0 0 0; sigma_i 0 0];
B1 = [rho_g 0, 0 rho_u];
B2 = [0 sigma_g 0; 0 0 sigma_u];
F0 = fsolve(@(x) A1*x*B1 - A0*x + A2, zeros(3,2));
However, I get the following error:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches
the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*)
for elementwise multiplication.
Error in Standard_NK_economy>@(x)A1(ir,:)*x*B1(:,ir)-A0(ir,:)*x+A2(ir,:) (line 29)
f0 = fsolve(@(x) A1(ir,:)*x*B1(:,ir) - A0(ir,:)*x + A2(ir,:), zeros(1,2)); % x is 1x2
Error in fsolve (line 270)
fuser = feval(funfcn{3},x,varargin{:});
Error in Standard_NK_economy (line 29)
f0 = fsolve(@(x) A1(ir,:)*x*B1(:,ir) - A0(ir,:)*x + A2(ir,:), zeros(1,2)); % x is 1x2
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Related documentation
2 个评论
Dyuman Joshi
2024-1-10
Please share the mathematical formulation of the equations you are trying to solve.
There are undefined variables in the code above, thus we can not run your code.
采纳的回答
Torsten
2024-1-10
编辑:Torsten
2024-1-10
Your equations are linear in the unknowns - so you should use a linear solver, not a nonlinear one like "fsolve":
% Calibration
sigma = 1;
kappa = 0.2;
beta = 0.99;
phi_pi = 1.5;
phi_x = 0.5;
sigma_i = 0.0025;
sigma_g = 0.0025;
sigma_u = 0.0025;
rho_g = 0.5;
rho_u = 0.3;
% Define A0, A1, A2, A3 for the 3 DSGE equations, where Z = [x; pi; i], S =
% [g; u], and eps = [eps_i; eps_g; eps_u]
A0 = [1 0 sigma; -kappa 1 0; -phi_x -phi_pi 1];
A1 = [1 sigma 0; 0 beta 0; 0 0 0];
A2 = [1 0; 0 1; 0 0];
A3 = [0 0 0; 0 0 0; sigma_i 0 0];
% Define B1, B2 for the two state equations: S_t = B1*S_t-1 + B2*eps_t
B1 = [rho_g 0; 0 rho_u];
B2 = [0 sigma_g 0; 0 0 sigma_u];
X = sym('X',[size(A1,2),size(B1,1)]);
size(X)
eqns = A1*X*B1 - A0*X + A2 == 0;
symvar(eqns)
[A,b] = equationsToMatrix(eqns)
F0 = A\b
F0 = [F0(1),F0(2);F0(3),F0(4);F0(5),F0(6)];
F1 = A0\A3 % inv(A0)*A3
G1 = F0*B1
G2 = F0*B2 + F1
6 个评论
更多回答(1 个)
Dyuman Joshi
2024-1-10
编辑:Dyuman Joshi
2024-1-10
% Calibration
sigma = 1;
kappa = 0.2;
beta = 0.99;
phi_pi = 1.5;
phi_x = 0.5;
sigma_i = 0.0025;
sigma_g = 0.0025;
sigma_u = 0.0025;
rho_g = 0.5;
rho_u = 0.3;
% Define A0, A1, A2, A3 for the 3 DSGE equations, where Z = [x; pi; i], S =
% [g; u], and eps = [eps_i; eps_g; eps_u]
A0 = [1 0 sigma; -kappa 1 0; -phi_x -phi_pi 1];
A1 = [1 sigma 0; 0 beta 0; 0 0 0];
A2 = [1 0; 0 1; 0 0];
A3 = [0 0 0; 0 0 0; sigma_i 0 0];
% Define B1, B2 for the two state equations: S_t = B1*S_t-1 + B2*eps_t
B1 = [rho_g 0; 0 rho_u];
B2 = [0 sigma_g 0; 0 0 sigma_u];
%size of x
r = 3;
c = 2;
F0 = fsolve(@(x) fun(x, A0, A1, A2, B1, r, c), zeros(r, c))
F1 = A0\A3 % inv(A0)*A3
G1 = F0*B1
G2 = F0*B2 + F1
%% Function definition
%Define equations to be solved as a function
function y = fun(x, A0, A1, A2, B1, r, c)
x = reshape(x, r, c);
y = A1*x*B1 - A0*x + A2;
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!