MATLAB 求解非线性方程组时无法识别的函数或变量“x”

问题描述 投票:0回答:1

运行此代码时,我不断收到错误无法识别的函数或变量“x”。

function aby = system_of_eqs(x)
    % a=alpha, b=beta, y=gamma
    % Initialize aby as a vector of zeros
    aby = zeros(3, 1);

    % First equation
    aby(1) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) + 
    (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) + 
    (1/(4*x(2))) + (1/(16*x(3)))) + ...
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) + 
    (1/(6*x(2))) + (1/(36*x(3)))));

    % Second equation
    aby(2) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) + 
    (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) + 
    (1/(4*x(2))) + (1/(16*x(3)))) + ...
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) + 
    (1/(6*x(2))) + (1/(36*x(3)))));

    % Third equation
    aby(3) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) + 
    (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) + 
    (1/(4*x(2))) + (1/(16*x(3)))) + ...
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) + 
    (1/(6*x(2))) + (1/(36*x(3)))));

    % Use a non-zero initial guess
    x0 = [1, 4, 19]; % Non-zero initial guess

    % Solve the system of equations using fsolve
    solution = fsolve(@system_of_eqs, x0);

    % Display the solution
    display(solution)
end

matlab代码(2023b)

我什至在函数之前将 x 定义为数字,但仍然遇到相同的错误。这对我来说毫无意义。我该如何求解 x1, x2, x3 这就是重点?

matlab
1个回答
0
投票

您编写了一个崩溃的递归函数。

函数名称

system_of_eqs
fsove
内部使用,作为在
fsolve
内部运行
system_of_eqs
的函数。

要运行

system_of_eqs
,您必须输入我们读者看不到的
x
,因为您没有提供调用
system_of_eqs
的代码,但我们假设您已正确调用
system_of_eqs

x=[1 2 3]
system_of_eqs(x)

现在一旦进入

system_of_eqs
,到达
fsolve
时,正如您所定义的
system_of_eqs
,此函数需要定义输入字段
x

MATLAB 无法按照当前定义

fsolve
的方式继续求解
system_of_eqs

您必须使用其他名称或更好的名称来定义

fsolve
使用的函数;定义内部不含
fsolve
的函数。

1.-

定义

system_of_eqs
就像这样

function y=system_of_eqs(x)

    % First equation
    y(1) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) +   (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) +     (1/(4*x(2))) + (1/(16*x(3)))) + ...
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) +     (1/(6*x(2))) + (1/(36*x(3)))));

    % Second equation
    y(2) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) +     (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) +     (1/(4*x(2))) + (1/(16*x(3)))) + ...
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) +     (1/(6*x(2))) + (1/(36*x(3)))));

    % Third equation
    y(3) = ( ((1/x(1)) + (1/x(2)) + (1/x(3))) + ((1/x(1)) +     (1/(2*x(2))) + (1/(4*x(3)))) + ...
    ((1/x(1)) + (1/(3*x(2))) + (1/(9*x(3)))) + ((1/x(1)) +     (1/(4*x(2))) + (1/(16*x(3)))) + ... 
    ((1/x(1)) + (1/(5*x(2))) + (1/(25*x(3)))) + ((1/x(1)) +     (1/(6*x(2))) + (1/(36*x(3)))));

2.-

使用正确的语法使用

fsolve
解决问题

x0=[1 2 3]
f1=@system_of_eqs
fsolve(f1,x0)

=
   1.0e+02 *
   6.298874874335936   4.673058950322499   3.960434817680791

使用问题中定义的

x0 = [1 4 19]
获得相同的结果

3.-

fsolve
产生附加注释

No solution found.

**fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
value of the function tolerance.**

options = optimoptions('fsolve','Display','iter');
y2=fsolve(f1,x0,options)

                                         Norm of      First-order   Trust-region
 Iteration  Func-count     f(x)          step         optimality    radius
     0          4         134.308                           120               1
     1          8         40.7951              1           16.6               1
     2         12         21.4812              1           5.37               1
     3         16         13.9187              1           2.46               1
     4         20         9.98199              1           1.36               1
     5         24         7.59035              1          0.843               1
     6         28         6.00003              1          0.566               1
     7         32         4.87936              1          0.401               1
     8         36         4.05639              1          0.296               1
     9         40         2.74514            2.5          0.156             2.5
    10         44         1.99759            2.5         0.0936             2.5
    11         48         1.52864            2.5         0.0607             2.5
    12         52         1.21364            2.5         0.0418             2.5
    13         56        0.990841            2.5         0.0301             2.5
    14         60        0.650616           6.25         0.0152            6.25
    15         64        0.463832           6.25        0.00878            6.25
    16         68        0.348728           6.25        0.00557            6.25
    17         72        0.272192           6.25        0.00377            6.25
    18         76        0.218515           6.25        0.00268            6.25
    19         80        0.137793         15.625        0.00132            15.6
    20         84       0.0947877         15.625       0.000747            15.6
    21         88       0.0691766         15.625       0.000464            15.6
    22         92       0.0527003         15.625       0.000308            15.6
    23         96       0.0414793         15.625       0.000215            15.6
    24        100       0.0334948         15.625       0.000156            15.6
    25        104       0.0213184        39.0625       7.92e-05            39.1
    26        108       0.0147501        39.0625       4.55e-05            39.1
    27        112       0.0108084        39.0625       2.86e-05            39.1
    28        116      0.00825892        39.0625       1.91e-05            39.1
    29        120       0.0065156        39.0625       1.34e-05            39.1
    30        124       0.0052712        39.0625       9.73e-06            39.1
    31        128      0.00336616        97.6563       4.96e-06            97.7
    32        132       0.0023342        97.6562       2.87e-06            97.7
    33        136      0.00171313        97.6562        1.8e-06            97.7
    34        140      0.00131059        97.6562       1.21e-06            97.7
    35        144       0.0010349        97.6562       8.46e-07            97.7

我们换个方法吧

options = optimoptions('linprog','Algorithm','dual-simplex','Display','off');
y2=fsolve(f1,x0,options)


Warning: You have passed LINPROG options to FSOLVE. FSOLVE will use the
common options and ignore the LINPROG options that do not apply.
To avoid this warning, convert the LINPROG options using OPTIMOPTIONS. 
 In optim.options/SolverOptions/convertForSolver (line 776)
In prepareOptionsForSolver (line 49)
In fsolve (line 174) 

y2 =
   1.0e+03 *
   1.398666270065036   1.037653551559435   0.879415418789467

现在

fsolve
仍在解决但发出更多警告。

还有另一种不同的方法

options = optimoptions('fsolve','Algorithm','levenberg-marquardt');
[y3,fval,exitflag] = fsolve(f1,x0,options);

y3 =
   1.0e+03 *
   2.733060678987443   2.026813806848032   1.716724478563590

现在不再有警告或注释。

然而,我们发现改变

x0
会得到不同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.