How to apply gradient metod to a single variable optimization problem

As in the title I'm having a problem with optimizing a single variable function. I have already managed to make a proper function through interpolation methods and said function is listed in the code as 'J'. Now, what I have to do is to use gradient method to maximize it in range of T = 0:120. However I'm lackng knowledge on how to do so.
I've been trying different approaches so far, using fmincon or fminsearch (which BTW I was told I can't use for some reason), searching the web for some answers but nothing worked for me so far. Eventually I decided that gradient would be the best way to solve this, but at this point I really struggle to come up with a solution.
I think I'm either lacking some knowledge regarding how gradient optimization should work, or I'm missing something. Either way, I'd appreciate any help what should I do next.
Below is the code I have so far, excluding the part where Lagrange Interpolation is calculated, as I know for sure that part works well.
% Data given
Cp = 85;
Cs = 22;
Kr = 15000;
Kp = 1320;
Kop = 4000;
tau = 10;
syms T;
syms Cp;
syms Cs;
syms Kr;
syms Kp;
syms Kop;
syms tau;
output = (-0.000591914003251751*T.^5 +0.0157328896604937*T.^4 -0.255479792438275*T.^3 +2.25724123015864*T.^2 -8.07704906204852*T +199.7);
input = (-0.000265610835537946*T.^5 +0.00716439790013456*T.^4 -0.112811639550303*T.^3 +0.936296091270008*T.^2 -2.55652056277004*T +240.1);
Gsx =Cp*output - Cs*input;
IGsx = int(Gsx,T);
%%
J = (1./(T+tau))*((IGsx) - Kr - Kp*tau - Kop*T);
Jd = diff(J,T);

 採用された回答

Torsten
Torsten 2022 年 6 月 5 日
編集済み: Torsten 2022 年 6 月 5 日
As you can see, without giving values to Cp, Cs, tau, Kp, Kop and Kr, you cannot solve for the critical points of Jd since you had to solve for the roots of a 6th order polynomial.
So assign values to the symbolic variables and use "fminsearchbnd" for optimization:
% Data given
Cp = 85;
Cs = 22;
Kr = 15000;
Kp = 1320;
Kop = 4000;
tau = 10;
syms T;
syms Cp;
syms Cs;
syms Kr;
syms Kp;
syms Kop;
syms tau;
output = (-0.000591914003251751*T.^5 +0.0157328896604937*T.^4 -0.255479792438275*T.^3 +2.25724123015864*T.^2 -8.07704906204852*T +199.7);
input = (-0.000265610835537946*T.^5 +0.00716439790013456*T.^4 -0.112811639550303*T.^3 +0.936296091270008*T.^2 -2.55652056277004*T +240.1);
Gsx =Cp*output - Cs*input;
IGsx = int(Gsx,T);
%%
J = (1./(T+tau))*((IGsx) - Kr - Kp*tau - Kop*T);
Jd = diff(J,T);
T=solve(Jd==0,T
T = 

4 件のコメント

Okay, I tried to apply this, though I don't understand how would I apply those values to symbolic variables. I tried doing it throu 'subs' option but it didn't work. Should I use the solve syntax as solve(___,Name,Value)? And if so, how would that look for multiple variables? Also, after doing that, in the fminsearchbnd, what should i input as a function? T or Jd?
Jd = diff(J,T);
T = solve(Jd==0,T,)
subs(Cp,85);
subs(Cs,22);
subs(Kr,1500);
subs(Kp,1320);
subs(Kop,4000);
subs(tau,10);
[x,fval,exitflag] = fminsearchbnd(Jd,0,0,120)
Torsten
Torsten 2022 年 6 月 5 日
編集済み: Torsten 2022 年 6 月 5 日
Your function looks quite strange:
syms T Cp Cs Kr Kp Kop tau
output = (-0.000591914003251751*T.^5 +0.0157328896604937*T.^4 -0.255479792438275*T.^3 +2.25724123015864*T.^2 -8.07704906204852*T +199.7);
input = (-0.000265610835537946*T.^5 +0.00716439790013456*T.^4 -0.112811639550303*T.^3 +0.936296091270008*T.^2 -2.55652056277004*T +240.1);
Gsx =Cp*output - Cs*input;
IGsx = int(Gsx,T);
%%
J = (1./(T+tau))*((IGsx) - Kr - Kp*tau - Kop*T);
J = subs(J,[Cp Cs Kr Kp Kop tau],[85 22 1500 1320 4000 10]);
J = matlabFunction(J);
T = 0:1:120;
plot(T,J(T))
%[x,fval,exitflag,output]=fminsearchbnd(J,60,0,120)
Szymon Zajac
Szymon Zajac 2022 年 6 月 5 日
Thank you, that really helped me a lot, because at some point I felt like I was running in circles. I will still explore ways of doing it without fminsearchbnd command (because the person evaluating this might require me to do it "by hand" so to say), however I belive the subs part was the one I was stuck on.
Once again thank you and have a good day sir!
Torsten
Torsten 2022 年 6 月 5 日
編集済み: Torsten 2022 年 6 月 5 日
To do it "by hand", use polynomials for input and output of degree <= 3 in T.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021b

質問済み:

2022 年 6 月 5 日

編集済み:

2022 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by