Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook assignments/due09-15-16/Introductory Jupyter Notebook.ipynb

228 views
Kernel: SageMath 7.3

Math 231 Writing Assignment 1: Introduction to jupyter and SageMath

The first part of this worksheet contains examples. For Markdown (including Heading cells like the one above), you can double click the cell to see the text that generates the cell, then execute the cell again (by clicking the "run cell" button above) to get back to the output. The second part of this worksheet contains some exercises for you to do.

Examples

In a jupyter notebook, you can enter input and get output. Executing this block will typeset whatever you have typed here.

You can use Markdown mode from the dropdown list in the toolbar above to typeset text. You can also input and typeset mathematical formulas. You enclose them in dollar signs: for example, f(x)=x2f(x) = x^2.

The advantage here is that you can make mathematical formulas fairly complicated, like c=limx0+(x1)(x2)xc = \lim_{x\rightarrow 0^+} \frac{(x-1)(x-2)}{x} or like 02x3sin(x4)dx\int_0^2 x^3\sin(x^4)dx.

Technically, what is in the dollar signs is LaTeX code. Most of the commands begin with a backslash. To see the formulas above better, we can add the displaystyle command: c=limx0+(x1)(x2)x\displaystyle c = \lim_{x\rightarrow 0^+} \frac{(x-1)(x-2)}{x} and 02x3sin(x4)dx\displaystyle \int_0^2 x^3\sin(x^4)dx.

The most common LaTeX mathmatical expressions are summarized here: http://web.ift.uib.no/Teori/KURS/WRK/TeX/symALL.html. You won't need very many of those for this course.

Double click a region to change it from code back to an editable region, then execute the code again when you are finished.

This jupyter notebook has a Kernel running SageMath, which is a computer algebra system. You can do mathematical computations, graphing, and elementary programming. The cells below give some examples of this. Jupyter notebooks can be set up with a variety of kernels, including Python, Octave -- which is kind of like Matlab -- and R (a statistical software package). We will probably only use the Sagemath kernel in this coruse.

The following code prints the values of f(x)=x2f(x)=x^2 for x=0,1,2,,9x = 0, 1, 2, \ldots, 9. Note: As in a language like Python, range(0,10) gives the whole numbers from 0 to 9. If you want to include 10, you have to use range(0,11).

for x in range(0,10): print(x,x^2)
(0, 0) (1, 1) (2, 4) (3, 9) (4, 16) (5, 25) (6, 36) (7, 49) (8, 64) (9, 81)

A jupyter notebook can display graphs. Since xx was given a value above, we will start by declaring xx as a variable again.

x = var('x') plot(x^2, (x,0,5),figsize=3)
Image in a Jupyter notebook

You can also plot curves implicitly. The following command will plot the curve x3+y2=2xx^3+y^2 = 2x.

equation = x^3 + y ^2 == 2*x implicit_plot(equation, (x,-3, 3), (y,-3, 3),figsize=3)
Image in a Jupyter notebook

Here is a more complicated example of plotting. The goal is to plot f(x)=x2f(x) = x^2, g(x)=2x2g(x) = 2x^2, and h(x)=3x2h(x) = 3x^2 in different colors on the same set of axes for 2x2-2\leq x \leq 2. We will also save this down to a file called our-graph.png. Outputting the result to a .png file is especially helpful if you need to include the graph in a written document or web submission. We will define each of the three plots to be equal to a symbol (a, b, or c), then make d the "sum" of the three plots. We can then perform methods on d (i.e. we can show or save the plot).

a = plot(x^2,(x,-2,2),color='red') b = plot(2*x^2,(x,-2,2),color='green') c = plot(3*x^2,(x,-2,2),color='blue') d = a + b + c d.show(figsize=3) d.save('our-graph.png')
Image in a Jupyter notebook

SageMath can also do basic computations such as evaluating, differentiating, and integrating expressions. More basic SageMath commands are given in Appendix B of the textbook. (Anything in side a block of code that begins with a "hashtag" symbol is a comment and is ignored when the cell is executed.)

x = var('x') g = (x+2)*(x^2-9) print(g) #This just prints g. print(g.expand()) #This multiplies g out. print(g.factor()) #This factors g. print(g.substitute(x=1)) #This substitutes x=1 into g. print(g.integrate(x)) #This integrates g with respect to x. Note that this command gives 0 as the "constant of integration." print(g.differentiate(x)) #This differentiates g with respect to x. #Let's also plot g and its derivative together: a = plot(g,(x,-4,4)) b = plot(g.differentiate(x),(x,-4,4)) c = a+b c.show(figsize=3)
(x^2 - 9)*(x + 2) x^3 + 2*x^2 - 9*x - 18 (x + 3)*(x + 2)*(x - 3) -24 1/4*x^4 + 2/3*x^3 - 9/2*x^2 - 18*x 2*(x + 2)*x + x^2 - 9
Image in a Jupyter notebook

Exercises

11. Define a polynomial function y=f(x)y=f(x) that has exactly four xx-intercepts.

In the Markdown text provided below (the box that says "Answer 1"), write a paragraph in complete sentences explaining what your function is, what its xx-intercepts are, and how you know. In the paragraph, give your polynomial in the Markdown text in both expanded and factored forms.

In the Code box provided, define your function in SageMath, and plot your function and its first two derivatives on the same set of axes. Be sure that all four xx-intercepts show on your graph, and use a different color for each curve.

Answer 1.this function can have 4 intercepts at 1,2,3,4

x=var('x') a=plot((x-1)*(x-2)*(x-3)*(x-4),(x,0,5)) a.show(figsize=3)
Image in a Jupyter notebook

22. Suppose you start with 3 cents on Day 1, and the total amount of money that you have doubles each day. (So, on Day 2, you have 6 cents, and on Day 3, you have 12 cents.)

In the Markdown text provided below (the box that says "Answer 2"), write a paragraph in complete sentences explaining how to find a formula for c(n)c(n), the number of cents you have on Day nn. Then, find the day on which you pass $1,000,000.

In the Code box provided, enter code to generate the amount of money you have on Day nn for 1n251\leq n\leq 25. Have SageMath print out pairs of the form (n,c(n))(n,c(n)).

Answer 2.3*2^(x-1)) it start at 3 at the first day and double evrery other day so it should be 2^(x-1) since it strat with 3

x=var('x') a=plot(3*2^(x-1),(x,3,6)) a.show(figsize=3)
Image in a Jupyter notebook

33. Come up with an equation in xx and yy defining a curve in the xyxy-plane that cannot be solved algebraically for xx or yy. Plot the curve over a range of values that shows the curve's interesting features. .

Answer 3.

x,y=var("x,y") equation = 3^x + y ^2 == 2*x implicit_plot(equation, (x,-3, 3), (y,-3, 3),figsize=3)
Image in a Jupyter notebook