Nested for loops to optimize price

조회 수: 12 (최근 30일)
Yousif Alhammadi
Yousif Alhammadi 2019년 12월 7일
댓글: Walter Roberson 2019년 12월 7일
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
Walter Roberson
Walter Roberson 2019년 12월 7일
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}) );

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Optimization Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by