Borrar filtros
Borrar filtros

Please help me correct this error: Error in P_function>P_panelpower (line 26) elseif t>=6 && t<11 Error in P_function (line 10) P = P_panelpower(t);

2 visualizaciones (últimos 30 días)
% Defining time start and end time from midnight to 24th hour with 6mins intervals
tstart=0;
tend=24;
% Defining total number of samples including the end points
samples=(tend-tstart)/0.1 + 1;
% Defining time vector
t=linspace(tstart,tend,samples);
% Calculating P(t) and storing it in P
P = P_panelpower(t);
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

Error in solution>P_panelpower (line 23)
elseif t>=6 && t<11
% Plotting the panel power P(t)
plot(t, P);
xlabel('Time (hours)');
ylabel('Power output (kW)');
title('Power output of PV panel set');
%Total energy produced in an average day from the panel set
energy = sum(P) * 0.1; % Multipluing by 0.1 because each interval is 0.1 hour, basically from kWmin to kWh
% Displaying total energy
fprintf('Total energy produced in an average day: %.2f kWh\n', energy);
% Defining P(t) function using if else condition statments
function P = P_panelpower(t)
if t<6
P=0;
elseif t>=6 && t<11
P= 15*(1-cos(pi*((t-6)/5)));
elseif t>=11 && t<13
P= 30;
elseif t>=13 && t<18
P =15*(1+cos(pi*((t-13)/5)));
else
P=0;
end
end
  1 comentario
Nicholas St. John
Nicholas St. John el 9 de Feb. de 2024
Hi,
When you do comparison operators (< == > etc.) on an array, you get a logical array back indicating the response for each element, however, these if/else statements need a scalar value. My first guess would be to have a for loop and check for each element to do this.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 9 de Feb. de 2024
You are passing a vector to your function.
Inside your function you are testing the entire vector at the same time. If the entire vector is less than 6, you return a scalar 0. If the entire vector >=6 && less than 11 then you apply a transformation to the entire vector. And so on.
Except, when you test an entire vector you are not permitted to use the && operator, which is reserved for testing scalar operations.
Maybe you want
elseif all(t>=6) && all(t<11)
... but probably not.
You have several choices:
  1. Ensure that you only ever call the function with a scalar
  2. Code with all() or any() to test the entire vector appropriately (not recommended)
  3. change the && to & (not recommended)
  4. Change the function entirely to use logical indexing

Nicholas St. John
Nicholas St. John el 9 de Feb. de 2024
% Defining time start and end time from midnight to 24th hour with 6mins intervals
tstart=0;
tend=24;
% Defining total number of samples including the end points
samples=(tend-tstart)/0.1 + 1;
% Defining time vector
t=linspace(tstart,tend,samples);
% Calculating P(t) and storing it in P
P = P_panelpower(t);
% Plotting the panel power P(t)
plot(t, P);
xlabel('Time (hours)');
ylabel('Power output (kW)');
title('Power output of PV panel set');
%Total energy produced in an average day from the panel set
energy = sum(P) * 0.1; % Multipluing by 0.1 because each interval is 0.1 hour, basically from kWmin to kWh
% Displaying total energy
fprintf('Total energy produced in an average day: %.2f kWh\n', energy);
Total energy produced in an average day: 210.00 kWh
% Defining P(t) function using if else condition statments
function P = P_panelpower(t)
for i = 1:length(t)
if t(i)<6
P(i)=0;
elseif t(i)>=6 && t(i)<11
P(i)= 15*(1-cos(pi*((t(i)-6)/5)));
elseif t(i)>=11 && t(i)<13
P(i)= 30;
elseif t(i)>=13 && t(i)<18
P(i) =15*(1+cos(pi*((t(i)-13)/5)));
else
P(i)=0;
end
end
end

Categorías

Más información sobre Powertrain Reference Applications en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by