matlab中fsolve的用法 用fsolve函数出错,求帮忙解决

有关MATLAB用solve函数求解非线性方程组的问题 试过fsolve函数,发现对初始值太敏感了,所以想试试solve函数,毕竟不用初始值,但我写了下面的程序,发现会报错:
学而思问答
有关MATLAB用solve函数求解非线性方程组的问题试过fsolve函数,发现对初始值太敏感了,所以想试试solve函数,毕竟不用初始值,但我写了下面的程序,发现会报错:Error:Missing variable or function.我绝对是个菜鸟级,找不到错误啊!..clcclear allsyms k l c w rty=0.17;thet=0.8;afa=0.78;a=2.18;tc=0.15;tw=0.2;tr=0.33;deta=0.1;beta=0.935;eq1=r-(1-ty*thet)*afa*a*(k^(afa-1))*(l^(1-afa));eq2=w-(1-ty*thet)*(1-afa)*a*(k^afa)*l^(-afa);eq3=afa*(1-l)*(1-tw)-(1-afa)*c*(1+tc);eq4=beta*((1-tr)*r+1-deta)-1;eq5=k-(1-tw)*w*l-(1-tr)*r*k+(1+tc)*c-(1-deta)*k;[k,l,c,w,r]=solve(eq1,eq2,eq3,eq4,eq5)
按照你的思路solve在使用时,因为你的表达式里面有符号变量,所以要在最后解方程时注明需要求解的函数变量名也就是 最后一句[k,l,c,w,r]=solve(eq1,eq2,eq3,eq4,eq5,'k','l','c','w','r')但是solve基本是解解析解的,但是不是每个方程都有解析解,就像你的这道,解得时候会出现BUSY,说明可能不存在解析解
已知函数f(x)=3x2-6x-5.(1)求不等式f(x)>4的解集;(2)设g(x)=f(x)-2x2+mx,其中m∈R,求g(x)在区间[l,3]上的最小值;(3)若对于任意的a∈[1,2],关于x的不等式f(x)≤x2-(2a+6)x+a+b在区间[1,3]上恒成立,求实数b的取值范围.
求函数y=x-√(3x-2)的值域求函数x-根号下(3x-2)的值域,
log以18为底9的对数为a,18的b次方为5,求log以36为底45的对数?百度找的是错的,复制过来我不要哦
已知函数f(x)=(log2x)^2-2log0.5x+1,g(x)=x^2-ax+1若存在a∈R,对任意x1∈[1/8,2],总存在唯一x0∈[-1,2],使得f(x1)=g(x0)成立.求实数a的取值范围fsolve使用
我的图书馆
fsolve使用
解非线性方程组的方法有很多,比如直接降维、搜索(用最小二乘、牛顿迭代及最优化法)、连续法等等.&直接降维操作较难,求解时间长;牛顿迭代有局部收敛性;最优化必须给出真实解的初始值;连续发需要构造同伦方程。&看看fsolve的源代码:&& type fsolvefunction [x,FVAL,EXITFLAG,OUTPUT,JACOB] = fsolve(FUN,x,options,varargin)%FSOLVE solves systems of nonlinear equations of several variables.%% & FSOLVE attempts to solve equations of the form:% & & & & & &&% & F(X)=0 & &where F and X may be vectors or matrices. &&%% & X=FSOLVE(FUN,X0) starts at the matrix X0 and tries to solve the&% & equations in FUN. &FUN accepts input X and returns a vector (matrix) of&% & equation values F evaluated at X.&%% & X=FSOLVE(FUN,X0,OPTIONS) solves the equations with the default optimization% & parameters replaced by values in the structure OPTIONS, an argument% & created with the OPTIMSET function. &See OPTIMSET for details. &Used% & options are Display, TolX, TolFun, DerivativeCheck, Diagnostics,% & FunValCheck, Jacobian, JacobMult, JacobPattern, LineSearchType,% & NonlEqnAlgorithm, MaxFunEvals, MaxIter, PlotFcns, OutputFcn,% & DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter,% & PrecondBandWidth, TolPCG, and TypicalX. Use the Jacobian option to% & specify that FUN also returns a second output argument J that is the% & Jacobian matrix at the point X. If FUN returns a vector F of m% & components when X has length n, then J is an m-by-n matrix where J(i,j)% & is the partial derivative of F(i) with respect to x(j). (Note that the% & Jacobian J is the transpose of the gradient of F.)%% & X = FSOLVE(PROBLEM) solves system defined in PROBLEM. PROBLEM is a% & structure with the function FUN in PROBLEM.objective, the start point% & in PROBLEM.x0, the options structure in PROBLEM.options, and solver% & name 'fsolve' in PROBLEM.solver. &Use this syntax to solve at the&% & command line a problem exported from OPTIMTOOL. The structure PROBLEM&% & must have all the fields.%% & [X,FVAL]=FSOLVE(FUN,X0,...) returns the value of the equations FUN at X.&%% & [X,FVAL,EXITFLAG]=FSOLVE(FUN,X0,...) returns an EXITFLAG that describes the% & exit condition of FSOLVE. Possible values of EXITFLAG and the corresponding&% & exit conditions are%% & & 1 &FSOLVE converged to a solution X.% & & 2 &Change in X smaller than the specified tolerance.% & & 3 &Change in the residual smaller than the specified tolerance.% & & 4 &Magnitude of search direction smaller than the specified tolerance.% & & 0 &Maximum number of function evaluations or iterations reached.% & &-1 &Algorithm terminated by the output function.% & &-2 &Algorithm seems to be converging to a point that is not a root.% & &-3 &Trust region radius became too small.% & &-4 &Line search cannot sufficiently decrease the residual along the current% & & & & search direction.%% & [X,FVAL,EXITFLAG,OUTPUT]=FSOLVE(FUN,X0,...) returns a structure OUTPUT% & with the number of iterations taken in OUTPUT.iterations, the number of% & function evaluations in OUTPUT.funcCount, the algorithm used in OUTPUT.algorithm,% & the number of CG iterations (if used) in OUTPUT.cgiterations, the first-order&% & optimality (if used) in OUTPUT.firstorderopt, and the exit message in% & OUTPUT.message.%% & [X,FVAL,EXITFLAG,OUTPUT,JACOB]=FSOLVE(FUN,X0,...) returns the&% & Jacobian of FUN at X. &%% & Examples% & & FUN can be specified using @:% & & & &x = fsolve(@myfun,[2 3 4],optimset('Display','iter'))%% & where myfun is a MATLAB function such as:%% & & & function F = myfun(x)% & & & F = sin(x);%% & FUN can also be an anonymous function:%% & & & x = fsolve(@(x) sin(3*x),[1 4],optimset('Display','off'))%% & If FUN is parameterized, you can use anonymous functions to capture the&% & problem-dependent parameters. Suppose you want to solve the system of&% & nonlinear equations given in the function myfun, which is parameterized&% & by its second argument c. Here myfun is an M-file function such as% & &&% & & & function F = myfun(x,c)% & & & F = [ 2*x(1) - x(2) - exp(c*x(1))% & & & & & & -x(1) + 2*x(2) - exp(c*x(2))];% & & & & &&% & To solve the system of equations for a specific value of c, first assign the% & value to c. Then create a one-argument anonymous function that captures&% & that value of c and calls myfun with two arguments. Finally, pass this anonymous% & function to FSOLVE:%% & & & c = -1; % define parameter first% & & & x = fsolve(@(x) myfun(x,c),[-5;-5])%% & See also OPTIMSET, LSQNONLIN, @, INLINE.% & Copyright
The MathWorks, Inc.% & $Revision: 1.41.4.12 $ &$Date:
20:18:49 $% ------------Initialization----------------defaultopt = struct('Display','final','LargeScale','off',...&& 'NonlEqnAlgorithm','dogleg',...&& 'TolX',1e-6,'TolFun',1e-6,'DerivativeCheck','off',...&& 'Diagnostics','off','FunValCheck','off',...&& 'Jacobian','off','JacobMult',[],...% JacobMult set to [] by default&& 'JacobPattern','sparse(ones(Jrows,Jcols))',...&& 'MaxFunEvals','100*numberOfVariables',...&& 'DiffMaxChange',1e-1,'DiffMinChange',1e-8,...&& 'PrecondBandWidth',0,'TypicalX','ones(numberOfVariables,1)',...&& 'MaxPCGIter','max(1,floor(numberOfVariables/2))', ...&& 'TolPCG',0.1,'MaxIter',400,...&& 'LineSearchType','quadcubic','OutputFcn',[],'PlotFcns',[]);% If just 'defaults' passed in, return the default options in Xif nargin==1 && nargout &= 1 && isequal(FUN,'defaults')&& x =&& returnendif nargin & 3, options=[]; end% Detect problem structure inputif nargin == 1&& &if isa(FUN,'struct')&& & & &[FUN,x,options] = separateOptimStruct(FUN);&& &else % Single input and non-structure.&& & & &error('optim:fsolve:InputArg','The input to FSOLVE should be either a structure with valid fields or consist of at least two arguments.');&& &endendif nargin == 0&&error('optim:fsolve:NotEnoughInputs','FSOLVE requires at least two input arguments.')end% Check for non-double inputsif ~isa(x,'double')&&error('optim:fsolve:NonDoubleInput', ...&& & & &'FSOLVE only accepts inputs of data type double.')endLB = []; UB = [];&xstart=x(:);numberOfVariables=length(xstart);large & & & &= 'large-scale';medium & & & = 'medium-scale: line search';dogleg & & & = 'trust-region dogleg';switch optimget(options,'Display',defaultopt,'fast')&& &case {'off','none'}&& & & &verbosity = 0;&& &case 'iter'&& & & &verbosity = 2;&& &case 'final'&& & & &verbosity = 1;&& &case 'testing'&& & & &verbosity = I&& &otherwise&& & & &verbosity = 1;enddiagnostics = isequal(optimget(options,'Diagnostics',defaultopt,'fast'),'on');gradflag = &strcmp(optimget(options,'Jacobian',defaultopt,'fast'),'on');% 0 means large-scale trust-region, 1 means medium-scale algorithmmediumflag = strcmp(optimget(options,'LargeScale',defaultopt,'fast'),'off');funValCheck = strcmp(optimget(options,'FunValCheck',defaultopt,'fast'),'on');switch optimget(options,'NonlEqnAlgorithm',defaultopt,'fast')&& &case 'dogleg'&& & & &algorithmflag = 1;&& &case 'lm'&& & & &algorithmflag = 2;&& &case 'gn'&& & & &algorithmflag = 3;&& &otherwise&& & & &algorithmflag = 1;endmtxmpy = optimget(options,'JacobMult',defaultopt,'fast');if isequal(mtxmpy,'atamult')&& &warning('optim:fsolve:NameClash', ...&& & & &['Potential function name clash with a Toolbox helper function:\n' ...&& & & &'Use a name besides ''atamult'' for your JacobMult function to\n' ...&& & & &'avoid errors or unexpected results.'])end% Convert to inline function as neededif ~isempty(FUN) &% will detect empty string, empty matrix, empty cell array&& &funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);else&& &error('optim:fsolve:InvalidFUN', ...&& & & &['FUN must be a function name, valid string expression,\n' ...&& & & &' or, FUN may be a cell array that contains these type of objects.'])endJAC = [];x(:) =switch funfcn{1}&& &case 'fun'&& & & &fuser = feval(funfcn{3},x,varargin{:});&& & & &f = fuser(:);&& & & &nfun=length(f);&& &case 'fungrad'&& & & &[fuser,JAC] = feval(funfcn{3},x,varargin{:});&& & & &f = fuser(:);&& & & &nfun=length(f);&& &case 'fun_then_grad'&& & & &fuser = feval(funfcn{3},x,varargin{:});&& & & &f = fuser(:);&& & & &JAC = feval(funfcn{4},x,varargin{:});&& & & &nfun=length(f);&& &otherwise&& & & &error('optim:fsolve:UndefinedCalltype','Undefined calltype in FSOLVE.')endif gradflag&& &% check size of JAC&& &[Jrows, Jcols]=size(JAC);&& &if isempty(mtxmpy)&& & & &% Not using 'JacobMult' so Jacobian must be correct size&& & & &if Jrows~=nfun || Jcols~=numberOfVariables&& & & & & &error('optim:fsolve:InvalidJacobian', ...&& & & & & & & &['User-defined Jacobian is not the correct size:\n' ...&& & & & & & & &' the Jacobian matrix should be %d-by-%d.'],nfun,numberOfVariables)&& & & &end&& &endelse&& &Jrows =&& &Jcols = numberOfVendXDATA = []; YDATA = []; caller = 'fsolve';% Choose what algorithm to run: determine (i) OUTPUT.algorithm and&% (ii) if and only if OUTPUT.algorithm = medium, also option.LevenbergMarquardt.% Option LevenbergMarquard it's not user settable. For% this reason we change this option directly, users should use% optimset.if ~mediumflag&&& &if nfun &= numberOfVariables&& & & &% large-scale method and enough equations (as many as variables)&& & & &OUTPUT.algorithm =&& &else&&& & & &% large-scale method and not enough equations - switch to medium-scale algorithm&& & & &warning('optim:fsolve:FewerFunsThanVars', ...&& & & & & & & &['Large-scale method requires at least as many eq\n' ...&& & & & & & & & ' using line-search method instead.'])&& & & &OUTPUT.algorithm =&& & & &options.LevenbergMarquardt = 'off';&&& &endelse&& &if algorithmflag == 1 && nfun == numberOfVariables&&& & & &OUTPUT.algorithm =&& &elseif algorithmflag == 1 && nfun ~= numberOfVariables&& & & &warning('optim:fsolve:NonSquareSystem', ...&& & & & & & & &['Default trust-region dogleg method of FSOLVE cannot\n handle non- ', ...&& & & & & & & & 'using Gauss-Newton method instead.']);&& & & &OUTPUT.algorithm =&& & & &options.LevenbergMarquardt = 'off';&& &elseif algorithmflag == 2&& & & &OUTPUT.algorithm =&& & & &options.LevenbergMarquardt = 'on';&& &else % algorithmflag == 3&& & & &OUTPUT.algorithm =&& & & &options.LevenbergMarquardt = 'off';&& &endendif diagnostics & 0&& &% Do diagnostics on information so far&& &constflag = 0; gradconstflag = 0; non_eq=0;non_ineq=0;lin_eq=0;lin_ineq=0;&& &confcn{1}=[];c=[];ceq=[];cGRAD=[];ceqGRAD=[];&& &hessflag = 0; HESS=[];&& &diagnose('fsolve',OUTPUT,gradflag,hessflag,constflag,gradconstflag,...&& & & &mediumflag,options,defaultopt,xstart,non_eq,...&& & & &non_ineq,lin_eq,lin_ineq,LB,UB,funfcn,confcn,f,JAC,HESS,c,ceq,cGRAD,ceqGRAD);end% Execute algorithmif isequal(OUTPUT.algorithm, large)&& &if ~gradflag&& & & &Jstr = optimget(options,'JacobPattern',defaultopt,'fast');&& & & &if ischar(Jstr)&& & & & & &if isequal(lower(Jstr),'sparse(ones(jrows,jcols))')&& & & & & & & &Jstr = sparse(ones(Jrows,Jcols));&& & & & & &else&& & & & & & & &error('optim:fsolve:InvalidJacobPattern', ...&& & & & & & & & & &'Option ''JacobPattern'' must be a matrix if not the default.')&& & & & & &end&& & & &end&& &else&& & & &Jstr = [];&& &end&& &computeLambda = 0;&& &[x,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msg]=...&& & & &snls(funfcn,x,LB,UB,verbosity,options,defaultopt,f,JAC,XDATA,YDATA,caller,...&& & & &Jstr,computeLambda,varargin{:});elseif isequal(OUTPUT.algorithm, dogleg)&& &% trust region dogleg method&& &Jstr = [];&& &[x,FVAL,JACOB,EXITFLAG,OUTPUT,msg]=...&& & & &trustnleqn(funfcn,x,verbosity,gradflag,options,defaultopt,f,JAC,...&& & & &Jstr,varargin{:});else&& &% line search (Gauss-Newton or Levenberg-Marquardt)&& &[x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...&& & & &nlsq(funfcn,x,verbosity,options,defaultopt,f,JAC,XDATA,YDATA,caller,varargin{:});endResnorm = FVAL'*FVAL; &% assumes FVAL still a vectorif EXITFLAG & 0 % if we think we converged:&& &if Resnorm & sqrt(optimget(options,'TolFun',defaultopt,'fast'))&& & & &OUTPUT.message = ...&& & & & & &sprintf(['Optimizer appears to be converging to a minimum that is not a root:\n' ...&& & & & & &'Sum of squares of the function values is & sqrt(options.TolFun).\n' ...&& & & & & &'Try again with a new starting point.']);&& & & &if verbosity & 0&& & & & & &disp(OUTPUT.message)&& & & &end&& & & &EXITFLAG = -2;&& &else&& & & &OUTPUT.message =&& & & &if verbosity & 0&& & & & & &disp(OUTPUT.message);&& & & &end&& &endelse&& &OUTPUT.message =&& &if verbosity & 0&& & & &disp(OUTPUT.message);&& &endend% Reset FVAL to shape of the user-function output, fuserFVAL = reshape(FVAL,size(fuser));&solve 是求解符号函数的,fsolve在求解非线性方程组需要付给其初值,初值不同,结果不同。x=fsolve(fun,x0)求解fun(x)=0的解,x0是初值,fun是函数,x就是解 因为fsolve使用迭代法求解方程的,所以总要有个迭代的初值吧,这个初值就是你给的x0。 比如解方程组 x(1).^2+x(2).^2=1 x(1)=2*x(2) 可以写成 f=@(x)([x(1).^2+x(2).^2-1;x(1)-2*x(2)]) x=fsolve(f,[1 1]) 这里[1 1]就是初值,其实初值一般情况下可以随便给的。&
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢}

我要回帖

更多关于 matlab fsolve函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信