CoCalc Shared FilesAaron Tresham Calculus Material / Sage Common Mistakes.texOpen in CoCalc with one click!

\documentclass[12pt]{article} %You may change font size to 10pt or 11pt1\usepackage[top=.5in, bottom=1in, left=1in, right=1in]{geometry} %Change margins here2\title{Common Mistakes with Sage} %Change title here3\author{Aaron Tresham}4\date{\today}56\usepackage{parskip, amsmath, amssymb, latexsym, graphicx, enumerate, sagetex}78\begin{document}9\maketitle10\newcommand{\ds}{\displaystyle}1112\vspace{.1in}1314\section{Common Error Messages}1516Many (but not all) mistakes that you make in Sage will result in an error message. This is usually several lines of output all in red. You can ignore most of the message; just look down at the last line, which tells you the type of error.17181920\subsection{SyntaxError: invalid syntax}2122This is one of the broader errors, and many mistakes can lead to this error. Here are some examples:2324\begin{itemize}2526\item Missing Explicit Multiplication2728Every multiplication in Sage must be explicitly typed.2930For example, to define the function $f(x)=5x^2+3x-2$, you must type a multiplication after the 5 and 3: \verb|f(x)=5*x^2+3*x-2|. If you try \verb|f(x)=5x^2+3x-2|, you will get this error.3132\item Extra End Parenthesis3334Every set of parentheses must have a beginning and an end: $(\ \cdots )$. If you have an extra end parenthesis, you will get this error. For example, try \verb|f(x)=sin(x))|.3536\item Unmarked Comment3738Sometimes you may want to write a note in Sage. You can do this by putting a pound sign (\#) at the beginning of the line. This tells Sage that the rest of the line is a comment and should be ignored when processing. If you leave of the comment marker, Sage will try to intepret your comment as a command, often resulting in a syntax error (may depend on exactly what's in your comment).3940\end{itemize}4142\subsection{SyntaxError: unexpected EOF while parsing}4344I have seen two kinds of mistakes that lead to this message:4546\begin{itemize}4748\item Missing End Parenthesis4950This error usually results from missing an end parenthesis (or, equivalently, having an extra beginning parenthesis).5152For example, \verb|f(x)=sin(x| will result in this error.5354This mistake is more common than having an extra end parenthesis. If you see ``unexpected EOF,'' then check for unmatched parentheses.5556\item Adding a Space after a \%5758Another mistake that results in this message is having a space after the \% sign in a \% decorator. For example, \verb|% var| (note the space after \%) instead of \verb|%var|.5960\end{itemize}6162\subsection{TypeError: \underline{\hspace{.5in}} object is not callable}6364I have seen two kinds of mistakes result in this error:6566\begin{itemize}6768\item Missing Multiplication6970Every multiplication must be explicitly typed (with a *), and missing multiplications may result in this error. For example, try \verb|3(5+2)|. The reason is that Sage confuses this with function notation, such as \verb|f(5+2)|. When you type \verb|3(5+2)|, Sage wants to call a function named ``3,'' but of course 3 is not a function, so it is ``not callable.'' What you want to type is \verb|3*(5+2)|, with an explicit multiplication.7172\item Overwriting a Sage Command7374This error may also result if you try to use a Sage command that you have accidentally overwritten. For example, if you type \verb|cos=5|, the cos function will be overwritten with the number 5. Then if you try \verb|cos(2)|, you will get a ``object is not callable'' error (Sage tries to do 5(2), as if 5 were a function).7576\end{itemize}7778\subsection{TypeError: \underline{\hspace{.5in}} takes at least 1 argument (0 given)}7980If you fail to give a Sage command the number of arguments it expects, you will get an error like this (the numbers may be different).8182For example, if you try to run \verb|plot()| with no function to graph, you will see8384``\verb|TypeError: plot() takes at least 1 argument (0 given).|''8586If you try \verb|solve(x^2==4)|, then you will see8788``\verb|TypeError: solve() takes at least 1 positional argument (0 given).|''8990This is because you forgot to specify the variable to solve for. You should have typed \verb|solve(x^2==4,x)|.9192\subsection{TypeError: unable to simplify to float approximation}9394There may be various ways for this error to arise.9596For example, if you try \verb|find_root(x^2==4,x,0,5)|, you will get this error. The problem is the ``x'' -- the correct syntax would be \verb|find_root(x^2==4,0,5)| (note: you need the extra ``x'' when using the \verb|solve| command, since \verb|solve| can handle equations with more than one variable).9798\subsection{TypeError: invalid integration algorithm}99100You may see this error when using the \verb|numerical_integral| command. For example, $\ds\int_1^2\frac{1}{x}\,dx\approx$ \verb|numerical_integral(1/x,1,2)|. Notice that the regular integral command likes to have the variable of integration specified: \verb|integral(1/x,x,1,2)|. The ``x'' specifies the variable. However, \verb|numerical_integral| does not need the variable specified, since there can be only one variable involved (so the answer will be a number). If you try to specify the variable anyway, e.g., \verb|numerical_integral(1/x,x,1,2)|, then you will get this \verb|TypeError|.101102\subsection{NameError: name '\underline{\hspace{.25in}}' is not defined}103104There are a few different situations in which a ``NameError'' may arise.105106\begin{itemize}107108\item Failing to Declare a Variable109110The variable $x$ is automatically declared, but any other variable must be explicitly declared using \verb|%var|.111112For example, you may want to find $\frac{d}{dx} ax^2+bx+c$, so you type113114\verb|derivative(a*x^2+b*x+c,x)|.115116When you run this, you will get ``NameError: name 'a' is not defined.''117118Instead, you need to do this:119\begin{verbatim}120%var a,b,c121derivative(a*x^2+b*x+c,x)122\end{verbatim}123124Note: There are certain contexts that do not require a variable declaration. For example, if you type \verb|y=10|, you do not have to declare $y$. In this case, $y$ is not really a variable, it's just another name for $10$.125126\item Misspelling a Command Name127128This error can also result from a simple typo. For example, if you try \verb|f(x)=son(x)| instead of \verb|f(x)=sin(x)| you will get ``NameError: name 'son' is not defined.''129130\item Forgetting Quote Marks131132Some options, such as the plot options \verb|color| and \verb|linestyle|, will require quote marks.133134For example, if you want to change the plot color to black, you can add \verb|color='black'| inside the plot command. If you leave off the quote marks, you will get ``NameError: name 'black' is not defined.''135136\end{itemize}137138\subsection{RuntimeError: Error in line(): option '\underline{\hspace{.25in}}' not valid}139140Many commands have optional arguments. For example, you can set the plot range using the xmin and xmax options: \verb|plot(f(x),xmin=-10,xmax=10)|. If you misspell one of these options, you may get this error.141142For example, if you misspell ``xmax'' and type \verb|plot(x^2,xmin=0,xman=5)|, you will get ``RuntimeError: Error in line(): option 'xman' not valid.''143144\subsection{RuntimeError: f appears to have no zero on the interval}145146This error occurs when you try to solve an equation using the \verb|find_root| command, but the equation has no solutions in the interval you specified.147148For example, if you try \verb|find_root(cos(x)==x,-1,0)|, you will get this error. This is because the equation $\cos(x)=x$ has only one solution ($x\approx 0.74$), and this solution is not in the interval from $-1$ to $0$ that you gave to \verb|find_root|.149150\subsection{RuntimeError: ECL says: Error executing code in Maxima}151152Sage uses another program called Maxima to perform many mathematical calculations; however, there are some calculations that Maxima is not able to do, which can result in this error.153154For example, if you try to compute $\ds\int\sqrt{1+\sin(x)^2}\,dx$ by typing155156\verb|integral(sqrt(1+sin(x)^2),x)|, you will get this error, since Maxima is not able to evaluate this antiderivative. If you get this error from a definite integral, then you can use the \verb|numerical_integral| command to get a decimal approximation of the answer.157158\subsection{ValueError: Assumption is inconsistent}159160This error arises when you set assumptions about variables (we do this when dealing with improper integrals). If your assumptions are contradictory, you will get this error. For example, if you try: \verb|assume(x<-1); assume(x>1)|, you will get this \verb|ValueError|.161162A common way to get this error is failing to ``forget'' previous assumptions. You may use \verb|assume(x<-1)| for one problem, and then when you move on to a different problem you may use \verb|assume(x>1)|. After you finish the first problem, you must run the command \verb|forget()|, which will clear the assumptions.163164\subsection{ValueError: Assumption is redundant}165166This is similar to the last error, except you have two assumptions that are redundant rather than contradictory. For example, if you try: \verb|assume(x>5); assume(x>1)|, you will get this \verb|ValueError|. This can also result from failing to use \verb|forget()|.167168\textbf{You must remember to forget()!}169170\subsection{ValueError: Integral is divergent}171172Mathematically, this is really no error at all. This error may result when you try to calculate a divergent improper integral.173174For example, try to find $\ds\int_0^1\frac{1}{x}\, dx$ by typing: \verb|integral(1/x,x,0,1)|.175176\subsection{ValueError: Sum is divergent}177178This is similar to the last error, but this message results when you try to calculate the sum of a divergent infinite series.179180For example, try to find $\ds\sum_{n=1}^{\infty}\frac{1}{n}$ by typing: \verb|sum(1/n,n,1,+Infinity)|181182(note: you must have \verb|%var n| before this line).183184Some divergent series will not give you this error. Here are some examples:185186\hspace{.25in}\verb|sum(n*(-2)^n,n,1,+Infinity)| will output \verb|Infinity|.187188\hspace{.25in}\verb|sum(n*(-1)^n,n,1,+Infinity)| will output \verb|und|.189190\hspace{.25in}\verb|sum(n*2^n,n,1,+Infinity)| will output \verb|+Infinity|.191192\subsection{DeprecationWarning}193194This is not an error, but it will result in a block of red in the output. When you get a deprecation warning, Sage will output the answer you expect. However, a future version of Sage may eliminate the syntax you used to get that output.195196For example, if you type \verb|derivative(x^2,x)(3)|, then you expect to get $\ds\left.\frac{d}{dx}x^2\right|_{x=3}=6$. You do indeed get 6, but you also get this warning message:197198``DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)''199200Sage is telling you to do this: \verb|derivative(x^2,x)(x=3)|. Sage wants you to explicitly indicate that $x=3$. Right now, this is optional (although preferred). In a future version of Sage, $x=3$ may be necessary.201202\subsection{verbose 0 (3757: plot.py, generate\_plot\_points) WARNING: When plotting, failed to evaluate function at 100 points.}203204This message appears in black rather than red. You will get this warning before a graph if your plot window includes input values not in the domain of the function you are plotting.205206For example, if you try \verb|plot(ln(x))|, then the plot will use $-1\le x \le 1$ by default. However, logarithms are not defined for $x\le 0$ (if you want the output to be a real number). Sage will produce a graph (with $0\le x \le 1$), but it will warn you that it has ignored $-1 \le x \le 0$.207208\section{Other Common Mistakes}209210Some mistakes commonly made when using Sage \textit{do not} result in an error message, but you don't get the right answer. You must be very careful about these!211212\subsection{Missing Explicit Multiplication}213214Sometimes missing multiplications will result in an error message (see above). However, here is an example that does not: To define the function $f(x)=(x-3)(x+2)$, you must type \verb|f(x)=(x-3)*(x+2)|, with an explicit multiplication between the two factors. If you leave off this multiplication, you will \emph{not} get an error message, but you \emph{will} get the wrong function!215216\subsection{Missing Parentheses}217218There are many situations when parentheses are not optional.219220For example, to write the fraction $\ds\frac{x+y}{5}$ in Sage, you must type \verb|(x+y)/5|. If you leave the parentheses off, you will not get an error message, you'll just get the wrong fraction: $\ds x+y/5=x+\frac{y}{5}\ne\frac{x+y}{5}$.221222Here's an example involving powers: $\verb|e^2*x|=e^{2}\cdot x\ne e^{2x}=\verb|e^(2*x)|$.223224\subsection{Scientific Notation}225226If the output from Sage is \verb|2.801232340e9|, you may think this is a number close to 2.8. However, this is actually a very large number (about 2.8 \emph{billion}). You have to watch out for scientific notation in Sage, which is indicated by the little ``e" in the output above. This output is actually $2.801232340\times 10^{9}=2,801,232,340.$227228Here is another example: \verb|3.456400000e-12| is not close to 3.5, it is actually close to $0$:229230$3.456400000\times 10^{-12}=0.0000000000034564\approx 0$.231232\subsection{Derivatives}233234There are two kinds of derivative questions: (1) the derivative function, such as $f'(x)$, and (2) the derivative at a particular value, such as $f'(3)$.235236The derivative command in Sage answers the first question. If you want $f'(x)$, then type \verb|derivative(f,x)|.237238To answer the second question, you should use a two step process. First, assign the derivative function a name. I like to use ``df" for ``derivative of f", but you can use any name you want. Type \verb|df(x)=derivative(f,x)|.239240Now that you have the derivative function, the second step is to plug the particluar value into this. For example, \verb|df(3)| is $f'(3)$, and \verb|df(-1)| is $f'(-1)$.241242If you want the second derivative, then you add a 2 to the derivative command:243244\verb|derivative(f,x,2)| is $f''(x)$.245246Similarly, \verb|derivative(f,x,10)| is the 10th derivative, $f^{(10)}(x)$.247248A common mistake is to use \verb|derivative(f,x,2)| when you want $f'(2)$. Of course, $f''(x)$ is very different from $f'(2)$.249250251\vspace{.5in}252253Help me build this list! Tell me any other common mistakes you come across. Thanks.254255\end{document}256257