% a -> lower bound of the design variable % b -> upper bound of the design variable % alpha -> midpoint of a and b % delx -> ?x for central difference method % derivative -> derivative using central difference method % derivative_alpha -> derivative at x = alpha % abs -> absolute of a number, MATLAB function % clear all clc a = 40; b = 90; epsilon = 0.01; delx = 0.01; fprintf(' a b \n') fprintf('-------------------------\n') for i= 1:100 fprintf(' %7.3f %8.3f \n',a,b) alpha = (a+b)/2; derivative = (func(a+delx) - func(a-delx) )/(2*delx); derivative_alpha = (func(alpha+delx)- func(alpha-delx))/(2*delx); if (derivative*derivative_alpha) < 0 b = alpha; else a = alpha; end if abs(a-b) < epsilon break; end end fprintf('-------------------------\n') fprintf('x* = %7.3f Minimum = %8.3f\n',a,func(a)) fprintf('Number of function calls = %3d\n',4*i) %
% *** Se crea un subprograma:
% MATLAB code func.m
% x -> input variable to the function
% fx -> output from the function % function fx = func(x) fx = 204165.5/(330-2*x) + 10400/(x-20); % % Se ingresa en Command window: % func(40) % func(90) % Resulta igual el resultado.
% *** Se genera la gráfica en un subprograma:
% MATLAB code exhaustive.m
% delta -> step size for search % T -> independent variable, temperature % U -> cost function % uvec -> vector of cost function evaluated at % different temperatures % minu -> minimum of cost function % min -> MATLAB function % clear all clc uvec=[]; delta = 0.01; for T = 40:delta:90 U = 204165.5/(330-2*T) + 10400/(T-20); uvec = [uvec U]; plot(T,U,'go') % Modificado: el color verde, % forma redonda. hold on end xlabel('T');ylabel('U'); [minu,i]= min(uvec); fprintf('Minimum Cost = %6.2f\n ',minu) fprintf('occurs at T = %6.2f\n ',40+(i-1)*delta) axis([40 90 1200 1550]); % Control de ejes.