Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Blaec (CSO) Tasks and Strategy Outline.
Download

Tutorial Release 10.4 The Sage Development Team https://doc.sagemath.org/pdf/en/tutorial/sage_tutorial.pdf

Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.

This tutorial is the best way to become familiar with Sage in only a few hours. You can read it in HTML or PDF versions, or from the Sage notebook (click Help, then click Tutorial to interactively work through the tutorial from within Sage).

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.

https://creativecommons.org/licenses/by-sa/3.0/

386 views
ubuntu2204
Kernel: SageMath 10.4

SageMath Example: Solving Non-Linear Systems

The following example of using Sage to solve a system of non-linear equations was provided by Jason Grout:

Solve the System Symbolically

First, we solve the system symbolically.

Declare the variables:

var('x y p q')
(x, y, p, q)

Define the equations:

eq1 = p + q == 9 eq2 = q * y + p * x == -6 eq3 = q * y^2 + p * x^2 == 24

Solve the system of equations:

solve([eq1, eq2, eq3, p == 1], p, q, x, y)
[[p == 1, q == 8, x == -4/3*sqrt(10) - 2/3, y == 1/6*sqrt(10) - 2/3], [p == 1, q == 8, x == 4/3*sqrt(10) - 2/3, y == -1/6*sqrt(10) - 2/3]]

Numerical Approximations of the Solutions

For numerical approximations of the solutions, you can use the following approach:

Solve the system of equations:

solns = solve([eq1, eq2, eq3, p == 1], p, q, x, y, solution_dict=True)

Print the numerical solutions:

[[s[p].n(30), s[q].n(30), s[x].n(30), s[y].n(30)] for s in solns]
[[1.0000000, 8.0000000, -4.8830369, -0.13962039], [1.0000000, 8.0000000, 3.5497035, -1.1937129]]

Solving Equations Numerically

Often times, solve will not be able to find an exact solution to the equation or equations specified. When it fails, you can use find_root to find a numerical solution. For example, solve does not return anything interesting for the following equation:

Declare the variable θ\theta:

theta = var('theta')

Solve the equation cos(θ)=sin(θ)\cos(\theta) = \sin(\theta):

solve(cos(theta) == sin(theta), theta)
[sin(theta) == cos(theta)]

On the other hand, we can use find_root to find a solution to the above equation in the range 0<ϕ<π20 < \phi < \frac{\pi}{2}:

Declare the variable ϕ\phi:

phi = var('phi')

Find the numerical root:

find_root(cos(phi) == sin(phi), 0, pi/2)
/ext/sage/10.4/local/var/lib/sage/venv-python3.12.4/lib/python3.12/site-packages/scikits/__init__.py:1: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html __import__("pkg_resources").declare_namespace(__name__) /ext/sage/10.4/local/var/lib/sage/venv-python3.12.4/lib/python3.12/site-packages/scikits/__init__.py:1: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('scikits')`. Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages __import__("pkg_resources").declare_namespace(__name__)
0.7853981633974484