Getting an exception at the Power_usag​e.Rice_Coo​ker(Tstart​) = 0; line

1 回表示 (過去 30 日間)
Kaushalya
Kaushalya 2023 年 4 月 24 日
回答済み: Bhanu Prakash 2024 年 7 月 18 日
% Import data into a table
rice_cooker = [0.0000 0.0000 0.0000 0.0000 0.0863 0.5180 0.4317 0.0432 0.0863 0.0000 0.0000 0.0432 0.1295 0.1295 0.0000 0.0000 0.0000 0.0863 0.4748 0.3885 0.0432 0.0000 0.0000 0.0000]';
blender = [0.0000 0.0000 0.0000 0.0000 0.0207 0.1861 0.1861 0.0207 0.0207 0.0000 0.0207 0.0207 0.0207 0.0000 0.0000 0.0414 0.0414 0.0414 0.1034 0.0620 0.0000 0.0000 0.0000 0.0000
]';
hour = (1:24)';
Starting_Probability = table(hour, rice_cooker, blender);
Starting_Probability.Properties.VariableNames{'rice_cooker'} = 'Rice_Cooker';
Starting_Probability.Properties.VariableNames{'blender'} = 'Blender';
% Import data into another table
cycle_t_first_column = [0 0 0 0 41 32 31 30 0 0 30 30 15 40 0 0 0 20 35 29 45 0 0 0
]';
cycle_t_second_column = [0 0 0 0 10 8 7 9 15 0 13 18 21 10 15 0 9 11 8 7 10 0 0 0
]';
cycle_t = table(cycle_t_first_column,cycle_t_second_column);
cycle_t.Properties.VariableNames{'cycle_t_first_column'} = 'Rice_Cooker';
cycle_t.Properties.VariableNames{'cycle_t_second_column'} = 'Blender';
% Import data into another table
app_pwr_first_column = [0.000 0.000 0.000 0.000 1.525 1.271 1.220 0.700 0.000 0.000 1.400 1.550 1.000 1.333 0.000 0.000 0.000 1.300 1.600 1.260 1.500 0.000 0.000 0.000
]';
app_pwr_second_column = [0.000 0.000 0.000 0.000 0.550 0.543 0.682 0.325 0.978 0.000 0.328 0.365 0.525 0.775 0.374 0.000 0.138 0.494 0.567 0.507 2.000 0.000 0.000 0.000
]';
app_pwr = table(app_pwr_first_column,app_pwr_second_column);
app_pwr.Properties.VariableNames{'app_pwr_first_column'} = 'Rice_Cooker';
app_pwr.Properties.VariableNames{'app_pwr_second_column'} = 'Blender';
app_pwr.Properties.RowNames = cellstr(num2str((1:24)'));
% Create a table named Power_usage with two columns and 24 rows
Power_usage = table(zeros(24,1), zeros(24,1));
% Rename the columns as RiceCooker and Blender
Power_usage.Properties.VariableNames = {'Rice_Cooker', 'Blender'};
% Rename the rows as 1 to 24
Power_usage.Properties.RowNames = cellstr(num2str((1:24)'));
% Get the values of Tstart and Applno variables
Tstart = 2; %input('Enter the value of Tstart: ');
Applno = 1; %input('Enter the value of Applno: ');
%% Create a loop until Tstart equals 24
while Tstart <= 24
% Generate 100 random numbers between 0 and 1
x = rand(100,1);
% Assign the 100th random number to a variable named r
r = x(100);
% Check the random number against the element of T table
if Applno == 1 % RiceCooker column
if r < Starting_Probability.Rice_Cooker(Tstart) % Random number is greater than element value
% Read the value from app_pwr table and store it in Power_usage table
Power_usage.Rice_Cooker(Tstart) = app_pwr.Rice_Cooker(Tstart);
else % Random number is less than element value
% Store 0 in Power_usage table
Power_usage.Rice_Cooker(Tstart) = 0;
end
elseif Applno == 2 % Blender column
if r < Starting_Probability.Blender(Tstart) % Random number is greater than element value
% Read the value from app_pwr table and store it in Power_usage table
Power_usage.Blender(Tstart) = app_pwr.Blender(Tstart);
else % Random number is less than element value
% Store 0 in Power_usage table
Power_usage.Blender(Tstart) = 0;
end
end
% Get the value of Tcycle from cycle_t table
if Applno == 1 % RiceCooker column
Tcycle = cycle_t.Rice_Cooker(Tstart);
elseif Applno == 2 % Blender column
Tcycle = cycle_t.Blender(Tstart);
end
% Update Tstart as per the equation
Tstart = Tstart + Tcycle;
end
% I'm getting an error at the Power_usage.Rice_Cooker(Tstart) = 0; line

回答 (1 件)

Bhanu Prakash
Bhanu Prakash 2024 年 7 月 18 日
Hi Kaushalya,
For the while loop (in line 36) to terminate, the value of Tstart must be greater then 24. The value of Tstart is updated based on the value of Tcycle.
However it appears that the value of Tcycle is always zero (0). As a result, Tstart will always retain its initial value of 2, causing the code to run indefinitely.
To avoid this, you need to ensure that the value of Tcycle is greater than 0. This can be achieved by modifying the 'rice_cooker' matrix to have non-zero values, or by any other approach you seem appropriate for your use case.
Hope this helps!

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by