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)
I have interpolated the attached table with the above code. Now I want to find the max using PSO, but I don't know how.

 Accepted Answer

Star Strider
Star Strider on 26 Oct 2021

0 votes

This is at least the second time (the first that I°m aware of is how to get maximum value of this code) you’ve asked the same question and still haven’t supplied the necessary information!
And still more continue to appear!

9 Comments

I made a big mistake because I didn't have time. I really enjoyed reading your previous reply. I'm really sorry.
Finding the maximum in the region-of-interest is straightforward —
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/779268/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');
Warning: Columns of data containing NaN values have been ignored during interpolation.
[r,c] = find(Zq == max(Zq(:)));
Check = [xq(c),yq(r),Zq(r,c), max(Zq(:))]
Check = 1×4
2.0000 4.3000 0.0079 0.0079
figure
AA = surf(xq,yq,Zq);
hold on
stem3(xq(c),yq(r),Zq(r,c), '^r', 'MarkerFaceColor','r')
hold off
text(xq(c),yq(r),Zq(r,c), sprintf('\\leftarrow (%.2f, %.2f, %.2f)',xq(c),yq(r),Zq(r,c)), 'Horiz','left', 'Vert','middle', 'FontSize',10, 'Color','r', 'Rotation',90)
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)
ans = 0.0070
Zq(23,23)
ans = 0.0073
Zq(81,81)
ans = 0.0058
Finding whatever the global maximum may be (that may be outside of the region-of-interest) requires knowing the function that created the ‘Zq’ data, so that it can be appropriately parameterised and then optimised.
.
kyungdoo lee
kyungdoo lee on 26 Oct 2021
Edited: kyungdoo lee on 26 Oct 2021
First of all, I'm sorry again
Is the above code finding the maximum value using PSO?
My ultimate goal is to use the PSO to find the magnet and coil lengths that give the maximum values ​​from the attached table. I used interpolation to make it more accurate.
No worries.
Is the above code finding the maximum value using PSO?
No. It is finding the max using the max function because that is the most efficient method here.
It is not possible to use pso or any other optimisation function because the function that created the data in the file (that would be used as the objective function in the optimisation) is not provided.
There is simply no need to use any specific optimisation function here, since the data are provided instead, and the limits to the variables are fixed.
My ultimate goal is to use the PSO to find the magnet and coil lengths that give the maximum values ​​from the attached table. I used interpolation to make it more accurate.
If the function that created the data is available, optimising it for the maximum or minimum would be possible. Since the function is not provided, using an optimisation function on the data is not possible.
.
You mean i can't use PSO if i don't know the function?
If so, how can I create a function in that graph?
You mean i can't use PSO if i don't know the function?
Yes. (That also applies to every other optimsiation function.)
If so, how can I create a function in that graph?
I honestly do not know, because I have no idea what it is or what created it. If those are data, then it would likely (although not certainly) be possible to fit a function to it to estimate a subset of the parameters of the function, and then optimise it with respect to other parameters.
If the plotted surface are data, and fitting a function to it is the objective, then the function would need to be provided, and also clearly described with respect to the variables, other arguments (if necessary), and parameters. Then any appropriate oprimisation function could be used to estimate the parameters and fit the function to the data.
A call to the ‘Harmony’ function appeared in at least one post earlier, however I have no idea what it is or what it does because the code for it was never posted. It may (or may not) be the objective function required for the optimisation.
I have no way of knowing anything other than what was posted.
.
Thank you for the reply.
In the graph above, the Z-axis number is too small to display well. How can I get a more accurate z-value?
As always, my pleasure!
The ‘Z’ value is as accurate as it needs to be. To increase the numbers displayed in the text object, increase the precision in the format specifying it —
text(xq(c),yq(r),Zq(r,c), sprintf('\\leftarrow (%.2f, %.2f, %.8f)',xq(c),yq(r),Zq(r,c)), 'Horiz','left', 'Vert','middle', 'FontSize',10, 'Color','r', 'Rotation',90)
To display it in its full precision, either use —
format long
or —
fprintf('Zq_max = %23.15E\n',Zq(r,c))
to print it in full precision. (The format functions apparently do not work with the online Run feature here, however they will work on your computer.)
.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!