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

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

plot(x^2)


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.

f(x)=x^2
plot(f)


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

plot(x^2,xmin=-10,xmax=10)


## Example 3

Graph for .

plot(x^2,xmin=-5,xmax=20)


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

plot(x^2,xmin=-10,xmax=10,ymin=-10,ymax=50)


## Example 5

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

plot(x^2,xmax=20,ymin=-100,ymax=100)


## Example 6

Graph for .

f(t)=2*t+4
plot(f,xmin=5, xmax=10)


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

f(x)=x^2
g(x)=(x-2)^2-3
plot(f)+plot(g)


## Example 8

Let's change the window so that .

plot(f,xmin=-5,xmax=5)+plot(g)


I changed the window for , but is still the default . I have to specify the window for each plot.

plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5)


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

f(x)=x^2
g(x)=(x-2)^2-3
plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5,color='red')


## Example 10

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

plot(f,xmin=-5,xmax=5,color='salmon')+plot(g,xmin=-5,xmax=5,color='fuchsia')


## Example 11

Let's add a graph of to get a graph of three functions.

f(x)=x^2
g(x)=(x-2)^2-3
h(x)=x^3
plot(f,xmin=-5,xmax=5,color='salmon')+plot(g,xmin=-5,xmax=5,color='fuchsia')+plot(h,xmin=-5,xmax=5,ymin=-50,ymax=50,color='teal')


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'

f(x)=x^2
g(x)=(x-2)^2-3
plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5,color='red',linestyle='dashed')


## Example 13

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

plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5,color='red',linestyle='dotted')


## Example 14

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

plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5,color='red',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

## Example 15

Solve the equation for .

We'll graph both sides of this equation and see where the two curves cross.

f(x)=x^2-10
g(x)=2*x-x^2
plot(f)+plot(g,color='red')


On the default plot, we don't see any points of intersection, so let's zoom out.

plot(f,xmin=-10,xmax=10)+plot(g,xmin=-10,xmax=10,color='red')


Now we see two points of intersection, around and . Let's zoom in around .

plot(f,xmin=2,xmax=4)+plot(g,xmin=2,xmax=4,color='red')


It looks like the curves cross near the point , so let's zoom in some more:

plot(f,xmin=2.75,xmax=2.85)+plot(g,xmin=2.75,xmax=2.85,color='red')


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.

plot(x^2-10==2*x-x^2,xmin=-5,xmax=5)


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.

solve(x^2-10==2*x-x^2,x)  #You could also type solve(f(x)==g(x),x)
show(_)

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

There are two solutions, and .

Let's convert to decimals.

N(1/2*sqrt(21) + 1/2)
N(-1/2*sqrt(21) + 1/2)

2.79128784747792 -1.79128784747792

## Example 18

Solve for : .

solve(3*x^2+2*x-4==-x^3+5*x^2-8*x+7,x)

[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]
show(_[0]);show(_[1]);show(_[2])




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

N(-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)
N(-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)
N((1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) - 26/9/(1/18*sqrt(3259)*sqrt(3) + 133/54)^(1/3) + 2/3)

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 .

plot(3*x^2+2*x-4,xmin=-7,xmax=7)+plot(-x^3+5*x^2-8*x+7,xmin=-7,xmax=7,ymin=-50,ymax=100,color='red')


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

solve(cos(x)==x,x)

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

plot(cos(x))+plot(x,color='red')


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.

find_root(cos(x)==x,-1,1)

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.

## Example 20

Solve for : .

First, we'll try the solve command.

solve(e^x-1.1==sin(x),x)

[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:

find_root(e^x-1.1==sin(x),-10,10)

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

plot(e^x-1.1,xmin=-3,xmax=2)+plot(sin(x),xmin=-3,xmax=2,ymax=1,color='red')


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

find_root(e^x-1.1==sin(x),-3,-1)
find_root(e^x-1.1==sin(x),-1,0)
find_root(e^x-1.1==sin(x),0,1)

-1.8911978319747127 -0.5513169357098279 0.395754320029599

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

%var y
solve(x*y+2==2*x-3*y,y)
show(_)

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

Now let's solve the same equation for .

solve(x*y+2==2*x-3*y,x)
show(_)

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

## Example 22

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

Solve for : .

%var a,b,c
solve(a*x^2+b*x+c,x)
show(_)

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

## Solving Inequalities

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

## Example 23

Solve .

solve(x^2-8>3,x)
show(_)

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

In interval notation, the solution is .

Here is a graph:

plot(x^2-8,xmin=-5,xmax=5)+plot(3,xmin=-5,xmax=5,color='red',linestyle='dashed')


## Example 24

Solve .

[Note: use "<=" for ]

solve(x^3-5*x<=2,x)
show(_)

[[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:

plot(x^3-5*x,xmin=-4,xmax=4,ymin=-10,ymax=10)+plot(2,xmin=-4,xmax=4,color='red',linestyle='dashed')


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