File: ef230-2023-08/www/examples/optimization_trough.m Download
%! Solution to a trough optimization problem
%! Includes surface and parameter plotting
% University of Tennessee : Will Schleter
clc; clear all; close all;
lt = 'rgbcmyk';
xlist = linspace(0,6,7);        % x range
tlist = linspace(0,pi/2,7);     % theta range
x=meshgrid(xlist,tlist)';        % 2D matrix of x values
t=meshgrid(tlist,xlist);         % 2D matrix of y values
a = (12 - 2.*x).*x.*sin(t) + x.*x.*cos(t).*sin(t) % calculate areas
maxa = max(max(a));              % find maximum area
[max_ix,max_it] = find(a==maxa); % find where max occured
max_a = a(max_ix,max_it)         % max area
max_x = x(max_ix,max_it)         % x for max area
max_t = t(max_ix,max_it)*180/pi  % t for max area
figure('units','normalized','position',[.1 .1 .4 .4]);
hold on
for ii=1:1:length(xlist)
    tt = linspace(min(tlist),max(tlist),200);
    aa = spline(t(ii,:),a(:,ii),tt);
    plot(tt*180/pi,aa,[lt(ii) '-']);
    leg(ii,:)=sprintf('x=%2u',xlist(ii));
end
legend(leg,2);
xlabel('Slope (degrees)');
ylabel('Flow area (m^2)');
plot(max_t,max_a,'o');
msg = sprintf('Max=%.2f at x=%.0f and t=%.0f',max_a,max_x,max_t);
text(max_t,max_a,msg,'horiz','center','vert','bottom');
grid on;
figure('units','normalized','position',[.55 .1 .4 .4])
surf(x,t*180/pi,a)
rotate3d on
xlabel('X value (m)')
ylabel('Angle (degrees)')
zlabel('Flow area (m^2)')