Nested loop for matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Below is my code. I want to calculate the value of g where my for loop iterates for i and K. It means I hould have 6 combinations of output.However, I am only getting 3 combinations of output saved in g. Could you please provid the hint or feeback on it? Thanks!
clc
clear all;
ED = readmatrix("Data filteration1.xlsx");
ki = 0.0042; %%current temp coeff
Ns=72; %%72cells in series and 1 in parellel
Np=1;
Ego=1.1;
K= 1.380*10^-23;
q= 1.602*10^-19;
Voc=48.37;
Isc=10.27;
G=[1000 800];
Tck = [25+273 20+273];
I=zeros(400,1);
Vi=ones(400,1);
X= [1.53 0.35 700;1 0.3 600;1.1 0.33 750];
x3=X(:,1);
x4=X(:,2);
x5=X(:,3);
for i=1:length(Tck) % I=2
for k=1:size(X,1) %k=3
Vt(k,i) = K.*Tck(i)/q;
Iph(k,i)= (Isc + ki .* (Tck(i)-298)).*(G(i)./1000);
Irs(k,i) = Isc./((exp((q.*Voc)./(x3(k,:).*Ns.*K.*Tck(i))))-1);
Io(k,i) = Irs(k,i).* (Tck(i)./298).^3 .* exp(((q.*Ego)./(x3(k,:).*K))*(1./298 - 1./Tck(i)));
g(k,:) = (Np.*Iph(k,i))-Io(k,i).*(exp((Vi+I.*x4(k,:))./Vt(k,i)/Ns./x3(k,:))-1)-((Vi+ I.*x4(k,:))./x5(k,:)) - I;
end
end
댓글 수: 0
채택된 답변
Voss
2022년 12월 15일
clc
clear all;
% ED = readmatrix("Data filteration1.xlsx");
ki = 0.0042; %%current temp coeff
Ns=72; %%72cells in series and 1 in parellel
Np=1;
Ego=1.1;
K= 1.380*10^-23;
q= 1.602*10^-19;
Voc=48.37;
Isc=10.27;
G=[1000 800];
Tck = [25+273 20+273];
I=zeros(400,1);
Vi=ones(400,1);
X= [1.53 0.35 700;1 0.3 600;1.1 0.33 750];
x3=X(:,1);
x4=X(:,2);
x5=X(:,3);
nT = length(Tck);
nX = size(X,1);
g = zeros(nT*nX,numel(I));
row = 1;
for i=1:nT % I=2
for k=1:nX %k=3
Vt(k,i) = K.*Tck(i)/q;
Iph(k,i)= (Isc + ki .* (Tck(i)-298)).*(G(i)./1000);
Irs(k,i) = Isc./((exp((q.*Voc)./(x3(k,:).*Ns.*K.*Tck(i))))-1);
Io(k,i) = Irs(k,i).* (Tck(i)./298).^3 .* exp(((q.*Ego)./(x3(k,:).*K))*(1./298 - 1./Tck(i)));
g(row,:) = (Np.*Iph(k,i))-Io(k,i).*(exp((Vi+I.*x4(k,:))./Vt(k,i)/Ns./x3(k,:))-1)-((Vi+ I.*x4(k,:))./x5(k,:)) - I;
row = row+1;
end
end
size(g)
추가 답변 (1개)
the cyclist
2022년 12월 15일
I'm guessing you intended
g(k,i)
rather than
g(k,:)
in the last line of the for loop.
댓글 수: 3
the cyclist
2022년 12월 15일
I'm not certain why you think it should be 6*400. If you look at your indexing, here is what happens during all the iterations of your for loops:
i==1, k==1
Write g(1,:)
i==1, k==2
Write g(2,:)
i==1, k==3
Write g(3,:)
i==2, k==1
Over-write to g(1,:)
i==2, k==2
Over-write to g(2,:)
i==2, k==3
Over-write to g(3,:)
===========================
Notice that you never use i to index into g, so you are overwriting instead of creating rows 4-6.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!