📄 nonlin_gg.m
字号:
iter = 1;while ~done % Select element with largest derivative if grad_given if verbose display('Calculating gradient...') end grad = Grad(s,x); else if verbose display('Estimating gradient...') tt=toc; end grad = zeros(m,1); for cm=1:m DELTA = zeros(m,1); DELTA(cm) = 0.5*g_tol; grad(cm) = (C(s+DELTA,x)-C(s-DELTA,x))/g_tol; end if verbose display(['Elapsed time: ' num2str(toc-tt)]) end end [val ind]=max(abs(w.*grad)); if ~sum(IND==ind) IND=[IND ind]; if verbose display(sprintf('New coefficient selected.')) end end N_IND=1:m; N_IND(IND)=[]; if optimiser_given if verbose display(sprintf('Optimising using supplied function.')) end s = OPT(s,x,IND); ne = C(s,x); else if verbose display(sprintf('Optimising using gradient optimisation.')) tt=toc; end CHANGE=1; count=0; while norm(grad)>grad_tol && count<Max_Grad && abs(CHANGE)>1e-12 && ~isempty (find(grad, 1)) count=count+1; % ESTIMATE GRADIENT if count==1 grad(N_IND)=zeros(size(N_IND)); else if grad_given grad = Grad(s,x); grad(N_IND) = 0; else grad = zeros(m,1); for cm=1:length(IND) DELTA = zeros(m,1); DELTA(IND(cm)) = 0.5*g_tol; grad(IND(cm)) = (C(s+DELTA,x)-C(s-DELTA,x))/g_tol; end end end if count == Max_Grad display(sprintf('Maximum number of gradient steps reached.')) end % Update s it = 0; as = step_size*grad; nne = C(s-as,x); count = 0; while ne <= nne && ~isempty (find(grad, 1)) && count < 15 count = count+1; it = it+1; as = step_size*10^(-it)*grad; nne = C(s-as,x); end s = s-as; % Calculate error and change in error CHANGE = ne-nne; ne = nne; if verbose && toc-tt>10 display(sprintf('Norm of gradient --- %i,',norm(grad))); tt=toc; end end end ERR=ne^2/n; if comp_err err_cost(iter)=ERR; end if comp_time iter_time(iter)=toc; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Are we done yet?%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if strcmp(STOPCRIT,'M') if length(IND) >= STOPTOL done =1; elseif verbose && toc-t>10 display(sprintf('Nonzero elements %i --- %i iterations to go --- current cost %i',length(IND) ,STOPTOL-length(IND),ne)) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end elseif strcmp(STOPCRIT,'cost') if comp_err if err_cost(iter)<STOPTOL; done = 1; elseif verbose && toc-t>10 display(sprintf('Iteration %i. --- %i cost',iter ,err_cost(iter))) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end else if ERR<STOPTOL; done = 1; elseif verbose && toc-t>10 display(sprintf('Iteration %i. --- %i cost',iter ,ERR)) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end end elseif strcmp(STOPCRIT,'cost_change') && iter >=2 if comp_err && iter >=2 if ((err_cost(iter-1)-err_cost(iter))/sigsize <STOPTOL); done = 1; elseif verbose && toc-t>10 display(sprintf('Iteration %i. --- %i cost change',iter ,(err_cost(iter-1)-err_cost(iter))/sigsize )) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end else if ((oldERR - ERR)/sigsize < STOPTOL); done = 1; elseif verbose && toc-t>10 display(sprintf('Iteration %i. --- %i cost change',iter ,(oldERR - ERR)/sigsize)) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end end elseif strcmp(STOPCRIT,'corr') if max(abs(DR)) < STOPTOL; done = 1; elseif verbose && toc-t>10 display(sprintf('Iteration %i. --- %i corr',iter ,max(abs(DR)))) if do_plot PlotFunc(s) end t=toc; elseif do_plot && toc-t>10 PlotFunc(s) t=toc; end end % Also stop if residual gets too small or maxIter reached if comp_err if err_cost(iter)<1e-16 display('Stopping. Exact signal representation found!') done=1; end else if iter>1 if ERR<1e-16 display('Stopping. Exact signal representation found!') done=1; end end end if iter >= MAXITER display('Stopping. Maximum number of iterations reached!') done = 1; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% If not done, take another round%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if ~done iter=iter+1; oldERR=ERR; endend %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Only return as many elements as iterations%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if nargout >=2 err_cost = err_cost(1:iter);endif nargout ==3 iter_time = iter_time(1:iter);endif verbose display('Done') end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -