Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
License: OTHER
Image: ubuntu2004
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Prerequisites:
Intro to Sage
Graphing Functions and Solving Equations in Sage
In this lesson, we will first learn how to graph functions, and then we will talk about different methods of solving equations in Sage.
Basic Graphing
You can graph a function in Sage using the "plot" command.
Example 1
Graph the function .
By default, the plot command creates a graph with . The range of values on the y-axis is chosen by Sage to fit the graph to the window.
You may also define the function first, and then plot using the function name.
Changing the Plot Window - Horizontal Axis
To change the input values (horizontal axis), use the xmin and xmax options. Plot options are separated by commas.
Example 2
Graph for .
Example 3
Graph for .
Changing the Plot Window - Vertical Axis
Unless you specify the range on the y-axis, Sage will choose it for you.
To change the output values (vertical axis), use the ymin and ymax options.
Example 4
Graph for and .
Example 5
Here's another window. Notice that I did not specify xmin, so it defaults to .
Example 6
Graph for .
Note: Use the plot options xmin and xmax even if your input variable is something other than x.
Graphing More Than One Function
To graph multiple functions you add together multiple plots (using +).
Example 7
Graph and .
(Note: You can get the graph of by shifting the graph of two units right and three units down.)
Example 8
Let's change the window so that .
I changed the window for , but is still the default . I have to specify the window for each plot.
Changing Colors
We can change the color of the curves using the "color" option within a plot.
Example 9
Let's change the graph of to red so we can tell the two graphs apart. You do this by adding color='red' to the plot. Notice the quotes around red.
Example 10
Sage knows quite a few colors by name. Try some yourself.
Example 11
Let's add a graph of to get a graph of three functions.
Note: I have adjusted the ymin and ymax in the plot of above; otherwise, would dominate the graph.
Changing the Line Style
You can also change the style of line using the "linestyle" option. The default is "solid," but you can also used dashed or dotted lines or a combination of the two.
Example 12
Let's make the graph of using a dashed line by typing linestyle='dashed'
Example 13
To get a dotted line, type linestyle='dotted'
Example 14
To get a dash-dot pattern, use linestyle='dashdot'
Solving Equations
There are several different ways to solve equations in Sage, including:
Approximate solutions graphically
Use the solve command
Use the find_root command
We'll start with a graphical approach.
Example 15
Solve the equation for .
We'll graph both sides of this equation and see where the two curves cross.
On the default plot, we don't see any points of intersection, so let's zoom out.
Now we see two points of intersection, around and . Let's zoom in around .
It looks like the curves cross near the point , so let's zoom in some more:
Depending on how accurate we want to be, we could keep doing this over and over again, but this is a tedious process.
Example 16
It is possible to plot an equation in Sage. This produces a plot of the difference of the two sides.
Note: When you write an equation in Sage, you have to use two equal signs, since a single equal sign is for assignment.
Solve by graphing this equation.
If we do the plot this way, then the solutions to our equation are the roots (zeros) of the graph. In other words, solving is equivalent to solving .
Using the solve Command
Sage has a command called "solve" that can solve many (but not all) equations. This command takes two arguments: the equation to solve and the variable to solve for.
Don't forget two equal signs when you type an equation.
Example 17
Solve using the solve command.
There are two solutions, and .
Let's convert to decimals.
Example 18
Solve for : .
Sage found three solutions, but two are complex; there is only one real solution.
Here is a graph of the two sides of this equation showing one real solution near .
Using the find_root Command
There are some equations that Sage cannot solve exactly using the solve command. In this case, we can approximate the solutions using the find_root command.
Example 19
Solve for : .
First, we will try the solve command.
Sage returns the original equation again; it was unable to find exact answers. However, we can find a numerial approximation using the find_root command.
The find_root command requires that we specify an interval to search for a solution. It will return the first solution it finds, so we have to make sure that our interval contains only one solution. One way to do this is by graphing first.
Here we have only one solution, near 0.75.
[Note: How do we know there are no more solutions outside this viewing window?]
The find_root command takes three arguments: an equation to solve, a lower bound, and an upper bound. It will search for solutions between the lower and upper bounds, and it will return the first solution it finds.
This command looks for solutions to the equation on the interval .
Notice that find_root returns a numerical approximation, while solve returns an exact answer.
Example 20
Solve for : .
First, we'll try the solve command.
The solve command is not able to solve this equation (notice that there are still x's on both sides of the output), so we'll have to use find_root instead.
What interval should we give find_root? Let's try the interval from to just to see what happens:
The find_root command returns only one solution. It gives us no clue whether or not there are more solutions in this interval. To use find_root effectively, we need to know how many solutions there are and their approximate location. So we'll look at a graph. It may take some trial and error to get a good window.
From the graph, we can see three solutions (near , , and ). We'll use this to determine the intervals to give find_root.
[Note: How do we know there are no more solutions outside this viewing window?]
Equations Involving Multiple Variables
You can also use the solve command to solve for one variable in an equation involving multiple variables. The answer will be an expression involving the other variables.
Note: find_root does not work with multiple variables, since the answer must be a number to use find_root.
Example 21
Solve for .
Don't forget to declare your variables.
Now let's solve the same equation for .
Example 22
If you forget the Quadratic Formula, then Sage will remind you (notice that you can leave off ==0).
Solve for : .
Solving Inequalities
You can also use Sage to solve inequalities, although we won't be needing this feature as much.
Example 23
Solve .
In interval notation, the solution is .
Here is a graph:
Example 24
Solve .
[Note: use "<=" for ]
In interval notation, the solution is .
A picture would be nice to help us interpret the output, so here's a graph:
You have to be careful reading this solution. Notice that [[x <= -2], [x >= -sqrt(2) + 1, x <= sqrt(2) + 1]] is not the same as [[x <= -2], [x >= -sqrt(2) + 1], [x <= sqrt(2) + 1]].
The latter is , which does not make sense anyway, because