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일

0 개 추천

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일
Is there any way to get the maximum value by modifying the code I posted?
Star Strider
Star Strider 2021년 10월 26일
Probably not because I have no idea what the objective is, what function is being used for the optimisation, and not unless the ‘Harmony’ function (and everything else necessary to run the code) appears!
Thank you for answer
Star Strider
Star Strider 2021년 10월 26일
As always, my pleasure!
.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

제품

질문:

2021년 10월 26일

댓글:

2021년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by