使用 Matlab 代码构建列表的问题

使用 Matlab 代码构建列表的问题

我无论如何也想不出如何让框架来处理这个列表。我对 Latex 还很陌生。据我所知,mcode 包是将列表格式化为 matlab 代码的工具。每次我尝试用框架来构建它时\framebox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{ ..CODE..}}

我收到错误..

("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\psnfss\t1pcr.fd")
Runaway argument?
! Paragraph ended before \lst@next was complete.
<to be read again> 
                   \par 
l.77 }}

这就是我需要构建的整个部分(包括包裹)。

\usepackage{amssymb,amsfonts,amsmath}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage{graphicx}
\usepackage[numbered]{matlab-prettifier}
\usepackage[]{mcode}
\usepackage{adjustbox}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\usepackage{color}
\usepackage{float}

\framebox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{
\begin{parts}
\part \textbf{Solution:} Conversion of gausselim to gaussdet
\begin{lstlisting}
function d = gaussdet(a)
[n,m] = size(a);
for j = 1:n
    den = a(j,j);
    for i = j+1:n
        mult = a(i,j)/den;
        a(i,j+1:n) = a(i,j+1:n)-mult*a(j,j+1:n);
    end
end
d = zeros(1,n);
for j = 1:n
    d(j) = a(j,j);      %Diagonal elements of a
end
d = prod(d);            %Product of the diagonal elements extracted  
end
\end{lstlisting}


\part  Matrix size vs. Computation time of gaussdet and cofactor functions \\ 
On last page\\
\part Have a look at the documentation for the built-in Matlab function \verb$det$. What method is Matlab using to compute the determinant?\\

Matlab's built-in $det( )$ function uses an $\mathbf{LU}$ factorization. Once the matrix is factored, it computes the products of the diagonals. \\

 What are some of the potential limitations of this approach? \\
Obvious limitations are the fact that floating point numbers in a computer are limited. When the matrix is built and computed, each iteration is subject to round-off errors, resulting in an inaccurate computation. Other limitations arise from this fact such as a matrix with elements comprised of very small numbers. As the $ \lim_{i \to n}\rightarrow  0$ , you're computer is subject to increasing round off errors and will eventuall return a value of$ 0$. This doesn't actually convey wether or not the matrix is actually singular.\\
How is it recommended to compute the determinant of the inverse of a matrix? Why?\\
By using the fact that $det(A^{-1}) = \frac{1}{det(A)}$ \\
The other option is $AI \xrightarrow[Operations]{Row} IA^{-1} $ where this calculation is subject to rounding error. Then you have to use a function, such as Gaussian Elimination on $A^{-1}$, where you're subject to another set of rounding errors. This can lead to very inaccurate computations.\\

Why? What might lead Matlab to compute a determinant very far from zero for a singular matrix?\\
As mentioned before, the real limitation is the way your computer stores and computes values of floating point integers. If there is sufficient roundoff error when computing a matrix, the returned value can be very wrong.\\
\end{parts}
}}

答案1

目前尚不清楚parts环境和\parts 在做什么,因此提供了以下不包含这些内容的解决方案。

您的代码片段中有两件事您无法做到:

  1. lstlisting将作为参数的一部分添加到函数中(如\fbox\framebox\parbox)。

  2. 添加\verb作为函数参数的一部分。

大致上,上述两件事都在verbatim在命令中

我们通过环境将整个代码片段存储在一个盒子(比如\codebox)中来克服(1)。使用而不是 可以lrbox轻松克服(2):\texttt\verb

在此处输入图片描述

\documentclass{article}

\usepackage{amssymb,amsfonts,amsmath}
\usepackage[letterpaper,margin=1in]{geometry}
\usepackage[numbered]{matlab-prettifier}
\usepackage{mcode}

\newsavebox{\codebox}

\begin{document}

% Store the listing/code in a box
\begin{lrbox}{\codebox}
\begin{lstlisting}
function d = gaussdet(a)
[n,m] = size(a);
for j = 1:n
    den = a(j,j);
    for i = j+1:n
        mult = a(i,j)/den;
        a(i,j+1:n) = a(i,j+1:n)-mult*a(j,j+1:n);
    end
end
d = zeros(1,n);
for j = 1:n
    d(j) = a(j,j);      %Diagonal elements of a
end
d = prod(d);            %Product of the diagonal elements extracted  
end
\end{lstlisting}
\end{lrbox}

\noindent
\framebox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{%
\textbf{Solution:} Conversion of gausselim to gaussdet

\usebox{\codebox}

Matrix size vs. Computation time of gaussdet and cofactor functions
On last page, Have a look at the documentation for the built-in Matlab function \texttt{det}. What method is Matlab using to compute the determinant?

Matlab's built-in \texttt{det( )} function uses an~$\mathbf{LU}$ factorization. Once the matrix is factored, it computes the products of the diagonals.

What are some of the potential limitations of this approach?
Obvious limitations are the fact that floating point numbers in a computer are limited. When the matrix is built and computed, each iteration is subject to round-off errors, resulting in an inaccurate computation. Other limitations arise from this fact such as a matrix with elements comprised of very small numbers. As the $ \lim_{i \to n}\rightarrow  0$, you're computer is subject to increasing round off errors and will eventuall return a value of~$0$. This doesn't actually convey wether or not the matrix is actually singular.

How is it recommended to compute the determinant of the inverse of a matrix? Why?
By using the fact that $\text{det}(A^{-1}) = \frac{1}{\text{det}(A)}$

The other option is $AI \xrightarrow[\text{Operations}]{\text{Row}} IA^{-1}$ where this calculation is subject to rounding error. Then you have to use a function, such as Gaussian Elimination on $A^{-1}$, where you're subject to another set of rounding errors. This can lead to very inaccurate computations.

Why? What might lead Matlab to compute a determinant very far from zero for a singular matrix?

As mentioned before, the real limitation is the way your computer stores and computes values of floating point integers. If there is sufficient roundoff error when computing a matrix, the returned value can be very wrong.
}}
\end{document}

答案2

lstlisting是逐字环境,不能放在这样的盒子里。最简单的解决方案是使用包frame提供的密钥listings

\documentclass{article}

\usepackage{listings} % use mcode instead
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{lstlisting}[frame=single, breaklines]
function d = gaussdet(a)
[n,m] = size(a);
for j = 1:n
    den = a(j,j);
    for i = j+1:n
        mult = a(i,j)/den;
        a(i,j+1:n) = a(i,j+1:n)-mult*a(j,j+1:n);
    end
end
d = zeros(1,n);
for j = 1:n
    d(j) = a(j,j);      %Diagonal elements of a
end
d = prod(d);            %Product of the diagonal elements extracted  
end
\end{lstlisting}

\end{document}

输出:

在此处输入图片描述

相关内容