Minimizing a tri-variate function

8 visualizaciones (últimos 30 días)
terance
terance el 25 de En. de 2012
Comentada: nick el 16 de Abr. de 2025
Hello!
I have the following task - I need to find the minimum of the following function (using only fminsearch) F(X,Y,Z)=(X-a*Y)^2+(X-a*Z)^2 , by parametre a
The problem is that X, Y, Z are stored in arrays (one dimentional per each variable).
I've written the following code
function F= F_linear(M,F,SP,a) F = (M-a*F)^2 + (M-a*SP)^2; end
But, when I try to use fminsearch (like this:x = fminsearch(@(x) myfun(X,Y,Z,a))) the following error-message occures:
Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.
Error in ==> F_linear at 2 F = (X-a*Y)^2 + (X-a*Z)^2;
Error in ==> @(x)F_linear(X,Y,Z,a)
Error in ==> fminsearch at 191 fv(:,1) = funfcn(x,varargin{:});
How can I fix this problem? Thank you for your reply!

Respuestas (1)

nick
nick el 16 de Abr. de 2025
Editada: nick el 16 de Abr. de 2025
Hello terance,
To resolve this error, kindly use element wise operators in the function F_linear, as shown below:
function F = F_linear(M, F, SP, a)
F = sum((M - a .* F).^2 + (M - a .* SP).^2);
end
You may refer to the documentation by executing the following command in MATLAB Command Window to know more about element-wise operations:
doc power
  2 comentarios
Sam Chak
Sam Chak el 16 de Abr. de 2025
Looking at the tri-variate function, the minimum value must occur when .
Since there are two quadratic components, one must solve the equations and .
It is evident that the solutions represent two straight lines. However, I am currently unsure how to implement this in MATLAB and display the results in a 3D projection.
nick
nick el 16 de Abr. de 2025
The two equations and represent planes in 3D space and for the minimum value of f(x,y,z), these equations gives the following result :
These set of coordinates represents a straight line in the 3D space when and the y-z plane when . The code below shows the intersection region between these 2 equations :
Y = linspace(-10, 10, 100);
Z = linspace(-10, 10, 100);
[Y_grid, Z_grid] = meshgrid(Y, Z);
% parameter a
a_values = [-2, -1, 0, 1, 2];
figure;
for i = 1:length(a_values)
a = a_values(i);
% Calculate X for each line
X_Y = a * Y_grid;
X_Z = a * Z_grid;
subplot(2, 3, i);
hold on;
% Plot the line X = aY
surf(X_Y, Y_grid, Z_grid, 'FaceAlpha', 0.5, 'EdgeColor', 'none', 'FaceColor', 'r');
% Plot the line X = aZ
surf(X_Z, Y_grid, Z_grid, 'FaceAlpha', 0.5, 'EdgeColor', 'none', 'FaceColor', 'b');
xlabel('X');
ylabel('Y');
zlabel('Z');
title(sprintf('a = %.1f', a));
grid on;
view(3);
hold off;
end
sgtitle('3D Projection of Lines X = aY and X = aZ for Different a Values');
figure;
for i = 1:length(a_values)
a = a_values(i);
subplot(2, 3, i);
hold on;
if a == 0
% Special case for a = 0, the solution is the plane X = 0
X_plane = zeros(size(Y_grid));
surf(X_plane, Y_grid, Z_grid, 'FaceAlpha', 0.5, 'EdgeColor', 'none', 'FaceColor', 'g');
title('Solution Plane for a = 0');
else
X_line = a * Y;
Z_line = Y;
plot3(X_line, Y, Z_line, 'k', 'LineWidth', 2);
title(sprintf('a = %.1f', a));
end
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on;
view(3);
hold off;
end
sgtitle('3D Solution Line for X = aY = aZ with Y = Z for Different a Values');

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by