how to get maximum value of this code

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Star Strider
Star Strider il 26 Ott 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 Commenti
Star Strider
Star Strider il 26 Ott 2021
As always, my pleasure!
.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by