MATLAB: How do i find max value of a two varible equation

max valuetwo varible equation

%equation X=-100*sin(t*omega)
omega=1:1:100
t=0:0.001:10

Best Answer

If you have a recent release, then you can use implicit expansion to calculate all combinations and find the t and omega that result in the maximum value. This will not find the true optimal values, but only the values allowed by your vectors. If you want to find the 'real' answers, you can use the second method (which only uses base functions, so no need for the curve fitting toolbox).
omega=1:1:100;
t=0:0.001:10;
X=-100*sin(t'*omega);
[r,c]=find(X==max(X(:)));
X_fun=@(t,omega) -100*sin(t*omega);
initialguess_t_omega=[7,13];
%invert the direction to use fminsearch to find the maximum
sol=fminsearch(...
@(inputvar) -X_fun(inputvar(1),inputvar(2)),...
initialguess_t_omega);
clc
fprintf('method 1: predetermined combinations\n')
fprintf('optimum for t=%.4f\n',t(r))
fprintf('optimum for omega=%d\n',omega(c))
fprintf('method 2: solve equation\n')
fprintf('optimum for t=%.4f\n',sol(1))
fprintf('optimum for omega=%.4f\n',sol(2))
Related Question