Vectors must be the same length.

1 次查看(过去 30 天)
Hello everyone, can you please help me with this problem? I currently in my project's for Iterative Method. But I can't run the coding because of this problem.
plot(Iteration,totalCost_iteration);
Attached below is my full coding, can you guys help me?
clear clc;
% n alpha beta gamma min max
d=[1 550 8.1000 0.00028 0 680
2 309 8.1000 0.00056 0 360
3 307 8.1000 0.00056 0 360
4 240 7.7400 0.00324 60 180
5 240 7.7400 0.00324 60 180
6 240 7.7400 0.00324 60 180
7 240 7.7400 0.00324 60 180
8 240 7.7400 0.00324 60 180
9 240 7.7400 0.00324 60 180
10 126 8.6000 0.00284 40 120
11 126 8.6000 0.00284 40 120
12 126 8.6000 0.00284 55 120
13 126 8.6000 0.00284 55 120];
Pd=1800; %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)')
  1 个评论
Timo Dietz
Timo Dietz 2021-1-4
编辑:Timo Dietz 2021-1-4
?? The code works fine for me (Matlab2018a) - no error message, plot is done.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-1-4
I suspect the issue is with your opening line. It is currently clearing a variable named clc. I suspect your intent is to clear your workspace and clear you command window. If that is the case, you must either put each command on its own line, or separate the two with a comma or semicolon.
clear
clc
clear, clc
clear; clc
% Not this. The purple text indicates clc is being treated as text
clear clc

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by