| 8 | 1/1 | 返回列表 |
| 查看: 2318 | 回復: 7 | |||
hustesewzw鐵蟲 (初入文壇)
|
[求助]
求助matlab高手指點:大型非線性方程組(牛頓拉佛森法) 已有3人參與
|
最近想完成一個生化模型問題,模型主體方程為微分方程組,用 差分法轉(zhuǎn)化為多元非線性方程組。擬采用牛頓拉佛森法求解,方程組比較大:99個未知數(shù),99個方程,matlab程序已寫好,問題是求解到最后有部分方程的y值始終比較大(我設(shè)置的是1e-4),x的收斂條件也為1e-4(可以滿足)。jacobi迭代次數(shù)設(shè)置為30次。請問我的y值不能滿足條件的原因是什么呢?是我的初始值取得有問題嗎?有沒有高手為我指點一下迷津,在此深表感謝。!獻上我的全部金幣。 |
金蟲 (正式寫手)
|
分享一個NewtonRaphson的算法給你, function [x, resnorm, F, exitflag, output, jacob] = newtonraphson(fun, x0, options) % NEWTONRAPHSON Solve set of non-linear equations using Newton-Raphson method. % % [X, RESNORM, F, EXITFLAG, OUTPUT, JACOB] = NEWTONRAPHSON(FUN, X0, OPTIONS) % FUN is a function handle that returns a vector of residuals equations, F, % and takes a vector, x, as its only argument. When the equations are % solved by x, then F(x) == zeros(size(F( , 1)).% % Optionally FUN may return the Jacobian, Jij = dFi/dxj, as an additional % output. The Jacobian must have the same number of rows as F and the same % number of columns as x. The columns of the Jacobians correspond to d/dxj and % the rows correspond to dFi/d. % % EG: J23 = dF2/dx3 is the 2nd row ad 3rd column. % % If FUN only returns one output, then J is estimated using a center % difference approximation, % % Jij = dFi/dxj = (Fi(xj + dx) - Fi(xj - dx))/2/dx. % % NOTE: If the Jacobian is not square the system is either over or under % constrained. % % X0 is a vector of initial guesses. % % OPTIONS is a structure of solver options created using OPTIMSET. % EG: options = optimset('TolX', 0.001). % % The following options can be set: % * OPTIONS.TOLFUN is the maximum tolerance of the norm of the residuals. % [1e-6] % * OPTIONS.TOLX is the minimum tolerance of the relative maximum stepsize. % [1e-6] % * OPTIONS.MAXITER is the maximum number of iterations before giving up. % [100] % * OPTIONS.DISPLAY sets the level of display: {'off', 'iter'}. % ['iter'] % % X is the solution that solves the set of equations within the given tolerance. % RESNORM is norm(F) and F is F(X). EXITFLAG is an integer that corresponds to % the output conditions, OUTPUT is a structure containing the number of % iterations, the final stepsize and exitflag message and JACOB is the J(X). % % See also OPTIMSET, OPTIMGET, FMINSEARCH, FZERO, FMINBND, FSOLVE, LSQNONLIN % %% initialize % There are no argument checks! x0 = x0( ; % needs to be a column vector% set default options oldopts = optimset( ... 'TolX', 1e-12, 'TolFun', 1e-6, 'MaxIter', 100, 'Display', 'iter'); if nargin<3 options = oldopts; % use defaults else options = optimset(oldopts, options); % update default with user options end FUN = @(x)funwrapper(fun, x); % wrap FUN so it always returns J %% get options TOLX = optimget(options, 'TolX'); % relative max step tolerance TOLFUN = optimget(options, 'TolFun'); % function tolerance MAXITER = optimget(options, 'MaxIter'); % max number of iterations DISPLAY = strcmpi('iter', optimget(options, 'Display')); % display iterations TYPX = max(abs(x0), 1); % x scaling value, remove zeros ALPHA = 1e-4; % criteria for decrease MIN_LAMBDA = 0.1; % min lambda MAX_LAMBDA = 0.5; % max lambda %% set scaling values % TODO: let user set weights weight = ones(numel(FUN(x0)),1); J0 = weight*(1./TYPX'); % Jacobian scaling matrix %% set display if DISPLAY fprintf('\n%10s %10s %10s %10s %10s %12s\n', 'Niter', 'resnorm', 'stepnorm', ... 'lambda', 'rcond', 'convergence') for n = 1:67,fprintf('-'),end,fprintf('\n') fmtstr = '%10d %10.4g %10.4g %10.4g %10.4g %12.4g\n'; printout = @(n, r, s, l, rc, c)fprintf(fmtstr, n, r, s, l, rc, c); end %% check initial guess x = x0; % initial guess [F, J] = FUN(x); % evaluate initial guess Jstar = J./J0; % scale Jacobian if any(isnan(Jstar( )) || any(isinf(Jstar( ))exitflag = -1; % matrix may be singular else exitflag = 1; % normal exit end if issparse(Jstar) rc = 1/condest(Jstar); else if any(isnan(Jstar( ))rc = NaN; elseif any(isinf(Jstar( ))rc = Inf; else rc = 1/cond(Jstar); % reciprocal condition end end resnorm = norm(F); % calculate norm of the residuals dx = zeros(size(x0));convergence = Inf; % dummy values %% solver Niter = 0; % start counter lambda = 1; % backtracking if DISPLAY,printout(Niter, resnorm, norm(dx), lambda, rc, convergence);end while (resnorm>TOLFUN || lambda<1) && exitflag>=0 && Niter<=MAXITER if lambda==1 %% Newton-Raphson solver Niter = Niter+1; % increment counter dx_star = -Jstar\F; % calculate Newton step % NOTE: use isnan(f) || isinf(f) instead of STPMAX dx = dx_star.*TYPX; % rescale x g = F'*Jstar; % gradient of resnorm slope = g*dx_star; % slope of gradient fold = F'*F; % objective function xold = x; % initial value lambda_min = TOLX/max(abs(dx)./max(abs(xold), 1)); end if lambda<lambda_min exitflag = 2; % x is too close to XOLD break elseif any(isnan(dx)) || any(isinf(dx)) exitflag = -1; % matrix may be singular break end x = xold+dx*lambda; % next guess [F, J] = FUN(x); % evaluate next residuals Jstar = J./J0; % scale next Jacobian f = F'*F; % new objective function %% check for convergence lambda1 = lambda; % save previous lambda if f>fold+ALPHA*lambda*slope if lambda==1 lambda = -slope/2/(f-fold-slope); % calculate lambda else A = 1/(lambda1 - lambda2); B = [1/lambda1^2,-1/lambda2^2;-lambda2/lambda1^2,lambda1/lambda2^2]; C = [f-fold-lambda1*slope;f2-fold-lambda2*slope]; coeff = num2cell(A*B*C); [a,b] = coeff{:}; if a==0 lambda = -slope/2/b; else discriminant = b^2 - 3*a*slope; if discriminant<0 lambda = MAX_LAMBDA*lambda1; elseif b<=0 lambda = (-b+sqrt(discriminant))/3/a; else lambda = -slope/(b+sqrt(discriminant)); end end lambda = min(lambda,MAX_LAMBDA*lambda1); % minimum step length end elseif isnan(f) || isinf(f) % limit undefined evaluation or overflow lambda = MAX_LAMBDA*lambda1; else lambda = 1; % fraction of Newton step end if lambda<1 lambda2 = lambda1;f2 = f; % save 2nd most previous value lambda = max(lambda,MIN_LAMBDA*lambda1); % minimum step length continue end %% display resnorm0 = resnorm; % old resnorm resnorm = norm(F); % calculate new resnorm convergence = log(resnorm0/resnorm); % calculate convergence rate stepnorm = norm(dx); % norm of the step if any(isnan(Jstar( )) || any(isinf(Jstar( ))exitflag = -1; % matrix may be singular break end if issparse(Jstar) rc = 1/condest(Jstar); else rc = 1/cond(Jstar); % reciprocal condition end if DISPLAY,printout(Niter, resnorm, stepnorm, lambda1, rc, convergence);end end %% output output.iterations = Niter; % final number of iterations output.stepsize = dx; % final stepsize output.lambda = lambda; % final lambda if Niter>=MAXITER exitflag = 0; output.message = 'Number of iterations exceeded OPTIONS.MAXITER.'; elseif exitflag==2 output.message = 'May have converged, but X is too close to XOLD.'; elseif exitflag==-1 output.message = 'Matrix may be singular. Step was NaN or Inf.'; else output.message = 'Normal exit.'; end jacob = J; end function [F, J] = funwrapper(fun, x) % if nargout<2 use finite differences to estimate J try [F, J] = fun(x); catch F = fun(x); J = jacobian(fun, x); % evaluate center diff if no Jacobian end F = F( ; % needs to be a column vectorend function J = jacobian(fun, x) % estimate J dx = eps^(1/3); % finite difference delta nx = numel(x); % degrees of freedom nf = numel(fun(x)); % number of functions J = zeros(nf,nx); % matrix of zeros for n = 1:nx % create a vector of deltas, change delta_n by dx delta = zeros(nx, 1); delta(n) = delta(n)+dx; dF = fun(x+delta)-fun(x-delta); % delta F J(:, n) = dF( /dx/2; % derivatives dF/d_nend end |
金蟲 (正式寫手)
金蟲 (正式寫手)
鐵桿木蟲 (職業(yè)作家)
鐵蟲 (初入文壇)
銀蟲 (小有名氣)
| 8 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 085700資源與環(huán)境308求調(diào)劑 +3 | 墨墨漠 2026-03-18 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 311求調(diào)劑 +4 | 冬十三 2026-03-18 | 4/200 |
|
|
[考研] 328求調(diào)劑,英語六級551,有科研經(jīng)歷 +3 | 生物工程調(diào)劑 2026-03-17 | 7/350 |
|
|
[考研] 一志愿985,本科211,0817化學工程與技術(shù)319求調(diào)劑 +7 | Liwangman 2026-03-15 | 7/350 |
|
|
[考研] 085600材料與化工 +5 | 安全上岸! 2026-03-16 | 5/250 |
|
|
[考研] 化工學碩306求調(diào)劑 +10 | 42838695 2026-03-12 | 10/500 |
|
|
[考博] 環(huán)境領(lǐng)域全國重點實驗室招收博士1-2名 +3 | QGZDSYS 2026-03-13 | 5/250 |
|
|
[考研] 生物學071000 329分求調(diào)劑 +3 | 我愛生物生物愛?/a> 2026-03-17 | 3/150 |
|
|
[考研] 326求調(diào)劑 +5 | 上岸的小葡 2026-03-15 | 6/300 |
|
|
[考研] 梁成偉老師課題組歡迎你的加入 +8 | 一鴨鴨喲 2026-03-14 | 10/500 |
|
|
[考研] 304求調(diào)劑 +4 | ahbd 2026-03-14 | 4/200 |
|
|
[考研] 285求調(diào)劑 +6 | ytter 2026-03-12 | 6/300 |
|
|
[考研] 26考研一志愿中國石油大學(華東)305分求調(diào)劑 +3 | 嘉年新程 2026-03-15 | 3/150 |
|
|
[考研] 本科南京大學一志愿川大藥學327 +3 | 麥田耕者 2026-03-14 | 3/150 |
|
|
[考研] 265求調(diào)劑 +4 | 威化餅07 2026-03-12 | 4/200 |
|
|
[考研] 招收0805(材料)調(diào)劑 +3 | 18595523086 2026-03-13 | 3/150 |
|
|
[考研] 土木第一志愿276求調(diào)劑,科研和技能十分豐富,求新興方向的導師收留 +3 | 土木小天才 2026-03-12 | 3/150 |
|
|
[考研] 0817化學工程與技術(shù)考研312分調(diào)劑 +3 | T123 tt 2026-03-12 | 3/150 |
|
|
[考博] 26讀博 +4 | Rui135246 2026-03-12 | 10/500 |
|
|
[考研] 333求調(diào)劑 +3 | 152697 2026-03-12 | 4/200 |
|