0% found this document useful (0 votes)
34 views12 pages

SAGE Math Syntax Guide

1. The document provides instructions for using various mathematical functions and operations in SageMath such as finding the factorial of a number, determining if a number is prime, plotting equations, solving systems of equations using matrices, curve fitting, and calculating integrals. 2. Examples are given for plotting single and multiple equations, implicit and parametric plots, vector fields, 3D surfaces, and calculating limits, derivatives, integrals, and solving differential equations. 3. Instructions are also provided for linear algebra operations like finding the determinant, inverse and eigenvalues of a matrix as well as verifying Cayley-Hamilton theorem.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

SAGE Math Syntax Guide

1. The document provides instructions for using various mathematical functions and operations in SageMath such as finding the factorial of a number, determining if a number is prime, plotting equations, solving systems of equations using matrices, curve fitting, and calculating integrals. 2. Examples are given for plotting single and multiple equations, implicit and parametric plots, vector fields, 3D surfaces, and calculating limits, derivatives, integrals, and solving differential equations. 3. Instructions are also provided for linear algebra operations like finding the determinant, inverse and eigenvalues of a matrix as well as verifying Cayley-Hamilton theorem.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

SAGE MATHS AS CALCULATOR

[Link] finding factorial of a number

Factorial (number)

[Link] find how many digits are there in a number

A=number

[Link]()

[Link] find if a number is prime or not

Number.is_prime()

If it will be prime the output will be true if it will not be prime the output will be false

[Link] of prime numbers in a given interval

List(prime(1,50))

[Link] to find gcd and LCM to of two numbers

Gcd(num1, num2)

Lcm(num1,num2)

[Link] find value in numeric/decimal form of any trigonometric function

Sin(45).n()

[Link] show anything in mathematical form for example pi


Show(pi)

[Link] to differentiate a given equation

Var('given variables in the equation')

f=given equation

• Show(diff(d, x or y)

• Show(diff(equation, x or y)

• Show(diff(equation, how many times we have to differentiate)

[Link] to integrate a given function

Show(integrate(equation, x or y))

[Link] to integrate if limits is given

Show(integrate(equation, x or y, the given limits))

[Link] to solve a quadratic equation

var('a,b,c')

def quad(a,b,c): x1=(-b+sqrt(b**2-4*a*c))/(2*a)

x2=(-b-sqrt(b**2-4*a*c))/(2*a)

return(x1,x2)

Then we'll take a quadratic equation will take out a b and c from that equation and we will use the
quad (a,b,c)

[Link] to solve linear equations with two variables


Var('the given variables')

Show(solve([equation 1, equation 2], given 2 variables))

2 [Link]

1. Syntax of plotting given equation

Var('given variables')

f=given equation

[Link](at which point we have to plot the curve of it)

2. Other the things that we can use while plotting a curve

plot(sin(x),0,2*pi,color='red', figsize=3,title="Sinx", thickness=3, legend_label='sinx')

Where can change colour, figure size, give title or legend lable to the curve, can change the
thickness, can even edit the axes also

[Link] if we want to plot 3 or 4 curves together

P1=plot(equ, points, color, figsize)

P2=plot(equ, points, color, figsize)

P3=plot(equ, points, color, figsize)

P4=plot(equ, points, color, figsize)

Show(p1+p2+p3+p4)

We will use different colours to show the plot of different curves properly.

4. Scatter plot syntax

Data=[data given]
Scatter_plot(data)

5. Implicit plot syntax

var('given variables')

f(given variables)=equation

implicit_plot(f, points for which you have to plot the equation, color, figsize, etc etc)

6. Parametric plot syntax

Var('t')

Parametric_plot([equation 1, equation 2], (t, points for which we have to plot the curve))

7. Polar plots

Var('t')

Polar_plot([equation 1, equation 2], (t, points for which we have to plot the curve))

8. Vector plot

A=vector ([points for which we have to plot])

Plot(A, figsize)

9. 3D plotting

Var('given variables')

f=given equation

plot3d(f(given variables), x and y points at which we have to plot the 3d diagram)

10. If Implicit equation is given for 3D plot


Implicit_plot3d()

11. Surface of revolution

Revolution_3d(function, (x, xmin, max)

[Link] WITH SAGEMATHS

1. How to find limit

Limit(equation, x=given limit)

2. How to find derivative

Var('given variables')

F=given equation

Show([Link](for how many times we have to derive it))

3. Syntax for derivation and points are also given

Var('given variables')

F=given equation

Show(diff(f,x)(given points))

4. To find implicit derivative

var('given variables')

f(x,y)=given equation

show(f)

show('dy/dx=',-diff(f,x)/diff(f,y))
5. Local maxima and minima

F.find_local_maxima()

F.find_local_minima()

6. How to find the intervals where function is increasing or decreasing

First we will plot the the curve of the given equation

Var()

F=equation

Pf=plot(f, points for which we've to plot the curve)

Show(pf)

Then we will differentiate the given equation and will plot both the curve in the same graph

D=diff(f,x)

Show(d)

Pd=plot(D,points for which we've to plot the curve)

Show(pd+pf, figsize)

Then by observing the graph we can write the intervals of increasing and decreasing function

7. Syntax for critical points and point of inflation

cpts=solve(d2f==0, x, solution_dict=true)

cpts

infl=solve(d2f==0,x,solution_dict=true)
infl

We can take out the derivatives of an equation at different orders and we can plot those all curves in
a single graph to determine concave up and concave down points

[Link] OF LINEAR ALGEBRA

1. Defining a matrix

Show(matrix(rows, columns [elements]))

Show(matrix(ZZ, rows, columns [elements]))

ZZ shows integer field defines all elements in matrix as a integer

RR (Real Field) defines all entries in matrix is a real number

CDF (Complex Dence Field) defines all entries in matrix is a complex number

QQ (Rational Field) defines all entries in matrix is a rational number

2. Matrix operations

Define matrix P

Define matrix Q

For addition and subtraction

show('P+Q =',P+Q , 'P-Q =', P-Q)

For transpose

show('P Q = ',P*[Link]())
3. Syntax for finding determinant transpose a inverse adjoint of a matrix

Define matrix R

[Link]()

[Link]()

[Link]()

[Link]()

[Link]()

3*R (this is for scalar multiplication)

4. How to solve a system of equation with the use of matrix

Define variables

Show(solve([equation 1, equation 2, equation 3] variables))

A=matrix([equation points])

B=vector([])

show('A= ', A , 'B= ', [Link]())

C=[Link](B)

Show('C=',C)

rank(A)==rank(C)

5. How to check if the vectors are linearly dependent or not

Define matrix U

Show matrix()
[Link]().echelon_form()

[Link] values and eigen vectors using characteristic equation

A=matrix()

p(x)=A.characteristic_polynomial(x)

show('p(x)=', p(x))

P(x).roots()

E=[Link]()

Show(E)

V=A.eigenvectors_right()

Show('V=', V)

Sum(E)==[Link]()

Product(E)==det(A)

Det(A)

7. Syntax for diagonal matrix

D,M=A.eigenmatrix_right()

Show('D=',D, 'M=',M)

8. How to verify cayley Hamilton theorem


A=matrix()

p(x)=A.characteristic_polynomial(x)

show('p(x)=', p(x))

Coff=[Link]()

Show(coff)

show('p(A)=',sum ([ Coff[k]* A ^(k) for k in range () ]))

[Link] FITTING

1. Syntax for finding the best fir curve for given data

data=[(points given)]

point(data)

Then we'll define variables and we'll write equation

f1=find_fit(data)

f1

model1=[Link](f)

model1

point(data) + plot(model1)

2. Comparing two fittings

var('given variables')

y(x) = given polynomial equation


z(x)= given polynomial equation

show(y)

show(z)

f1=find_fit(data,y,solution_dict=True)

f2=find_fit(data,z,solution_dict=True)

show(f1,f2)

model1=[Link](f1)

model2=[Link](f2)

show(model1)

show(model2)

Point(data) + plot(model1) + plot(model1)

8 INTEGRAL CALCULUS

1. Integrating a function

f(x)=given function

Show(integrate (f(x),x))

2. Integration if limit is given

show(integral(f(x), x, lower limit, upper limit))

3. How to find area enclosed between two curves

f(x)=given equation

g(x)=given equation
plot(f(x), points where the curve is to be plotted + plot(g(x), points where the curve is to be plotted)

S = solve(f(x) == g(x),x,solution_dict=True)

a,b = S[0][x], S[1][x]

a,b

p1= plot(f(x), (x,a,b), fill = g(x), fillcolor=)

p2=plot(g(x), (x,a,b), color=)

show(p1+p2)

Area = integral((f(x) - g(x)),x,a,b)

show(Area)

show(Area.n())

4. Syntax for finding Arc length

f(x)

Plot(f(x))

Show(integral(sqrt(1+derivative (f,x)^2), given points).n()

Common questions

Powered by AI

To plot multiple curves in SageMath, define each curve with `P1=plot(equ1, range, color='color1')`, `P2=plot(equ2, range, color='color2')`, etc. Use `Show(p1+p2+...+pn)` to plot them together. Customize appearance by setting attributes like `color`, `figsize`, `title`, and `legend_label` for clear differentiation .

To determine if a number is prime using SageMath, use the method `number.is_prime()`. If the number is prime, the output will be `True`; otherwise, it will be `False` .

To solve a quadratic equation in SageMath, define the quadratic equation coefficients using `var('a,b,c')`. The function `quad(a,b,c)` computes roots via the quadratic formula: `x1=(-b+sqrt(b^2-4*a*c))/(2*a)` and `x2=(-b-sqrt(b^2-4*a*c))/(2*a)`, giving the solution as `(x1, x2)` .

First, define the variable using `var('given variables')` and the polynomial equations `y(x)` and `z(x)`. Use `f1=find_fit(data,y,solution_dict=True)` and `f2=find_fit(data,z,solution_dict=True)` to find the best fit for the data. Substitute the solutions in the equations using `model1=y.subs(f1)` and `model2=z.subs(f2)`. Finally, use `Point(data) + plot(model1) + plot(model2)` to visually compare the fitted models on the same graph .

In SageMath, find where two curves intersect by solving `S = solve(f(x) == g(x),x)`. Use these intersection points as limits for integration. Plot each curve using `p1= plot(f(x), (x,a,b))` and `p2=plot(g(x), (x,a,b))`. The area is calculated using `Area = integral((f(x) - g(x)), x, a, b)` and visualized with coloring or shading in `p1` and `p2` plots .

To find the local maxima and minima of a function in SageMath, first define the function using `F=given equation`. To find the local maxima, use `F.find_local_maxima()` and for local minima, use `F.find_local_minima()` .

To find intervals where a function is increasing or decreasing, first plot the function using `plot(f, points)`. Differentiate it with `diff(f,x)` and plot this derivative to the same graph. By observing the slopes in the derivative plot, you can determine intervals of increasing (positive slope) and decreasing (negative slope).

To verify the Cayley-Hamilton theorem in SageMath, obtain the characteristic polynomial `p(x)` of matrix `A` using `p(x)=A.characteristic_polynomial(x)`. Extract coefficients with `Coff=p.coefficients()`. To verify the theorem, compute `p(A)` as `sum([ Coff[k]*A^(k) for k in range()])` and verify it equals zero, confirming `A` satisfies its polynomial .

Define a matrix `A` and find its characteristic polynomial `p(x)=A.characteristic_polynomial(x)`. Roots of `p(x)` yield eigenvalues using `P(x).roots()`. Compute eigenvectors using `A.eigenvectors_right()`. Verify the trace as the sum of eigenvalues `Sum(E)==A.trace()` and determinant as their product `Product(E)==det(A)` .

In SageMath, define the system of equations and solve them by expressing the equations in matrix form. Use `solve([equation 1, equation 2], variables)` to find the solution. Matrix `A` is formed by coefficient matrices and vector `B` by constants. Augment the matrix using `C=A.augment(B)`, checking `rank(A)==rank(C)` ensures consistency for solution .

You might also like