CoCalc Public Files02 - Graphing and Solving Equations Assignment / Graphing and Solving Equations Notes.sagewsOpen with one click!

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 $f(x)=x^2$.

By default, the plot command creates a graph with $-1\le x \le 1$. 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 $f(x)=x^2$ 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 $f(x)=x^2$ for $-10 \le x \le 10$.

Graph $f(x)=x^2$ for $-5\le x \le 20$.

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 $f(x)=x^2$ for $-10\le x \le 10$ and $-10 \le y \le 50$.

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

Graph $f(t)=2t+4$ for $5 \le t \le 10$.

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 $f(x)=x^2$ and $g(x)=(x-2)^2-3$.

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

Let's change the window so that $-5 \le x \le 5$.

I changed the window for $f$, but $g$ is still the default $-1\le x\le1$. 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 $g$ to red so we can tell the two graphs apart. You do this by adding color='red' to the $g$ plot. Notice the quotes around red.

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

Let's add a graph of $h(x)=x^3$ to get a graph of three functions.

Note: I have adjusted the ymin and ymax in the plot of $h$ above; otherwise, $h$ 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 $g$ 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 $x^2-10=2x-x^2$ for $x$.

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 $x=3$ and $x=-2$. Let's zoom in around $x=3$.

It looks like the curves cross near the point $(2.8,-2)$, 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 $x^2-10=2x-x^2$ 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 $f(x)=g(x)$ is equivalent to solving $f(x)-g(x)=0$.

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 $x^2-10=2x-x^2$ using the solve command.

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

[$\displaystyle x = -\frac{1}{2} \, \sqrt{21} + \frac{1}{2}$, $\displaystyle x = \frac{1}{2} \, \sqrt{21} + \frac{1}{2}$]

There are two solutions, $-\frac{1}{2}\sqrt{21}+\frac{1}{2}$ and $\frac{1}{2}\sqrt{21}+\frac{1}{2}$.

Let's convert to decimals.

2.79128784747792
-1.79128784747792

Solve for $x$: $\ 3x^2+2x-4=-x^3+5x^2-8x+7$.

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

$x = -\frac{1}{2} \, {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}} {\left(i \, \sqrt{3} + 1\right)} + \frac{-13 i \, \sqrt{3} + 13}{9 \, {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}}} + \frac{2}{3}$

$x = -\frac{1}{2} \, {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}} {\left(-i \, \sqrt{3} + 1\right)} - \frac{-13 i \, \sqrt{3} - 13}{9 \, {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}}} + \frac{2}{3}$

$x = {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}} - \frac{26}{9 \, {\left(\frac{1}{18} \, \sqrt{3259} \sqrt{3} + \frac{133}{54}\right)}^{\frac{1}{3}}} + \frac{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 $x\approx1.2$.

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 $x$: $\ \cos(x)=x$.

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 $\cos(x)=x$ on the interval $[-1,1]$.

Notice that find_root returns a numerical approximation, while solve returns an exact answer.

Solve for $x$: $\ e^x-1.1=\sin(x)$.

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 $-10$ to $10$ 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 $x=-2$, $x=-0.5$, and $x=0.5$). 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 $xy+2=2x-3y$ for $y$.

Don't forget to declare your variables.

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

[$\displaystyle y = \frac{2 \, {\left(x - 1\right)}}{x + 3}$]

Now let's solve the same equation for $x$.

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

[$\displaystyle x = -\frac{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$: $\ ax^2+bx+c=0$.

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

[$\displaystyle x = -\frac{b + \sqrt{b^{2} - 4 \, a c}}{2 \, a}$, $\displaystyle x = -\frac{b - \sqrt{b^{2} - 4 \, a c}}{2 \, a}$]

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

Solve $x^2-8>3$.

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

[[$\displaystyle x < -\sqrt{11}$], [$\displaystyle x > \sqrt{11}$]]

In interval notation, the solution is $(-\infty,-\sqrt{11})\cup(\sqrt{11},\infty)$.

Here is a graph:

Solve $x^3-5x\le 2$.

[Note: use "<=" for $\le$]

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

[[$\displaystyle x \leq \left(-2\right)$], [$\displaystyle x \geq -\sqrt{2} + 1$, $\displaystyle x \leq \sqrt{2} + 1$]]

In interval notation, the solution is $(-\infty,-2]\cup[-\sqrt{2}+1,\sqrt{2}+1]$.

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 $(-\infty,-2]\cup[-\sqrt{2}+1,\infty)\cup(-\infty,\sqrt{2}+1]$, which does not make sense anyway, because $[-\sqrt{2}+1,\infty)\cup(-\infty,\sqrt{2}+1]=\mathbb{R}$