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 views (last 30 days)
Show older comments
% 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);
% 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 Comment
Nicholas St. John
on 9 Feb 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.
Answers (2)
Walter Roberson
on 9 Feb 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:
- Ensure that you only ever call the function with a scalar
- Code with all() or any() to test the entire vector appropriately (not recommended)
- change the && to & (not recommended)
- Change the function entirely to use logical indexing
0 Comments
Nicholas St. John
on 9 Feb 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);
% 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
0 Comments
See Also
Categories
Find more on Powertrain Reference Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!