how to get maximum value of this code

조회 수: 1 (최근 30일)
kyungdoo lee
kyungdoo lee 2021년 10월 26일
댓글: Star Strider 2021년 10월 26일
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?

채택된 답변

Star Strider
Star Strider 2021년 10월 26일
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
kyungdoo lee
kyungdoo lee 2021년 10월 26일
Thank you for answer
Star Strider
Star Strider 2021년 10월 26일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by