Learn Calculus with Julia: Here is a highly recommended math tutorial | Exercises + code
Xiaocha from Aofei Temple
Quantum Bit Report | Public Account QbitAI
Julia is known for its speed and simplicity, and was born for the needs of computational science. It is perfect for learning calculus, and Julia's syntax is closer to actual mathematical expressions, which is very friendly to beginners who have not learned programming language.
Recently, John Verzani, a mathematics professor at Staten Island College in New York, wrote a tutorial on calculus and Julia , which includes common calculus concepts and graphical demonstrations, making it more vivid and intuitive than textbooks. Exercises are also attached after each chapter for readers to consolidate their knowledge.
Although many schools use Mathematica, Maple and other mathematical software for teaching, the advantage of Julia is that it is completely open source and free .
Preparation
Before using the tutorial, we first install the Plots package for Julia, which is an extension package for plotting function graphs. In addition, we also need to install other packages such as the SymPy scientific computing library.
using Pkg
Pkg.add("Plots")
Pkg.add("SymPy")
Pkg.add("Roots")
Pkg.add("ForwardDiff")
Pkg.add("ImplicitEquations")
After installing the above extension packages, you can draw function graphs. We simply draw the sine function graph in the range of 0 to 2π:
using Plots
plot(sin, 0, 2pi)
Julia supports inputting special mathematical symbols. The specific method is to use a slash followed by the LaTeX name of the symbol, and then press the Tab key to output the special character. For example:
θ = 45; v₀ = 200
The way to input θ is \theta [tab], and the way to input v₀ is v\_0 [tab].
Derivative
After completing the basic teaching of Julia, the following is the basic concepts of calculus.
Let's review the definition of derivative . From the function graph, the derivative is the limit of the slope of the secant line of the function. When two points on the secant line merge into one point, it becomes a tangent line.
In fact, it is to find the following limit:
Julia has an integrated limit-finding function. For the sine function sin(x), its derivative is the limit of [sin(x+h)-sin(x)]/h when h approaches 0.
using SymPy
limit((sin(x+h) - sin(x))/ h, h, 0)
By using the above method, we can get the derivative of sin(x) at x=0 to be 1, and the function graph is:
f(x) = sin(x)
c = 0
tl(x) = f(c) + 1 * (x - c)
plot(f, -pi/2, pi/2)
plot!(tl)
Applications of Derivatives
1. Newton's method
By gradually approximating the tangent line, we can find the approximate solution of the equation.
2. Finding the limit using L’Hôpital’s rule
Written in Julia:
using SymPy
a,x = symbols("a, x", positive=true, real=true)
f(x) = sqrt(2a^3*x - x^4) - a * (a^2*x)^(1//3)
g(x) = a - (a*x^3)^(1//4)
The above expression is too complicated and is an indeterminate form of 0/0. Derivatives of the numerator f(x) and the denominator g(x) are taken separately:
fp, gp = subs(diff(f(x),x), x=>a), subs(diff(g(x),x), x=>a)
got the answer
(-4*a/3, -3/4)
So the limit value is 16a/9 .
integral
Definite integral is to find the area under the function curve:
The figure above shows the method of finding the definite integral: divide the graph below the function into several long strips. As the strips become thinner and thinner, the sum of the areas of these strips becomes closer and closer to the area enclosed under the curve.
In order to find the approximate value of the definite integral of the function f(x) = x 2 in the interval [0,1], we divide the entire area into 50,000 parts:
a, b = 0, 1
f(x) = x^2
n = 50_000
xs = a:(b-a)/n:b
deltas = diff(xs)
cs = xs[1:end-1]
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
The final result is:
0.3333233333999998
Obviously, this method is too complicated to find the definite integral, so we need to introduce the concept of indefinite integral . Indefinite integral is to find the original function f(x) when the derivative f'(x) is known.
The definite integral and the indefinite integral are related by the Newton-Leibniz formula :
Application of integral
After learning integration, the tutorial gives several practical application examples:
1. Find the length of the curve
Solving f(x)=x 2 for the arc length in the interval [0,1] is actually the integral.
First find the indefinite integral:
using SymPy
@vars x
F = integrate(sqrt(1 + (2x)^2), x)
F(1)-F(0) is the required arc length:
2. Find the volume
The way to calculate the volume is to "cut" the object into circles of Michelin, and the sum of the volumes of each circle is the total volume.
Rotate the straight line x/r+y/h=1 around the y-axis to obtain a cone with a base diameter of r and a height of h.
using SymPy
@vars r h x y
R = r*(1 - y/h)
integrate(pi*R^2, (y, 0, h))
Finally, the volume is calculated:
There are many other basic concepts in the tutorial. Due to the length of the article, we will not introduce them one by one. Friends who are interested can go to the blog to learn more.
Original address:
https://calculuswithjulia.github.io/
-over-
Recommended Activities
The 2019 Artificial Intelligence and Machine Learning Innovation Summit aims to discover innovative projects and outstanding teams in the fields of artificial intelligence, big data, and Internet architecture in the global Internet field, integrate international best technical practices, build an industry case study think tank, and help companies transform and upgrade in the AI era. Click the above image for details
Join the community
The QuantumBit AI community has started recruiting. The QuantumBit community is divided into: AI discussion group, AI+ industry group, and AI technology group;
Students who are interested in AI are welcome to reply to the keyword "WeChat group" in the dialogue interface of the Quantum Bit public account (QbitAI) to obtain the group entry method. (The technical group and AI+ industry group need to be reviewed and the review is strict, please understand)
Quantum Bit QbitAI · Toutiao signed author
Tracking new trends in AI technology and products
If you like it, click "Like"!