Nested loop for matrix

4 visualizaciones (últimos 30 días)
Mitali Murkar
Mitali Murkar el 15 de Dic. de 2022
Comentada: Voss el 17 de Dic. de 2022
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

Respuesta aceptada

Voss
Voss el 15 de Dic. de 2022
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)
ans = 1×2
6 400
  2 comentarios
Mitali Murkar
Mitali Murkar el 17 de Dic. de 2022
Hello Voss, thank you so much for the hint. It works now! Very helpful.
Voss
Voss el 17 de Dic. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 15 de Dic. de 2022
I'm guessing you intended
g(k,i)
rather than
g(k,:)
in the last line of the for loop.
  3 comentarios
the cyclist
the cyclist el 15 de Dic. de 2022
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.
Mitali Murkar
Mitali Murkar el 17 de Dic. de 2022
Thank you so much for clarification and your very prompt response.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by