how to get maximum value of this code

3 views (last 30 days)
T = readtable('data1.xlsx') ;
T = table2array(T) ;
x = T(1,2:end); % coil length
y = T(2:end,1); % magnitude length
Z = T(2:end,2:end); % use fillmissing to fill NaNs
[X,Y] = meshgrid(x,y) ;
[xq,yq] = meshgrid(2:0.1:10); % grid interval 0.1
Zq = interp2(x,y,Z,xq,yq,'spline');
figure
AA = surf(xq,yq,Zq);
title('(spline,cubic,makima) Interpolation Using Finer Grid');
%%
% example
% if input (1, 1), in interpolation (2.0, 2.0)
% if input (23, 23) , in interpolation (42, 42)
% if input (81, 81) , in interpolation (10.0 , 10.0)
% Upper limit& Lower limit are 1~81
Zq(1,1)
Zq(23,23)
Zq(81,81)
%% optimmization algorithm
% source : https://sites.google.com/a/hydroteq.com/www/
clc
% HS: Harmony Search minimization
% Use the Matlab startup random number sequence:
rand('twister',5489); % Commment out to start rand from current position
% Specify objective function and bounds
f = Zq;
xL = [0.01 0.01]; % minimum range
xU = [0.81 0.81]; % maximum range
% Set HS algorithm parameters
HSparams.HMS = 10;
HSparams.MaxImp = 10000;
HSparams.HMCR = 0.8;
HSparams.PAR = 0.4;
HSparams.b = (xU-xL)/1000;
% Perform minimization
[xbest,fbest] = harmony(f,xL,xU,HSparams);
fprintf('Best solution found:\n')
disp(xbest)
fprintf('Function value = %f\n', fbest)
I want to find maxinmum of attached table. But when I use this code I get the minimum.
What should I fix to get the maximum value?

Accepted Answer

Star Strider
Star Strider on 26 Oct 2021
Important parts of the code appear to be missing.
However to get the maximum, negate the function —
f = @(x) sin(x);
x0 = rand;
[minimum,fvmin] = fminsearch(f, x0)
minimum = -1.5708
fvmin = -1.0000
[maximum,fvmax] = fminsearch(@(x)-f(x), x0)
maximum = 1.5708
fvmax = -1.0000
figure
plot((-pi:0.1:pi), sin((-pi:0.1:pi)))
hold on
plot(minimum,f(minimum),'vr', maximum,f(maximum),'^r')
hold off
legend('Function','Minimum','Maximum', 'Location','best')
ylim(ylim*1.1)
.
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!