【问题标题】:Solving an overdetermined set of equations with lsqcurvefit in MATLAB在 MATLAB 中使用 lsqcurvefit 求解一组超定方程
【发布时间】:2016-02-14 22:00:38
【问题描述】:

我有一个类似这个问题的问题: How to solve an overdetermined set of equations using non-linear lest squares in Matlab

我有 9 个方程,其中有 2 个未知数 x 和 y,如下:

A11=(sin(x))*(cos(y))*(sin(x))*(cos(y));
A12=(sin(x))*(cos(y))*(sin(x))*(sin(y));
A13=(sin(x))*(cos(y))*(cos(x));
A21=(sin(x))*(sin(y))*(sin(x))*(cos(y));
A22=(sin(x))*(sin(y))*(sin(x))*(sin(y));
A23=(sin(x))*(sin(y))*(cos(x));
A31=(sin(x))*(cos(y))*(cos(x));
A32=(sin(x))*(sin(y))*(cos(x));
A33=(cos(x))*(cos(x));

我知道每个 A_ij 的值并想计算 x 和 y。 我试图通过像这样使用 lsqcurvefit 来实现这一点:

ydata=[0, 0, 0, 0, 1, 0, 0, 0, 0]; % this is one set of A_ij
lb=[0,0];
ub=[pi,2*pi];
x0=[pi/2;pi];
p=zeros(2,1);
p = lsqcurvefit( myfun,x0,xdata,ydata,lb,ub)

我没有 xdata 的任何值,那么有什么方法可以让它像这样运行吗? 我将函数 myfun 定义为:

function r = myfun(p)

x=p(1);
y=p(2);

The 9 equations;

r=[A11, A12, A13, A21, A22, A23, A31, A32, A33];
end

现在,每当我运行 lsqcurvefit 时,都会收到错误消息“输入参数不足”。并且错误发生在 x=p(1); 的行中 我不知道缺少什么或更好,我不知道如何处理我没有 xdata 输入的事实。

我希望有人可以帮助我完成这项工作。 非常感谢您。

法比安

【问题讨论】:

    标签: matlab nonlinear-optimization


    【解决方案1】:

    经过一些修改后,我让它与 lsqcurvefit 一起工作。这基本上是我如何将 xdata 和 ydata 寻址到函数句柄的问题。 这就是我的代码最终的样子:

    ydata = [0.12; 0.28; 0.16; 0.66; 0.38; 0.22];
    xdata = [0; 0; 0; 0; 0; 0]; % just as a dummy, lsqcurvefit wants the input
    x0=[0,0];
    lb=[0,0];
    ub=[180,180];
    [x,resnorm,residual,exitflag,output] = lsqcurvefit(@fun,x0,xdata,ydata,lb,ub);
    % only x is needed, but I used the other information for other parts of my code
    % as a separate function.m file
    function F = fun(x,xdata) % again, xdata is just a dummy and not used
    
    p1=cosd(x(1));
    p2=sind(x(1))*sind(x(2));
    p3=sind(x(1))*cosd(x(2));
    % I didn't need all 9 entries, since the 3x3 matrix is symmetric
    F =[p1*p1;...
        p1*p2;...
        p1*p3;...
        p2*p2;...
        p2*p3;...
        p3*p3];
    
    end
    

    我希望有人能从这些信息中得到利用。

    【讨论】:

      猜你喜欢
      • 2018-05-01
      • 2012-11-09
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多