我在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参数的要点。我正在提供一个方程组,为什么我需要一个起点?
答案 0 :(得分: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
**我通过A&amp; b通过double(),因为起初我得到一个错误,fsolve希望所有值都是双倍的。