我制作了一个包含一些符号函数的matlab代码
首先我运行此代码,它给了我一个结果。
但是试图再次运行相同的代码,出现了内存不足的消息。
有人有想法解决这个问题吗?
以下是我的代码。
clc;
clear;
tic;
rng('shuffle');
p=2; % AR order
yr=normrnd(0,1,1000,1);
T=max(size(yr));
for k=25:35;
intnum=k/10;
fore=100;
int=((max(yr)-min(yr))/intnum);
fin=T-fore;
% Initial setting matrix
y_T1=zeros(fore,1);
y_fore_w=zeros(fore,1); y_diff_w=zeros(fore,1);
y_fore_o=zeros(fore,1);y_diff_o=zeros(fore,1);
y_fore_f=zeros(fore,1);y_diff_f=zeros(fore,1);
y_fore_wl=zeros(fore,1);y_diff_wl=zeros(fore,1);
y_fore_m=zeros(fore,1);y_diff_m=zeros(fore,1);
y_gap=zeros(fore,1);
for e=fin:T-1 ;
y_1=yr(p+1:e);x_1=yr(p:e-1);x_2=yr(p-1:e-2);
A=[y_1 x_1 x_2];
st=1;
ye=yr(st:e);
coordinate = zeros(length(y_1), 3); % Giving new indices to A
y_1_diff=max(y_1)-min(y_1);x_1_diff=max(x_1)-min(x_1);x_2_diff=max(x_2)-min(x_2);
y_1_int=y_1_diff/int;x_1_int=x_1_diff/int;x_2_int=x_2_diff/int;
y_1_int_num=round(y_1_int);x_1_int_num=round(x_1_int);x_2_int_num=round(x_2_int);
for i=1:y_1_int_num;
coordinate(A(:,1) >= (i-1)*int+min(A(:,1)) & A(:,1) < i*int+min(A(:,1)), 1) = i ; % Terrific idea for some conditions in ROW
end
for i=1:x_1_int_num;
coordinate(A(:,2) >= (i-1)*int+min(A(:,2)) & A(:,2) < i*int+min(A(:,2)), 2) = i ; % Terrific idea for some conditions in ROW
end
for i=1:x_2_int_num;
coordinate(A(:,3) >= (i-1)*int+min(A(:,3)) & A(:,3) < i*int+min(A(:,3)), 3) = i; % Terrific idea for some conditions in ROW
end
sz=[y_1_int_num x_1_int_num x_2_int_num];
nucu=max(sz)^(p+1);
y_int_sub_num=zeros(nucu,1); % Number of frequency
yfunc=cell(nucu,1); % Cell of functions
c=1:max(sz);
C=combvec(c,c,c)'; % Generate a reference matrix to coordinate
d=sortrows(C);
syms c b1 b2;
for i=1:nucu;
a=ismember(coordinate, d(i,:), 'rows');
s=sum(a);
y_int_sub_num(i)=s;
B=A(a,:);
func=symfun((B(:,1)-c-b1*B(:,2)-b2*B(:,3)).^2, [c,b1,b2]);
yfunc1=sum(func);
yfunc{i}=yfunc1*y_int_sub_num(i);
clear func; clear yfunc1;
end
yfuncs=symfun(sym('f(c,b1,b2)'),[c,b1,b2]);
for j=1:nucu;
yfuncs=yfuncs+yfunc{j};
end
yfuncs= yfuncs-'f(c,b1,b2)';
y_trans = matlabFunction(yfuncs,'Vars',{[c;b1;b2]});
% WLLS
options = optimset('Algorithm','Interior-point',...
'TolFun',1e-8, 'MaxFunEvals',1000000,'MaxIter',1000000); % Set option
[b_w,fval] = fminsearch(y_trans, [0;0;0], options);
% OLS
c=ones(T-p,1);y_c=zeros(T-p,4);
y_c(:,1)=yr(p+1:T);y_c(:,2)=c;y_c(:,3)=yr(2:end-(p-1));y_c(:,4)=yr(1:end-2);
[b_0,bint_0,e_0,rint,stats] = regress(y_c(:,1), y_c(:,2:4));
% WLS
e_0_w=e_0.^2/sum(e_0.^2); %normalization required for Matlab
[bw,se_bw] = lscov(y_c(:,1), y_c(:,2:4), e_0_w);
% FGLS_iterated
[bf, Sigma] = lscov(y_c(:,1), y_c(:,2:4), e_0_w);
% [bf, Sigma]=fglsi(y_c(:,1), y_c(:,3), y_c(:,4), y_c(:,3));
% MLE
[b_m, b_mm, mu, sig] = regress(y_c(:,1), y_c(:,2:4)); % Since error, OL
% Foracast and Differences
n=e-(fin-1);
y_T1(n)=yr(e+1);
y_fore_w(n)=b_w(1)+b_w(2)*ye(end)+b_w(3)*ye(end-1);y_diff_w(n)=y_T1(n)-y_fore_w(n);
y_fore_o(n)=b_0(1)+b_0(2)*ye(end)+b_0(3)*ye(end-1);y_diff_o(n)=y_T1(n)-y_fore_o(n);
y_fore_wl(n)=bw(1)+bw(2)*ye(end)+bw(3)*ye(end-1);;y_diff_wl(n)=y_T1(n)-y_fore_wl(n);
y_fore_f(n)=bf(1)+bf(2)*ye(end)+bf(3)*ye(end-1);;y_diff_f(n)=y_T1(n)-y_fore_f(n);
y_fore_m(n)=b_m(1)+b_m(2)*ye(end)+b_m(3)*ye(end-1);;y_diff_m(n)=y_T1(n)-y_fore_m(n);
y_gap(n)=abs(y_diff_w(n))-abs(y_diff_o(n));ygap(n)=(y_gap(n)<=0);
end
toc
a=1;filename = [ 'comp', num2str(a), '.mat' ];
save(filename);
if mean(y_diff_w.^2) <=mean(y_diff_o.^2)
[int mean(y_diff_w.^2)]
beep;
end
z1=zeros(y_1_int+1,1);
for i=1:y_1_int+1;
z=(yr>=(i-1)*int+min(yr) & yr<i*int+min(yr));
z1(i)=sum(z);
end
z1
end
sort(yr)
[max(yr) min(yr)]