matlab求解非线性方程组

标签:
教育 |
最近有人问我matlab求解方程组的问题,
下面提供了fsolve函数help,但要有几点需要注意
1.方程组的有解性
2.方程组解是否唯一
3.x0的选取,如果解不唯一,不同的x0可能会得到不同的解
fsolve - Solve
system of nonlinear equations
Equation
Solves a problem specified by
F(x) = 0
for
Syntax
x = fsolve(fun,x0)
x = fsolve(fun,x0,options)
x = fsolve(problem)
[x,fval] = fsolve(fun,x0)
[x,fval,exitflag] = fsolve(...)
[x,fval,exitflag,output] = fsolve(...)
[x,fval,exitflag,output,jacobian] = fsolve(...)
Description
fsolve
x = fsolve(fun,x0)
x =
fsolve(fun,x0,options)
x = fsolve(problem)
Create the structure
[x,fval] =
fsolve(fun,x0)
[x,fval,exitflag] =
fsolve(...)
[x,fval,exitflag,output] =
fsolve(...)
[x,fval,exitflag,output,jacobian] =
fsolve(...)
Passing Extra Parameters
Input Arguments
Function Arguments
The nonlinear system of equations to
solve. x = fsolve(@myfun,x0)
where function F = myfun(x) F = ... % Compute function values at x fun x = fsolve(@(x)sin(x.*x),x0); If the user-defined values
for If the Jacobian can also be
computed options = optimset('Jacobian','on') then the
function
If |
||
problem |
objective |
Objective function |
x0 |
Initial point for |
|
solver |
'fsolve' | |
options |
Options structure created
with |
Output Arguments
Function Arguments
This section provides function-specific details
for
exitflag |
Integer identifying the reason the algorithm terminated. The
following lists the values
ofexitflag |
|
|
1 |
Function converged to a
solution |
|
2 |
Change
in |
|
3 |
Change in the residual was smaller than the specified tolerance. |
|
4 |
Magnitude of search direction was smaller than the specified tolerance. |
|
0 |
Number of iterations
exceeded |
|
-1 |
Algorithm was terminated by the output function. |
|
-2 |
Algorithm appears to be converging to a point that is not a root. |
|
-3 |
Trust radius became too small. |
|
-4 |
Line search cannot sufficiently decrease the residual along the current search direction. |
output |
Structure containing information about the optimization. The fields of the structure are |
|
|
iterations |
Number of iterations taken |
|
funcCount |
Number of function evaluations |
|
algorithm |
Optimization algorithm used. |
|
cgiterations |
Total number of PCG iterations (large-scale algorithm only) |
|
stepsize |
Final displacement
in |
|
firstorderopt |
Measure of first-order optimality (dogleg or large-scale algorithm, [ ] for others) |
|
message |
Exit message |
Options
Optimization options used by
The
LargeScale |
Use large-scale algorithm if possible when set
to |
Medium-Scale and Large-Scale Algorithms
These options are used by both the medium-scale and large-scale algorithms:
DerivativeCheck |
Compare user-supplied derivatives (Jacobian) to finite-differencing derivatives. |
Diagnostics |
Display diagnostic information about the function to be solved. |
DiffMaxChange |
Maximum change in variables for finite differencing. |
DiffMinChange |
Minimum change in variables for finite differencing. |
Display |
Level of
display. |
FunValCheck |
Check whether objective function values are
valid. |
Jacobian |
If |
MaxFunEvals |
Maximum number of function evaluations allowed. |
MaxIter |
Maximum number of iterations allowed. |
OutputFcn |
Specify one or more user-defined functions that an optimization
function calls at each iteration.
See |
PlotFcns |
Plots various measures of progress while the algorithm executes,
select from predefined plots or write your own.
Specifying |
TolFun |
Termination tolerance on the function value. |
TolX |
Termination tolerance
on |
TypicalX |
Typical |
Large-Scale Algorithm Only
These options are used only by the large-scale algorithm:
JacobMult |
Function handle for Jacobian multiply function. For large-scale
structured problems, this function computes the Jacobian matrix
product W = jmfun(Jinfo,Y,flag,p1,p2,...)
where |
|
[F,Jinfo] = fun(x) Y
|
|
See |
JacobPattern |
Sparsity pattern of the Jacobian for finite differencing. If it
is not convenient to compute the Jacobian
matrix |
MaxPCGIter |
Maximum number of PCG (preconditioned conjugate gradient)
iterations (see |
PrecondBandWidth |
The
default |
TolPCG |
Termination tolerance on the PCG iteration. |
Medium-Scale Algorithm Only
These options are used only by the medium-scale algorithm:
NonlEqnAlgorithm |
Specify one of the following algorithms for solving nonlinear equations:
|
LineSearchType |
Line search algorithm choice. This option applies to
the |
Examples
Example 1
This example finds a zero of the system of two equations and two unknowns:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/eqn1199380333.gif
You want to solve the following system for
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/eqn1199380455.gif
starting at
First, write an M-file that computes
function F = myfun(x) F = [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))];
Next, call an optimization routine.
x0 = [-5; -5]; % Make a starting guess at the solution options=optimset('Display','iter'); % Option to display output [x,fval] = fsolve(@myfun,x0,options) % Call optimizer
After 33 function evaluations, a zero is found.
Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 3 23535.6 2.29e+004 1 1 6 6001.72 1 5.75e+003 1 2 9 1573.51 1 1.47e+003 1 3 12 427.226 1 388 1 4 15 119.763 1 107 1 5 18 33.5206 1 30.8 1 6 21 8.35208 1 9.05 1 7 24 1.21394 1 2.26 1 8 27 0.016329 0.759511 0.206 2.5 9 30 3.51575e-006 0.111927 0.00294 2.5 10 33 1.64763e-013 0.00169132 6.36e-007 2.5 Optimization terminated successfully: First-order optimality is less than options.TolFun x = 0.5671 0.5671 fval = 1.0e-006 * -0.4059 -0.4059
Example 2
Find a matrix x that satisfies the equation
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/eqn1199379189.gif
starting at the point
First, write an M-file that computes the equations to be solved.
function F = myfun(x) F = x*x*x-[1,2;3,4];
Next, invoke an optimization routine.
x0 = ones(2,2); % Make a starting guess at the solution options = optimset('Display','off'); % Turn off Display [x,Fval,exitflag] = fsolve(@myfun,x0,options)
The solution is
x = -0.1291 0.8602 1.2903 1.1612 Fval = 1.0e-009 * -0.1619 0.0776 0.1161 -0.0469 exitflag = 1
and the residual is close to zero.
sum(sum(Fval.*Fval)) ans = 4.7915e-020
Notes
If the system of equations is linear, use/
3x1
x1
x1
You can formulate and solve the problem as
A = [ 3 11 -2; 1 1 -2; 1 -1 1]; b = [ 7; 4; 19]; x = A/b x = 13.2188 -2.3438 3.4375
Algorithm
The Gauss-Newton, Levenberg-Marquardt, and large-scale methods
are based on the nonlinear least-squares algorithms also used
in
Large-Scale Optimization
fsolve, with the
Medium-Scale Optimization
By default
Alternatively, you can select a Gauss-Newton
method
The default line search algorithm for the Levenberg-Marquardt
and Gauss-Newton methods, i.e.,
theLineSearchType
Diagnostics
Medium and Large-Scale Optimization
fsolve
Optimizer is stuck at a minimum that is not a root Try again with a new starting guess
In this case, run
Medium-Scale Optimization
For the trust-region dogleg method,
The optimization algorithm can make no further progress: Trust region radius less than 10*eps
In this case, run
Limitations
The function to be solved must be continuous. When
successful,
fsolve
Large-Scale Optimization
The preconditioner computation used in the preconditioned
conjugate gradient part of the large-scale method forms
Medium-Scale Optimization
The default trust-region dogleg method can only be used when the system of equations is square, i.e., the number of equations equals the number of unknowns. For the Levenberg-Marquardt and Gauss-Newton methods, the system of equations need not be square.
References
[1] Coleman, T.F. and Y. Li, "An Interior, Trust Region Approach
for Nonlinear Minimization Subject to Bounds,"
[2] Coleman, T.F. and Y. Li, "On the Convergence of Reflective
Newton Methods for Large-Scale Nonlinear Minimization Subject to
Bounds,"
[3] Dennis, J. E. Jr., "Nonlinear Least-Squares,"
[4] Levenberg, K., "A Method for the Solution of Certain
Problems in Least-Squares,"
[5] Marquardt, D., "An Algorithm for Least-squares Estimation of
Nonlinear Parameters,"
[6] Moré, J. J., "The Levenberg-Marquardt Algorithm:
Implementation and Theory,"
[7] Moré, J. J., B. S. Garbow, and K. E. Hillstrom,
[8] Powell, M. J. D., "A Fortran Subroutine for Solving Systems
of Nonlinear Algebraic Equations,"
http://blog.csdn.net/aris_zzy/article/details/2196865
MATlab求解方程方法
1、解方程
最近有多人问如何用matlab解方程组的问题,其实在matlab中解方程组还是很方便的,例如,对于代数方程组Ax=b(A为系数矩阵,非奇异)的求解,MATLAB中有两种方法:
(1)x=inv(A)*b
(2)x=A
例:
x1+2x2=8
2x1+3x2=13
>>A=[1,2;2,3];b=[8;13];
>>x=inv(A)*b
x
>>x=A
x
即二元一次方程组的解x1和x2分别是2和3。
对于同学问到的用matlab解多次的方程组,有符号解法,方法是:先解出符号解,然后用vpa(F,n)求出n位有效数字的数值解.具体步骤如下:
第一步:定义变量syms
第二步:求解[x,y,z,...]=solve('eqn1','eqn2',...,'eqnN','var1','var2',...'varN');
第三步:求出n位有效数字的数值解x=vpa(x,n);y=vpa(y,n);z=vpa(z,n);...。
如:解二(多)元二(高)次方程组:
x^2+3*y+1=0
y^2+4*x+1=0
解法如下:
>>syms
>>[x,y]=solve('x^2+3*y+1=0','y^2+4*x+1=0');
>>x=vpa(x,4);
>>y=vpa(y,4);
结果是:
x
y
二元二次方程组,共4个实数根;
还有的同学问,如何用matlab解高次方程组(非符号方程组)?举个例子好吗?
解答如下:
基本方法是:solve(s1,s2,…,sn,v1,v2,…,vn),即求表达式s1,s2,…,sn组成的方程组,求解变量分别v1,v2,…,vn。
具体例子如下:
x^2
x^2
解法:
>>
运行结果为
x
y
即x等于1和3;y等于1和-1.5
或
>>[x,y]
结果一样,二元二方程都是4个实根。
通过这三个例子可以看出,用matlab解各类方程组都是可以的,方法也有多种,只是用到解方程组的函数,注意正确书写参数就可以了,非常方便。
cite from:http://bbs.seu.edu.cn/pc/pccon.php?id=950&nid=14498&tid=0
2、变参数非线性方程组的求解
对于求解非线性方程组一般用fsolve命令就可以了,但是对于方程组中某一系数是变化的,该怎么求呢?
%定义方程组如下,其中k为变量
function F = myfun(x,k)
H=0.32;
Pc0=0.23;W=0.18;
F=[Pc0+H*(1+1.5*(x(1)/W-1)-0.5*(x(1)/W-1)^3)-x(2);
%求解过程
H=0.32;
Pc0=0.23;W=0.18;
x0 = [2*W; Pc0+2*H];
options = optimset('Display','off');
k=0:0.01:1;
for i=1:1:length(k)
kk=k(i);
x = fsolve(@(x) myfun(x,kk), x0, options);%求解非线性方程组
x1(i)=x(1);
x2(i)=x(2);
end
plot(k,x1,'-b',k,x2,'-r');
xlabel('k')
legend('x1','x2')
cite from:http://forum.simwe.com/archiver/tid-836299.html
3、非线性方程数值求解
matlab里solve如何使用,是否有别的函数可以代替它.
matlab里我解y=9/17*exp(-1/2*t)*17^(1/2)*sin(1/2*17^(1/2)*t)=0这样的方程为什么只得到0这一个解,如何可以的到1/2*17^(1/2)*t=n*(pi)这样一族解??
在matlab里面solve命令主要是用来求解代数方程(即多项式)的解,但是也不是说其它方程一个也不能解,不过求解非代数方程的能力相当有限,通常只能给出很特殊的实数解。(该问题给出的方程就是典型的超越方程,非代数方程)
从计算机的编程实现角度讲,如今的任何算法都无法准确的给出任意非代数方程的所有解,但是我们有很多成熟的算法来实现求解在某点附近的解。matlab也不例外,它也只能给出任意非代数方程在某点附近的解,函数有两个:fzero和fsolve,具体用法请用help或doc命令查询吧。如果还是不行,你还可以将问题转化为非线性最优化问题,求解非线性最优化问题的最优解,可以用的命令有:fminbnd, fminsearch, fmincon等等。
*非线性方程数值求解
*单变量非线性方程求解
其中fname是待求根的函数文件名,x0为搜索的起点。一个函数可能有多个根,但fzero函数只给出离x0最近的那个根。tol控制结果的相对精度,缺省时取tol=eps,trace指定迭代信息是否在运算中显示,为1时显示,为0时不显示,缺省时取trace=0。
(1) 建立函数文件funx.m。
**非线性方程组的求解
其中X为返回的解,fun是用于定义需求解的非线性方程组的函数文件名,X0是求根过程的初值,option为最优化工具箱的选项设定。最优化工具箱提供了20多个选项,用户可以使用optimset命令将它们显示出来。如果想改变其中某个选项,则可以调用optimset()函数来完成。例如,Display选项决定函数调用时中间结果的显示方式,其中‘off’为不显示,‘iter’表示每步都显示,‘final’只显示最终结果。 optimset(‘Display’,‘off’)将设定Display选项为‘off’。
function q=myfun(p)
x=p(1);
y=p(2);
q(1)=x-0.6*sin(x)-0.3*cos(y);
q(2)=y-0.6*cos(x)+0.3*sin(y);
x=fsolve('myfun',[0.5,0.5]',optimset('Display','off'))
x =
将求得的解代回原方程,可以检验结果是否正确,命令如下:
q=myfun(x)
q =
cite from:http://blog.sina.com.cn/s/blog_56ef652d0100ebew.html
4、fsolve函数解方程
[X,FVAL,EXITFLAG,OUTPUT,JACOB]=FSOLVE(FUN,X0,...) returns the
cite from:
http://blog.163.com/realking123@126/blog/static/50526420201101745630904/