首页 > 解决方案 > MATLAB 的最小化问题没有给出预期的结果

问题描述

我试图找到最小化函数的系数。根据我所做的一些猜测,我创建了一些具有相同功能的假数据。我想根据假数据找到系数来验证我的代码,但我得到了意想不到的结果。两条线都应该在彼此之上,因为这绝对是函数可以做的事情。

我现在得到的结果

我不确定我的代码哪里出错了,如果你能提供任何帮助,我将不胜感激。代码如下,我还上传了重新创建结果所需的所有文件。

%% Location of point forces
xx = [1.5, 1.5, -0.5,0.4, 0.4]; % X Location of stokeslets
yy = [-3, 3, 0,1.25, -1.25]; % Y Location of stokeslets
pfl = [0, 0]; %Location of the potential flow [x y]
%% Import fake data
load('fakemodel.mat')
%% Optimization
simply_velo = @(sf_arg) velopt(posx,posy,sf_arg,xx,yy,sf_arg(:,6),pfl); %calculates function to minimize at poins where I have data
%function to minimize
fun = @(sf_arg) nan_norm(((speed_fake - simply_velo(sf_arg)).^2)); %relative error
%% Create the non linear constraint
sf_ini = [1.5,1.5,3,-3,-3,6]; %initial guess for variables
A = [0 0 0 1 0 0; 0 0 0 0 1 0;  -1 -1 0 0 0 0; 0 0 0 0 0 -1; 0 0 -1 0 0 0]; %A*x < b
b = [0; 0; 0; 0;0]; % b = ()
Aeq = [1 -1 0 0 0 0; 0 2 -1 0 0 0; 0 0 0 -1 1 0; 1 1 1 1 1 0]; %Aeq *x = beq
beq = [0; 0; 0 ;0]; %beq
error = zeros(50,6);
sav_fm = zeros(50,6);
for i = 1 : 50
    sf_ini = rand(size(sf_ini));
%     options = optimset('Display', 'iter', 'MaxFunEvals', 600,'PlotFcns',@optimplotfval,'TolFun',1e-4,'TolX',1e-4);
    options = optimset('MaxFunEvals', 6000,'TolFun',1e-15,'TolX',1e-15);
    [fm, fval] = fmincon(fun,sf_ini,A,b, Aeq, beq,[],[],[],options); %f min search
    sav_fm(i,:) = fm(:);
    Fval(i) = fval;
end
% saved_fm
[minfval,index] = min(Fval);
soln = sav_fm(index,:)

我不确定我的问题出在哪里。欢迎任何建议。

重新创建问题所需的文件

标签: matlab

解决方案


推荐阅读