本系列文章由 @YhL_Leo 出品,转载请注明出处。

文章链接: http://blog.csdn.net/yhl_leo/article/details/50054953


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

\usepackage{algorithm}
\usepackage{algpseudocode}

有些用例里还会加上:

\usepackage{caption}
\usepackage{algorithmicx}

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


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

\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}

编译结果为:

LaTeX 写算法伪码-LMLPHP

05-16 15:26