How to Shorten running time

Hi, I am new to matlab and I am given an assignment.
I am supposed to find the most suitable feed rate (Ft) that produces the highest amount of biomass, lowest ethanol and uses the least glucose.
How do I shorten the running time for this code? It has been running for quite a while. If there is anything wrong with it, please let me know. I read that maybe parfor loops can shorten running time but I have not learnt them in class and I am unsure of how to use it. Is it applicable in this code?
%given variables
Agitator_speed=803;
Aeration_rate=1;
Fermenter_volume=618;
Fermenter_initial_glucose_concentration=32;
Fermentation_temperature=39;
Feed_glucose_concentration=111;
Batch_time=1:60;
Total_biomass_produced=5606.3595;
Total_ethanol_produced=0.0046638;
Total_glucose_used=4599.3704;
%feed rate
a=1; b=1;
D1=a*ones(1,60)';
D2=b*[60,120]'/60;
Ft=[D1;D2];
best_batch_time=100;
best_ethanol_produced=100;
best_glucose_used=100;
best_biomass_produced=1;
while react_vol<Fermenter_volume
%calculate the values of each variable
for i=1:100
x=biomass_conc(:,i);
y=react_vol(:,i);best_ethanol_produced;
q=ethanol_conc(:,i);
m=gluc_conc(:,i);
total_biomass_produced=x*y;
total_ethanol_produced=q*y;
total_glucose_used=m*y;
end
%checking if the current Ft is the optimum (comparing variables)
if Batch_time<=best_batch_time
best_batch_time=Batch_time;
if total_ethanol_produced<=best_ethanol_produced
best_ethanol_produced=total_ethanol_produced;
if total_glucose_used<=best_glucose_used
best_glucose_used=total_glucose_used;
if total_biomass_produced>=best_biomass_produced
best_biomass_produced=total_biomass_produced;
end
end
end
best_feed_rate=Ft;
end
a=a+1;b=b+1;
end
fprintf('the batch time is %d\n',best_batch_time)
fprintf('the total ethanol produced is %d\n',best_ethanol_produced)
fprintf('the total glucose used is %d\n',best_glucose_used)
fprintf('the total biomass produced is %d\n',best_biomass_produced)

3 Comments

Right off the bat, the code doesn't even run as given, so either something is missing or it's thrown an error. You're referencing arrays that don't exist:
x=biomass_conc(:,i);
y=react_vol(:,i);best_ethanol_produced;
q=ethanol_conc(:,i);
m=gluc_conc(:,i);
I can't guess what size they're supposed to be.
Furthermore, that second line is two statements:
y=react_vol(:,i); % this statement assigns something to a variable
best_ethanol_produced; % this statement does nothing
This logical test doesn't look like it's going to work either
if Batch_time<=best_batch_time
You're comparing a scalar and a vector; you'll wind up with a logical vector instead of a single logical value
There is a protected file that contains the values to this script.
what do you mean by comparing a scalar and a vector? could you elaborate.
1:10 < 7
ans = 1×10 logical array
1 1 1 1 1 1 0 0 0 0
Left side of the < is a vector (like Batch_time is a vector) and right side is a scalar (like best_batch_time is). The result, as you can see, as a logical vector that indicates whether each individual comparison was true.
When you have a vector test like that in an if statement, the if is considered true only if every value being tested is non-zero. Equivalent, that is to say, to
all(1:10 < 7)
ans = logical
0
which is false if there is even one entry which is 0 (false)
for i=1:100
x=biomass_conc(:,i);
y=react_vol(:,i);best_ethanol_produced;
q=ethanol_conc(:,i);
m=gluc_conc(:,i);
total_biomass_produced=x*y;
total_ethanol_produced=q*y;
total_glucose_used=m*y;
end
Notice that you overwrite total_biomass_produced and total_ethonal_produced and total_glucose_produced every iteration of for i

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 Mar 2021

Commented:

on 22 Mar 2021

Community Treasure Hunt

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

Start Hunting!