How to deal with 2D surface fitting problem with particle swarm optimization
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
x = linspace(-10, 10, 200);
y = linspace(-10, 10, 200);
[X, Y] = meshgrid(x, y);
dataset = testfun(X,Y,A)
testfun = @(X, Y, A) A(1,1)*X + A(1,2)*Y - A(2,1)*(X-0.3).^2 + A(2,2)*(Y+3)*X.^2;
objfun = @(testfun, dataset) norm(testfun - dataset).^2;
Hi.
I'm dealing with 2D surface fitting problem with pso algorithm.
(I know there are also other approaches in matlab such as lsqcurvefit, fmincon.. but I'm trying to solve with pso algorithm)
x and y are 2D meshgrid domain and A is coefficient matrix for testfun.
I made a dataset = testfun(X, Y, A2) with matrix A2 = [-0.2 0.9; -0.4 -0.03]
What should I do to find Afit with 'particleswarm' function that satisfy minimized objfun?
1 commentaire
Matt J
le 29 Mar 2022
Modifié(e) : Matt J
le 29 Mar 2022
I know there are also other approaches in matlab such as lsqcurvefit, fmincon..
Never mind fmincon, your problem is a linear least squares minimization. It has a closed form solution and doesn't require iterative optimization at all.
but I'm trying to solve with pso algorithm
Even if so, it is not clear what answer could satisfy you. The best PSO strategy would be to include the analytical solution in the InitialSwarmMatrix, in which case PSO will converge in zero steps.
Réponses (1)
Balavignesh
le 15 Mar 2024
Modifié(e) : Balavignesh
le 15 Mar 2024
Hi Yunhyeok,
As per my understanding, you would like to solve 2D surface fitting problem using the Particle Swarm Optimization (PSO) algorithm with 'particleswarm' function.
The first step is to ensure the test function is correctly defined. You mentioned that A is a coefficient matrix for testfun. This function will be used both to generate your dataset and as part of the objective function during optimization.
testfun = @(X, Y, A) A(1,1)*X + A(1,2)*Y - A(2,1)*(X-0.3).^2 + A(2,2)*(Y+3).*X.^2;
Next steps would be to generate the dataset and defining your objective function. Assuming you have already generated a dataset, this dataset will be used to fit your model. However, the objective function should calculate the difference between the output of 'testfun' (using a candidate matrix A) and your dataset, then return the squared norm of this difference.
objfun = @(A) norm(testfun(X, Y, reshape(A, [2,2])) - dataset, 'fro').^2;
The 'reshape(A, [2,2])' part is crucial because 'particleswarm' searches over a vector, not a matrix. We will reshape the vector A back into a 2x2 matrix inside the objective function.
Finally, set up and run the 'particleswarm' function. You'll need to specify the number of variables (which is 4, since your matrix A has four elements), and optionally, you can set bounds for these variables if you know them.
nvars = 4; % Because A is a 2x2 matrix, hence 4 elements
lb = []; % Lower bounds, empty if unknown
ub = []; % Upper bounds, empty if unknown
% Running particleswarm
opts = optimoptions('particleswarm', 'Display', 'iter');
[Afit, fval] = particleswarm(objfun, nvars, lb, ub,opts);
% Reshape Afit back into a matrix
Afit = reshape(Afit, [2,2]);
Aftyer 'particleswarm' finishes running, 'Afit' will contain the coefficients of your test function that best fit the dataset according to the 'PSO' algorithm. 'fval' will give you the value of the objective function at 'Afit', i.e., how well 'Afit' fits the dataset.
Kindly have a look at the following documentation links to have more information on:
- 'particleswarm' function: https://www.mathworks.com/help/gads/particleswarm.html
- 'norm' function: https://www.mathworks.com/help/matlab/ref/norm.html
Hope this helps!
Balavignesh
0 commentaires
Voir également
Catégories
En savoir plus sur Particle Swarm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!