Steepest Ascent Method to find maximum

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

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
Syed Abdul Rafay 2022년 12월 13일
But h is unknown and if I don't use symbolic function the code doeasn't proceed with error unrecognized variable 'h'. I have to use the h value calculate in x new to get a updated value of x .
Torsten
Torsten 2022년 12월 13일
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.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 12월 13일

댓글:

2022년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by