Ask Your Question

Display x-intercept of a plot, involving x raised to the 3rd power.

asked 2013-07-29 20:19:45 +0100

bxdin gravatar image

The following is my syntax to generate the plot:

e1= x^3 + 4*x - 7
f(x)= e1.subs(x=x)
p1= plot(e1, (x,-15,15), ymin=-15, ymax=15)
p0= p1

Now to try and generate where on the x-axis the the plot crossed, 1.255 according to the author, I tried to get sage to solve for x, but it generated irrelevant answers:

ans = (solve(x^3 + 4*x - 7 == 0, x))
print (ans)

I tried substituting the value that didn't include imaginary into the original equation: x^3 + 4*x - 7, I got 1.333 x 10^-15, which is totally different from the author's.

The original question was: given f(x) = x^3 + 4*x - 2, one needs to evaluate f^-1(-5).

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2013-07-29 22:10:17 +0100

calc314 gravatar image

Your question actually appears to be two homework questions.

1) Find the x-intercept for the given polynomial.

Your code has some extraneous lines in it. So, I'll shorten it.

f(x)=x^3 + 4*x - 7
p1= plot(f(x), (x,-15,15), ymin=-15, ymax=15)

This plot shows just one real root.

print ans

Solve gives two nonreal roots and one real root. This is consistent with the plot, and you can get the real root using:


Another option is to use find_root to find the root numerically:


Either way, you get the answer you expected: 1.25538315684475

Substituting this into f should give you 0, and what you saw was that you got 10^(-15) which is VERY close to zero. This often happens when using numerical approximations.

2) given f(x) = x^3 + 4*x - 2, one needs to evaluate f^-1(-5)

You need to use solve find x-values for which f(x)=-5.

edit flag offensive delete link more

answered 2013-07-29 23:51:50 +0100

slelievre gravatar image

You defined e1 as an expression involving x:

sage: e1 = x^3 + 4*x - 7

You can plot it directly as a function of x:

sage: plot(e1, (x,-15,15), ymin=-15, ymax=15)

You solved the expression for x (you could shorten your code):

sage: ans = solve(e1, x)
sage: print(ans)

You get three solutions, two complex conjugate and one real. Since you started from a degree three polynomial with integer coefficients, Sage was able to find the roots algebraically (with square roots and cube roots, see Roots of a cubic function).

Exact formulas are nice, but now how do we get a numerical value?

You can get hold of the last solution like this:

sage: a = ans[2].rhs()

and substitute it in your expression to check that the expression becomes zero there.

sage: e1.subs(x=a)

This gives you a messy expression, because all the cube roots and square roots need to be simplified, which Sage does not do automatically.

You could ask for a numerical value of the expression evaluated at this value.

sage: e1.subs(x=a).n()

It's very close to zero (roughly 1.33 * 10^-15) but not exactly zero, because the approximations involved in taking numerical values did not cancel out perfectly.

You could ask sage to simplify the messy algebraic expression, which in this case works.

sage: e1.subs(x=a).full_simplify()

Now let's get a numerical value of the real root.

sage: a.n()

To check again, you can substitute this numerical value into the expression and see that it is zero up to the displayed precision.

sage: e1.subs(x=a.n())

Of course, if you were only interested in the numerical value, you could have used a numerical method, as mentioned in calc314's answer. Since the graph lets you see the root is between 1 and 2, you can specify these bounds.

sage: find_root(e1,1,2)
edit flag offensive delete link more

answered 2013-07-29 20:55:37 +0100

rickhg12hs gravatar image

updated 2013-07-30 00:29:15 +0100

I'm not sure I completely understand your question, but perhaps this is _a_ way of solving for the answers you seek:

[myroot.n() for myroot,mymult in (x^3 + 4*x - 7).roots() if myroot.n().parent() == RR]

[mysol.rhs().n() for mysol in solve(x^3 + 4*x - 2 == -5, x) if mysol.rhs().n().parent() == RR]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-07-29 20:19:45 +0100

Seen: 1,155 times

Last updated: Jul 30 '13