1

我在 matlab 中看到了几个 fsolve 的例子,但似乎找不到任何显示如何以矩阵形式传递参数的例子。

这是我的代码。

[A,b] = equationsToMatrix(eq1,eq2)

X0 = [0 0]

fsolve([A,b], X0)

这是输出

eq1 = - sx - sy/2 == 5 

eq2 = - (3*sx)/2 - (3*sy)/2 == 9 

A =

[   -1, -1/2]
[ -3/2, -3/2]


b =

 5
 9


X0 =

 0     0

Error using lsqfcnchk (line 109)
If FUN is a MATLAB object, it must have an feval method.

Error in fsolve (line 198)
    funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);

Error in SolveTesting (line 70)
fsolve([A,b], X0)

正如你所看到的,我已经得到了一个很好的求解格式的方程组,为什么 matlab 不接受这个?我也不明白 x0 论点的意义。我提供了一个方程组,那我为什么需要一个起点呢?

4

1 回答 1

0

弄清楚了。 这是交易。传递给fsolve的对象需要是指向函数的指针。此函数需要评估系统中的每个方程并返回一个矩阵,其中包含每个方程的数值结果。如果所有方程都返回零,则系统被求解。

传递给 fsolve 的函数既可以在单独的脚本中定义,也可以在简单的情况下在线创建。

例如定义:

function f = matrixfun(z,A,b)
    f = double(A) * [z(1);z(2)] + double(b);
end

然后调用:

>> fsolve(@matrixfun,guess,[],A,b)

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.

<stopping criteria details>


ans =

4.0000   2.0000

或者你可以这样做:

>> fsolve(@(z)double(A)*[z(1);z(2)]+double(b),[-5 -5])

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.

<stopping criteria details>


ans =

 4.0000   2.0000

** 我通过 double() 传递 A 和 b,因为起初我收到一个错误,即 fsolve 想要所有值都为双精度值。

于 2013-10-18T22:36:04.483 回答