如何在 LaTeX 中创建随机数学问题?

如何在 LaTeX 中创建随机数学问题?

我想创建一个数学问题解答表。系数abc必须随机分配,以便生成的 pdf 将显示数值二次方程而不是符号二次方程。注意a不能为零。

\documentclass{article}
\begin{document}
\section{Problem}
Let \(x_1\) and \(x_2\) be the roots of \(a x^2 +bx +c=0\). Find  \(x_1x_2\).

\section{Solution}
\[
x_1x_2=\frac{c}{a}
\]
\end{document}

请让解决方案自动简化。例如:\frac{c}{a}=3a=2和时c=6


事实上,实际情况比这个更复杂。不仅方程式必须随机生成,而且我在 PSTricks 中编写的相关图形绘图也必须随机生成。


我选择 Paulo 的答案是因为使用 Maxima 作为计算引擎是一个非常非常棒的想法。其他答案也很好而且很有创意,尤其是 Altermundus 的。

答案1

这只是部分解决方案,可能不是您想要的。不过,我发布它只是为了记录,以防有人需要它。=)

我会用千里马

Maxima 是一个用于处理符号和数值表达式的系统,包括微分、积分、泰勒级数、拉普拉斯变换、常微分方程、线性方程组、多项式以及集合、列表、向量、矩阵和张量。Maxima 通过使用精确分数、任意精度整数和可变精度浮点数来产生高精度数值结果。Maxima 可以绘制二维和三维函数和数据。

首先,我创建了一个名为解决命令在我的工作目录中:

@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r %1 | head --lines=-1

我现在不在我的 Linux 机器里,但是.sh文件类似。

使用 Jake 的代码,我添加了对此脚本的调用:

\documentclass{article}
\usepackage{pgf}

\begin{document} 

\section{Problem}

\pgfmathsetseed{1}
\pgfmathtruncatemacro\coeffa{random(2,10)}
\pgfmathtruncatemacro\coeffb{random(2,10)}
\pgfmathtruncatemacro\coeffc{random(2,10)}

% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
\immediate\write18{solve.cmd "tex(solve(\coeffa*x^2 + (\coeffb*x) + (\coeffc)));" > solution1.tex}

Let \(x_1\) and \(x_2\) be the roots of \(\coeffa x^2 +\coeffb x +\coeffc=0\). Find  \(x_1x_2\).

\section{Solution}

% read the solution
\input solution1

\end{document}

我尝试将 Maxima 的函数与调用一起使用plot2d,但未能修复文件扩展名的小错误。我打算创建一个png图并将gnuplot其包含在内。抱歉,它还没有成功。

不要忘记使用--shell-escape--enable-write18标志。

编辑:这让我头疼不已,但我想我找到了一个很好的改进方法。=)

我不习惯使用 Maxima,尽管我喜欢用它进行一些简单的计算。如果我的代码有误,请帮助我改进。谢谢。

我编辑了解决命令我的工作目录中的文件如下所示:

@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem   1. generate the three coefficients,
rem   2. output both equation and solution to a file,
rem   3. get rid of the 'false' lines in the text (Maxima's fault)
rem      (we do this thanks to 'grep' using an inverse flag)
rem   4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt

现在到了棘手的部分,LaTeX 代码:

\documentclass{article}

\immediate\write18{solve.cmd}

\newread\myread
\openin\myread=problem.txt
\read\myread to \theequation
\read\myread to \thesolution

\begin{document}

\section{Problem}

This is our equation:

\theequation

\section{Solution}

And this is our solution:

\thesolution

\end{document}

让我们检查一下代码:首先,我们运行solve.cmd,它将方程和解都输出到名为的文件中problem.txt。然后我们创建一个\newread\myread并打开此文件\openin\myread=problem.txt。现在我们将第一行映射到\theequation(这是我们在 Maxima 代码中定义的方程),将第二行映射到\thesolution(相同逻辑)。然后我们在需要时调用它。

再次,我的 Maxima 代码可能存在一些缺陷。但我希望您能理解其中的意思。如果将方程或解决方案更改为内联版本,我们可能会sed在其中进行一些操作并替换模式。=)

答案2

我知道我来晚了,但我会用智者和 SageTeX:

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\section{Problem}

\begin{sagesilent}
a = ZZ.random_element(-10,10)
while a == 0:
   a = ZZ.random_element(-10,10)
b = ZZ.random_element(-10,10)
c = ZZ.random_element(-10,10)
poly = a*x^2 + b*x + c
\end{sagesilent}

Let \(x_1\) and \(x_2\) be the roots of \(\sage{poly}=0\). 
Find  \(x_1x_2\).

\section{Solution}
\[
x_1x_2=\sage{c/a}.
\]

\sageplot{plot(poly) }

\end{document}

输出如下所示:

sagetex 的输出

答案3

液晶显示器软件包包含用于生成(伪)随机数的宏。一个小例子(图也是用 生成的TikZ):

\documentclass{article}
\usepackage[counter=coefa,first=-8, last=12]{lcg}
\usepackage{pgfplots}

\begin{document}

\rand\arabic{coefa}
\let\Coefa\thecoefa

\chgrand[counter=coefb]
\rand\arabic{coefb}
\let\Coefb\thecoefb

\chgrand[counter=coefc]
\rand\arabic{coefc}
\let\Coefc\thecoefc

\section{Problem}
Let \(x_1\) and \(x_2\) be the roots of \( \Coefa x^2 +\Coefb x + \Coefc =0\). Find  \(x_1x_2\).

\section{Solution}
\[
x_1x_2=\frac{\Coefc}{\Coefa}
\]


\begin{tikzpicture}
\begin{axis}[
  xlabel=$x$,
  ylabel={$f(x) = \Coefa x^2 + \Coefb x + \Coefc $}
]
\addplot[color=orange,thick] {\Coefa*x^2 + \Coefb* x + \Coefc};
\end{axis}
\end{tikzpicture}

\end{document}

当然,当编译我的例子时,您会得到不同的系数值,因此会得到不同的方程和图表。

答案4

为了简化分数,你可以使用欧几里得方法,使用下面的最小值

\reduceFrac{27}{81}
$\frac{\rfNumer}{\rfDenom}$

会得到 1/3。简化为整数更困难,因为您需要检查是否还有余数,因此您需要一个小数到分数的转换程序。请参阅如何跳出循环在代码的末尾,您将看到如何使用宏将例如 0.375 转换为小数近似值 768/2048。无法保证其准确性,需要进行一些改进。

\documentclass{article}
\begin{document}
\makeatletter
% Use Euclid's Algorithm to find the greatest 
% common divisor of two integers.
\def\gcd#1#2{{% #1 = a, #2 = b
    \ifnum#2=0 \edef\next{#1}\else
        \@tempcnta=#1 \@tempcntb=#2 \divide\@tempcnta by\@tempcntb
        \multiply\@tempcnta by\@tempcntb  % q*b
        \@tempcntb=#1
        \advance\@tempcntb by-\@tempcnta % remainder in \@tempcntb
        \ifnum\@tempcntb=0
            \@tempcnta=#2
            \ifnum\@tempcnta < 0 \@tempcnta=-\@tempcnta\fi
            \xdef\gcd@next{\noexpand%
                \def\noexpand\thegcd{\the\@tempcnta}}%
        \else
            \xdef\gcd@next{\noexpand\gcd{#2}{\the\@tempcntb}}%
        \fi
    \fi}\gcd@next
}
\newcommand\reduceFrac[2]
{%
   \gcd{#1}{#2}{\@tempcnta=#1 \divide\@tempcnta by\thegcd
   \@tempcntb=#2 \divide\@tempcntb by\thegcd
   \ifnum\@tempcntb<0\relax
      \@tempcntb=-\@tempcntb
        \@tempcnta=-\@tempcnta
    \fi
    \xdef\rfNumer{\the\@tempcnta}
    \xdef\rfDenom{\the\@tempcntb}}%
}

\reduceFrac{27}{81}
$\frac{\rfNumer}{\rfDenom}$
\makeatother
\end{document}

相关内容