Nested for loops to optimize price

12 views (last 30 days)
Yousif Alhammadi
Yousif Alhammadi on 7 Dec 2019
Commented: Walter Roberson on 7 Dec 2019
Testing various parameters to find the one that gives the most profit, through price equation:
Price = (0.12*P)-(Price of Reactor +0.04*M+1400000*Q). How to test all possible combinations of P, price of reactor, M, and Q to find the largest price/i.e. most profitable.
function Project = optimize(T)
T=[298.15:873.15];
length(T);
K=zeros(1,length(T));
myanswer=nan(2,length(T));
M=linspace(1,100,length(T));
Q=zeros(1,length(T));
Price=zeros(1,length(T));
for i=1:length(T)
K(i)=exp(-20000/(8.314*T(i)));
end
for j=1:length(T);
syms P
eqn = K(j)*M(j)^2-(2*M(j)*P*K(j))+(K(j)*P^2)-P;
myanswer(:,j)=solve(eqn);
myanswer(:,j)=double(myanswer(:,j));
end
P1=myanswer(1,:);
P2=myanswer(2,:);
r=linspace(2,100,length(T));
A=zeros(1,length(r));
for k=1:length(r)
A(k)=10*3.14*(r(k))^2;
Q(k)=25*3.14*r(k)^2*(T(k)-298.15);
end
Priceofreactor=A*150000;
Q=Q/0.12; %% accounts for 12% efficiency
P1=double(P1);
for d=1:length(T)
Price(d) = 0.12*P1(d)-(Priceofreactor(d)+0.04*M(d)+140*Q(d));
end
max(Price)
end
  1 Comment
Walter Roberson
Walter Roberson on 7 Dec 2019
for j=1:length(T);
syms P
eqn = K(j)*M(j)^2-(2*M(j)*P*K(j))+(K(j)*P^2)-P;
myanswer(:,j)=solve(eqn);
myanswer(:,j)=double(myanswer(:,j));
end
That can be done much more efficiently.
syms k m P
eqn = k*m^2-(2*m*P*k)+(k*P^2)-P;
sol = solve(eqn,P);
myanswer = double( subs(sol, {k,m}, {K,M}) );

Sign in to comment.

Answers (0)

Categories

Find more on Optimization Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!