Shared02 - Graphing and Solving Equations Assignment / Graphing and Solving Equations Notes.sagewsOpen in CoCalc

This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

- Intro to 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.

You can graph a function in Sage using the "plot" command.

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.

To change the input values (horizontal axis), use the xmin and xmax options. Plot options are separated by commas.

Graph for .

Graph for .

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.

Graph for and .

Here's another window. Notice that I did not specify xmin, so it defaults to .

Graph for .

Note: Use the plot options xmin and xmax even if your input variable is something other than x.

To graph multiple functions you add together multiple plots (using +).

Graph and .

(Note: You can get the graph of by shifting the graph of two units right and three units down.)

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.

We can change the color of the curves using the "color" option within a plot.

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.

Sage knows quite a few colors by name. Try some yourself.

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.

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.

Let's make the graph of using a dashed line by typing linestyle='dashed'

To get a dotted line, type linestyle='dotted'

To get a dash-dot pattern, use linestyle='dashdot'

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.

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.

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 .

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.**

Solve using the solve command.

[x == -1/2*sqrt(21) + 1/2, x == 1/2*sqrt(21) + 1/2]

[, ]

There are two solutions, and .

Let's convert to decimals.

2.79128784747792
-1.79128784747792

Solve for : .

[x == -1/2*(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3)*(I*sqrt(3) + 1) + 1/9*(-13*I*sqrt(3) + 13)/(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) + 2/3, x == -1/2*(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3)*(-I*sqrt(3) + 1) - 1/9*(-13*I*sqrt(3) - 13)/(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) + 2/3, x == (1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) - 26/9/(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) + 2/3]

Sage found three solutions, but two are complex; there is only one real solution.

0.392038325748806 - 2.98210141403215*I
0.392038325748806 + 2.98210141403215*I
1.21592334850239

Here is a graph of the two sides of this equation showing one real solution near .

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.

Solve for : .

First, we will try the solve command.

[x == cos(x)]

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.

0.7390851332151607

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.

Solve for : .

First, we'll try the solve command.

[sin(x) == e^x - 11/10]

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:

-1.891197831974637

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?]

-1.8911978319747127
-0.5513169357098279
0.395754320029599

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.

Solve for .

Don't forget to declare your variables.

[y == 2*(x - 1)/(x + 3)]

[]

Now let's solve the same equation for .

[x == -(3*y + 2)/(y - 2)]

[]

If you forget the Quadratic Formula, then Sage will remind you (notice that you can leave off ==0).

Solve for : .

[x == -1/2*(b + sqrt(b^2 - 4*a*c))/a, x == -1/2*(b - sqrt(b^2 - 4*a*c))/a]

[, ]

You can also use Sage to solve inequalities, although we won't be needing this feature as much.

Solve .

[[x < -sqrt(11)], [x > sqrt(11)]]

[[], []]

In interval notation, the solution is .

Here is a graph:

Solve .

[Note: use "<=" for ]

[[x <= -2], [x >= -sqrt(2) + 1, x <= sqrt(2) + 1]]

[[], [, ]]

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