您的位置:首页 > 其它

LaTeX 写算法伪码

2015-11-26 14:33 330 查看
本系列文章由 @YhL_Leo 出品,转载请注明出处。

文章链接: /article/3664579.html

LaTeX写算法伪码,需要包含包:

[code]\usepackage{algorithm}
\usepackage{algpseudocode}


有些用例里还会加上:

[code]\usepackage{caption}
\usepackage{algorithmicx}


可以遇到具体情形,再添加~

以常用的Levenberg-Marquardt(L-M)优化方法为例,给出一种方法:

[code]\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
%\usepackage{caption}
%\usepackage{algorithmicx}

\begin{document}    

    \begin{algorithm}
		\caption{\textbf{Levenberg-Marquardt Method}}
		\label{Alg:scanMatching}
		\begin{algorithmic}[1]
			\Require 
			$k:= 0$ ; $\nu:=2$; $\mathbf{x}:=\mathbf{x}_0 $;
			$\mathbf{A}:= \mathbf{J}(\mathbf{x})^T\mathbf{J}(\mathbf{x})$;
			$\mathbf{g}:=\mathbf{J}(\mathbf{x})^T\mathbf{f}(\mathbf{x})$
			\Ensure $found := (\| \mathbf{g}\|_\infty \leq \varepsilon_1)$; $\mu := \tau\cdot max\{ a_{ii} \}$ 
			\State \textbf{while} (\textbf{not} $found $) $\textbf{and}$ $k \leq k_{max}$ $\textbf{do}$ 
			\State \ \ \ \ $k := k+1$; Solve $(\mathbf{A}+\mu\mathbf{I})\mathbf{h}_{lm}) = -\mathbf{g}$
			\State \ \ \ \ $\textbf{if} \ \ \|\mathbf{h}_{lm} \| \leq \varepsilon_2 (\|\mathbf{x} + \varepsilon_2)$
			\State \ \ \ \ \ \ \ \  $found:= \textbf{true}$
			\State \ \ \ \ $\textbf{else}$
			\State \ \ \ \ \ \ \ \ $\mathbf{x}_{new} := \mathbf{x} + \mathbf{h}_{lm}$
			\State \ \ \ \ \ \ \ \ $\varrho:= (F(\mathbf{x}) - F(\mathbf{x}_{new}))/(L(\mathbf{0}) - L(\mathbf{h}_{lm}))$
			\State \ \ \ \ \ \ \ \ $\textbf{if} \ \  \varrho > 0$
			\State \ \ \ \ \ \ \ \ \ \ \ \ $\mathbf{x}:= \mathbf{x}_{new}$
			\State \ \ \ \ \ \ \ \ \ \ \ \ $\mathbf{A} := \mathbf{J}(\mathbf{x})^T\mathbf{J}(\mathbf{x}); \ \ \mathbf{g}:=\mathbf{J}(\mathbf{x})^T\mathbf{f}(\mathbf{x})$
			\State \ \ \ \ \ \ \ \ \ \ \ \ $found := (\|\mathbf{g}\|_{\infty} \leq \varepsilon_1)$
			\State \ \ \ \ \ \ \ \ \ \ \ \ $\mu := \mu \cdot max\{ \frac{1}{3}, 1-(2\varrho-1)^3\}; \ \ \nu:=2$
			\State \ \ \ \ \ \ \ \ $\textbf{else}$
			\State \ \ \ \ \ \ \ \ \ \ \ \ $\mu := \mu \cdot \nu; \ \ \nu := 2\nu$
			\State \ \ \ \ \ \ \ \ $\textbf{end if}$
			\State \ \ \ \ $\textbf{end if}$
			\State \textbf{end while} 
		\end{algorithmic}
	\end{algorithm}

\end{document}


编译结果为:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: