Steepest Ascent Method to find maximum
Ältere Kommentare anzeigen
I am looking for a problem in my implementation for steepest ascent method. My answer is in a long equation form with h in it. But I am calculating h in the loop still ther is no value of it for the answer. The solution is at the end of the function
function STPAscent(x,theta,TC)
x0 =1500;
theta0=pi/6;
i=0;
i_max=100;
ea=5;
es=1;
while (es<double(subs(ea)) && i<i_max)
fx = tan(theta0)/(2*100^2*(cos(theta0))^2) + (2*9.81*x0)/(2*100^2*(cos(theta0))^2); % derivative wrt "x"
ftheta= x0*(sin(theta0)*(100*sin(theta0)+981*x0*cos(theta0))+50)/(1000000*(cos(theta0))^4); % derivative wrt "theta"
syms h
x=x0+fx*h;
theta=theta0+ftheta*h;
f= 1000 + (x*tan(theta))/(2*100^2*(cos(theta))^2) + (9.81*x^2)/(2*100^2*(cos(theta))^2);
c=diff(f,h);
assume(h,'clear')
h=solve(c==0,h,'PrincipalValue',true);
x_new=x+fx*h;
theta_new=theta0+ftheta*h;
ea=abs((x_new-x0)/x_new)*100;
x0=x_new;
theta0=theta_new;
i=i+1;
end
fxtheta = 1000 + (x0*tan(theta0))/(2*100^2*(cos(theta0))^2) + (9.81*x0^2)/(2*100^2*(cos(theta0))^2)
simplify(fxtheta)
end
Command Window
Warning: Unable to solve symbolically. Returning a numeric
solution using vpasolve.
> In sym/solve (line 304)
In STPAscent (line 18)
fxtheta =
(981*((8836235812531391*h)/4503599627370496 + 1499.9993954193889930311396801317)^2)/(2000000*cos(pi/6 - 0.52363352427033321420523588398365)^2) + (tan(pi/6 - 0.52363352427033321420523588398365)*((8836235812531391*h)/4503599627370496 + 1499.9993954193889930311396801317))/(20000*cos(pi/6 - 0.52363352427033321420523588398365)^2) + 1000
ans =
0.0018882263675909642813601409431637*h^2 + 2.8871384744661982638760370739896*h + 2103.6241090862540686603690416302
3 Kommentare
Torsten
am 13 Dez. 2022
There is a weird mix of symbolic and numeric variables in your code.
Updating x as
x_new=x+fx*h;
has two different h's in it: The x contains a symbolic h, the h in f*h is still symbolic, but has a value that comes from
h=solve(c==0,h,'PrincipalValue',true);
In my opinion, it's nonsense to use symbolic maths in numerical optimization because it slows down the computational speed and gives problems from the mix as in your code above.
Syed Abdul Rafay
am 13 Dez. 2022
Torsten
am 13 Dez. 2022
You can define a function dependent on an unknown h for which you want to find the root and use "fzero" or "fsolve" to solve for h.
That's exactly what "solve" will do in your case because it doesn't find an analytical solution for h.
Antworten (0)
Kategorien
Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!