Bard Math 212 - Basic examples

660 days ago by ethan

Basic examples of Sage

Multiplying Two Numbers

\displaystyle 24536*5768

24536*5768 
       
141523648
141523648

Evaluating Sine

\displaystyle \sin \frac {\pi}2

sin(pi/2) 
       
1
1

Evaluating Natural Logarithm

\displaystyle \ln 2

log(2) 
       
log(2)
log(2)

Finding a Numerical Approximation to Natural Logarithm

\displaystyle \ln 2

n(log(2)) 
       
0.693147180559945
0.693147180559945

Finding a Limit

\displaystyle \lim_{x \to 0} \frac {\sin x}x

limit(sin(x)/x, x=0) 
       
1
1

Evaluating a Sum

\displaystyle \sum_1^5 \sqrt{n^2 +1}

sum(sqrt(n^2 +1) for n in [1..5]) 
       
sqrt(2) + sqrt(5) + sqrt(10) + sqrt(17) + sqrt(26)
sqrt(2) + sqrt(5) + sqrt(10) + sqrt(17) + sqrt(26)

Finding a Numerical Approximation to a Sum

\displaystyle \sum_1^5 \sqrt{n^2 +1}

n(sum(sqrt(n^2 +1) for n in [1..5])) 
       
16.0346843392517
16.0346843392517

Finding the Derivative of a Function of One Variable

\displaystyle \frac d{dx} [\sin (x^2 + 6x - 2)]

diff(sin(x^2 + 6*x - 2), x) 
       
2*(x + 3)*cos(x^2 + 6*x - 2)
2*(x + 3)*cos(x^2 + 6*x - 2)

Finding the Indefinite Integral of a Function of One Variable

\displaystyle \int \ln x\, dx

integral(ln(x), x) 
       
x*log(x) - x
x*log(x) - x

Finding the Definite Integral of a Function of One Variable

\displaystyle \int_3^5 \ln x\, dx

integral(ln(x), x, 3, 5) 
       
-3*log(3) + 5*log(5) - 2
-3*log(3) + 5*log(5) - 2

Finding a Numerical Approximation to a Definite Integral of a Function of One Variable

\displaystyle \int_3^5 \ln x\, dx

n(integral(ln(x), x, 3, 5)) 
       
2.75135269616617
2.75135269616617

Plotting the Graph of a Function of One Variable

\displaystyle f(x) = 4x - 6x^{\frac 23} + 2

x = var('x') plot(4*x - 6*((x^2)^(1/3)) + 2, (x,-5,5)) 
       

Plotting the Graphs of Two Functions of One Variable

The graph of \displaystyle f(x) = x^2 is in blue.

The graph of \displaystyle g(x) = - \cos x is in red.

x = var('x') P = plot(x^2, (x,-5,5)) Q = plot(-cos(x), (x,-5,5), color='red') P + Q 
       

Plotting the Graph of a Function of Two Variables

\displaystyle f(x, y) = x^2 + \frac {y^2}9

x, y = var('x,y') plot3d(x^2 + (1/9)*y^2, (x,-4,4), (y,-6,6), aspect_ratio=1) 
       

Plotting the Graphs of Two Functions of Two Variables

The graph of \displaystyle f(x, y) = x^2 + y^2 is in blue.

The graph of \displaystyle g(x, y) = 2x + 3y is in green.

x, y = var('x,y') P = plot3d(2*x + 3*y, (x,-2,2), (y,-2,2), color='green') Q = plot3d(x^2 + y^2, (x,-2,2), (y,-2,2)) P + Q 
       

Plotting a Surface in \mathbb{R}^3

\displaystyle \frac {x^2}9 - \frac {y^2}4 + 2z = 1

x, y, z = var('x,y,z') implicit_plot3d((1/9)*x^2 - (1/4)*y^2 + 2*z==1, (x, -30, 30), (y, -30,30), (z, -30,30), aspect_ratio=1) 
       

Plotting a Function in Polar Coordinate

r = 1 + \cos\theta

polar_plot(1 + cos(x), (x, 0, 2*pi), aspect_ratio=1, color='red') 
       

Plotting a Vector-Valued Function in \mathbb{R}^3

\displaystyle r(t) = (\cos t, \sin t, t)

t = var('t') parametric_plot3d((cos(t), sin(t), t), (t,0,4*pi), color='purple', aspect_ratio=1)