Info

此问题已关闭。 请重新打开它进行编辑或回答。

least square fit matrix dimension errors

1 次查看(过去 30 天)
msh
msh 2014-11-24
关闭: MATLAB Answer Bot 2021-8-20
Hi,
I am trying to run a nonlinear regression
my code is the following:
gamma = 3;
eps = 1.5;
alpha = 0.36;
delta = 0.06;
beta = 0.8930;
Abar = 0.4477;
sigma = 0.3726;
sigmaA = 0.05;
rho = 0.95;
z = 10;
% initial Guess
be1=[0.1690; 0.6491; 0.7317; 0.6477; 0.4509; 0.5470];
be2=[0.2963; 0.7447; 0.1890; 0.6868; 0.1835; 0.3685];
be3=[0.6256; 0.7802; 0.0811; 0.9294; 0.7757; 0.4868];
% Initialization
T=100;
A=rand(T,1);
eta=rand(T,1);
kh=zeros(T,1)+1.4837;
theta=zeros(T,1)+0.4026;
psi1=zeros(T,1);
psi2=zeros(T,1);
psi3=zeros(T,1);
v=zeros(T,1);
Rk=zeros(T,1);
Yi=zeros(T,1);
Rap=zeros(T,1);
fi1=zeros(T,1);
fi2=zeros(T,1);
fi3=zeros(T,1);
theta_y=zeros(T,1);
kh_y=zeros(T,1);
v_y=zeros(T,1);
% X - Data
Xx = zeros(T,length(be1));
for t=1:T
Xx(t,:)=[1 log(kh(t)) log(A(t))...
log(kh(t)^2) log(A(t)^2) ...
log(kh(t))*log(A(t))];
% Xx(t,:)=[1 log(kh(t)) log(A(t)) (eta(t))...
% log(kh(t)^2) log(A(t)^2) log((eta(t)^2))...
% log(kh(t))*log(A(t)) log(kh(t))*(eta(t)) log(A(t))*(eta(t))];
psi1(t)=exp(Xx(t,:)*be1); % data from the true function
end
% Y-data
for t=1:T
Rk(t)=1+alpha*kh(t).^(alpha-1)-delta;
Yi(t)=(1-alpha)*kh(t).^alpha - alpha*kh(t).^(alpha-1)+eta(t);
Rap(t)=Rk(t).^(1-gamma)+(1-gamma)*Rk(t).^(-gamma)*theta(t)*Yi(t)-0.5*gamma*(1-gamma)*(theta(t)*Yi(t)).^2;
fi1(t)=(v(t).^(1-gamma))*(1+alpha*kh(t).^(alpha-1)-delta)*((1-alpha)*kh(t).^alpha-alpha*kh(t).^(alpha-1)+eta(t));
end
fun=@(be1,Xx)exp(Xx*be1);
[be_new] = nlinfit(psi1,fi1,fun,be1);
In the last line of code, I got always an error due to inconsistent matrix dimension multiplication. I understood the problem, however I did not manage to get it into a form that I could use the "nlinfit" built in function by MATLAB. Can someone help me on this ?
Many thanks

回答(1 个)

Thorsten
Thorsten 2014-11-24
You forgot to define alpha, delta, gamma; if they are rand(T,1); you have to use alpha(t), delta(t), gamma(t).
  1 个评论
msh
msh 2014-11-24
Those are just parameters that I forgot to put here. Sorry about this. I believe that I am not sure I understood the way nlinfit works.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by