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?

 Akzeptierte Antwort

Star Strider
Star Strider am 26 Okt. 2021

0 Stimmen

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 Kommentare

kyungdoo lee
kyungdoo lee am 26 Okt. 2021
Is there any way to get the maximum value by modifying the code I posted?
Star Strider
Star Strider am 26 Okt. 2021
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 am 26 Okt. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by