Out of memory. Type "help memory" for your options.
5 次查看(过去 30 天)
显示 更早的评论
At one point I feel so shy to ask because I have asked alot within 2 hours. But I really need your help, Out of Memory? That doesn't even make sense right? I currently in my project's for Iterative Method. But I can't run the coding because of this problem.
P(:,i)=(Lamda(i,1)- beta)./(gamma*2);
Attached below is my full coding, can you guys help me?
clear
clc
% n all alpha beta gamma min max
d=[1 671 10.100 0.000299 150 455
2 574 10.200 0.000183 150 455
3 374 8.8000 0.001126 20 130
4 374 8.8000 0.001126 20 130
5 461 10.400 0.000205 150 470
6 639 10.100 0.000301 135 460
7 548 9.8000 0.000364 135 465
8 227 11.200 0.000338 60 300
9 173 11.200 0.000807 25 162
10 175 10.700 0.001203 25 160
11 186 10.200 0.000371 20 80
12 230 9.9000 0.001929 20 80
13 225 13.100 0.004447 25 85
14 309 12.100 0.001929 15 55
15 323 12.400 0.004447 15 55];
Pd=2630; %Pdemand
alpha=d(:,2); %%%%%%%%%%%%%%
n=d(:,1);
beta=d(:,3);
gamma=d(:,4);
Pmin=d(:,5);
Pmax=d(:,6);
i=1;
Iteration=i;
%set Lamda
Lamda(i,1) = 5;
%step 2 -- calculate P total
P(:,i)=(Lamda(i,1)-beta)./(gamma*2);
P_total(i,1) = sum(P(:,i));
costI(:,i)=alpha+(beta.*P(:,i))+(gamma.*P(:,i).*P(:,i));
totalCost_iteration(i,1)=sum(costI(:, i));
%step 3 -- calculate error//delP
e(i,1) = Pd - P_total(i,1);
%step 4 -- find new lambda
if e(i,1) >0
L=Lamda(i,1)+(Lamda(i,1))*(0.1);
i=i+1;
Iteration(i,1)=i;
Lamda(i,1) =L;
elseif e(i,1) <0
L=Lamda(i,1)-(Lamda(i,1))*(0.1);
i=i+1;
Iteration(i,1)=i;
Lamda(i,1) =L;
end
%calculate P total for second iteration
P(:,i)=(Lamda(i,1)-beta)./(gamma*2);
P_total(i,1) = sum(P(:,i));
e(i,1) = Pd - P_total(i,1); costI(:,i)=alpha+(beta.*P(:,i))+(gamma.*P(:,i).*P(:,i));
totalCost_iteration(i,1)=sum(costI(:, i));
%step 5 -- find new lambda and P total until error =0
while abs(e(i,1))>0.00000000001
i=i+1;
Iteration(i,1)=i;
Lamda(i,1) =((Lamda(i-2,1)*(e(i-1,1)-e(i-2,1)))-(e(i-2,1)*(Lamda(i-1,1)-Lamda(i- 2,1))))./(e(i-1,1)-e(i-2,1));
P(:,i)=(Lamda(i,1)- beta)./(gamma*2);
P_total(i,1) = sum(P(:,i));
e(i,1) = Pd - P_total(i,1);
costI(:,i)=alpha+(beta.*P(:,i))+(gamma.*P(:,i).*P(:,i));
totalCost_iteration(i,1)=sum(costI(:, i));
end
%find total cost and total power
cost=alpha+(beta.*P(:,i))+(gamma.*P(:,i).*P(:,i));
TotalCost=sum(costI(:,i)); Totalpower= sum(P(:,i));
table(d(:,1),P(:,i),cost,'V',{'Unit' 'Power' 'Cost'})
display(TotalCost);
display(Totalpower);
figure
plot(Iteration,totalCost_iteration)
title('Convergest Graph')
xlabel('Number of Iteration')
ylabel('Cost(RM/h)')
0 个评论
回答(1 个)
Aashray
2025-6-26
The "Out of Memory" error in this case doesn't mean your computer ran out of RAM. It happened because of a mistake that causes MATLAB to try and create an impossibly huge array.
The issue is in this line inside the while loop:
Lamda(i,1) = ((Lamda(i-2,1)*(e(i-1,1)-e(i-2,1))) - (e(i-2,1)*(Lamda(i-1,1)-Lamda(i-2,1)))) ./ (e(i-1,1) - e(i-2,1));
The formula assumes i-2 exists. However, at the 2nd iteration (i=2), i-2=0, which is invalid in MATLAB indexing (because MATLAB indexes start at 1).
This creates “NaNs”, which then causes size mismatches and MATLAB tries to grow arrays indefinitely, which causes Out of Memory error.
To fix this, you can just check if there are at least 3 iterations before using i-2:
if i >= 3
Lamda(i,1) = ((Lamda(i-2,1)*(e(i-1,1)-e(i-2,1)))-(e(i-2,1)*(Lamda(i-1,1)-Lamda(i-2,1))))/(e(i-1,1)-e(i-2,1));
else
if e(i,1) > 0
Lamda(i,1) = Lamda(i-1,1) + (Lamda(i-1,1))*0.1;
else
Lamda(i,1) = Lamda(i-1,1) - (Lamda(i-1,1))*0.1;
end
end
You may refer to the following documentation links for more understanding:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!