how do I calculate all values in for loop?

Hello. please help me
I want to calculate in each x = [0, 2, 5, 7, 8, 9, 10 ] -----> Output P equation.
I tried to write in below code. BUT when its only x = 10, result calculated. I want to calculate all x values.
How do I do?
PLEASE
m-file
function [Tc, P] = myfun1(Ppv_rated, x, G , Gref, Kt, Tref, Tamb)
for x = [0,2,5,7,8,9,10]
Tc = Tamb + (0.0256 * G);
P = (Ppv_rated * x) .* (G/Gref).*(1 + Kt*(Tc - Tref));
end
end
COMMAND WINDOW
>> Ppv_rated = 250;
>> G = [5.75, 6, 6.365, 6.5, 6.185, 5.75, 5, 4.8, 5.5, 6.18, 6.15, 5.8];
>> Gref = 1000;
>> Kt = -0.485;
>> Tref = 25;
>> Tamb = [25.63, 26.7, 26.610, 25.46, 24.9, 24.01, 23.16, 23.01, 23.54, 23.78, 24.45, 25.3];
>> [Tc, P] = myfun1(Ppv_rated, x, G , Gref, Kt, Tref, Tamb)
RESULT

Réponses (2)

KSSV
KSSV le 10 Sep 2020
Modifié(e) : KSSV le 10 Sep 2020
You need to save each value in aloop.
function [Tc, P] = myfun1(Ppv_rated, x, G , Gref, Kt, Tref, Tamb)
m = numel(x);
n = numel(Tamb) ;
Tc = zeros(m,n) ;
P = zeros(m,n) ;
for i = 1:length(x)
Tc(i,:) = Tamb + (0.0256 * G) ;
P(i,:) = (Ppv_rated * x(i)) .* (G/Gref).*(1 + Kt*(Tc(i,:) - Tref));
end
end
You need not to use a loop and you can use all vector operations.
function [Tc, P] = myfun1(Ppv_rated, x, G , Gref, Kt, Tref, Tamb)
Tc = Tamb + (0.0256 * G) ;
PP = ((Ppv_rated * x) .* ((G/Gref).*(1 + Kt*(Tc - Tref)))')';
end

8 commentaires

Thank you sir.
I understood. Its working. I have a question.
I want to input Ppv_rated, G, Gref, Kt, Tref, Tamb in this function not command window. Is it possible?
Ppv_rated = 250;
G = [5.75, 6, 6.365, 6.5, 6.185, 5.75, 5, 4.8, 5.5, 6.18, 6.15, 5.8];
Gref = 1000;
Kt = -0.485;
Tref = 25;
Tamb = [25.63, 26.7, 26.610, 25.46, 24.9, 24.01, 23.16, 23.01, 23.54, 23.78, 24.45, 25.3];
x = [0,2,5,7,8,9,10] ;
Tc = Tamb + (0.0256 * G) ;
PP = ((Ppv_rated * x) .* ((G/Gref).*(1 + Kt*(Tc - Tref)))')';
Sorry, sir
Is it impossible input in myfun1 function? Right?
function [Tc, P] = myfunc()
Ppv_rated = 250;
G = [5.75, 6, 6.365, 6.5, 6.185, 5.75, 5, 4.8, 5.5, 6.18, 6.15, 5.8];
Gref = 1000;
Kt = -0.485;
Tref = 25;
Tamb = [25.63, 26.7, 26.610, 25.46, 24.9, 24.01, 23.16, 23.01, 23.54, 23.78, 24.45, 25.3];
x = [0,2,5,7,8,9,10] ;
Tc = Tamb + (0.0256 * G) ;
P = ((Ppv_rated * x) .* ((G/Gref).*(1 + Kt*(Tc - Tref)))')';
end
After myfun1 function code finished , I want to call myfun1 function in PSO code.
problem.CostFunction = myfunc() ;
but it didn't work
another m-file using PSO code worked. But I don't know why myfunc m-file doesn't work.

Connectez-vous pour commenter.

VBBV
VBBV le 10 Sep 2020
Modifié(e) : VBBV le 10 Sep 2020
%if true
% code
% end
x = [0 2 5 7 8 9 10];
for i = 1:length(x)
for j = 1:length(Tamb)
Tc(j)= Tamb(j) + 0.0256*G(j);
P(i,j) = Ppv_rated*x(i)*(G(j)/Gref)*(1+Kt*(Tc(j)-Tref));
end
end

1 commentaire

Thank you sir.
I understood. Its working. I have a question.
I want to input Ppv_rated, G, Gref, Kt, Tref, Tamb in this function not command window. Is it possible?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by