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

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
show(p0)


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)
show(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 close merge delete

Sort by » oldest newest most voted

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)
show(p1)


This plot shows just one real root.

ans=solve(f(x)==0,x)
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:

ans.rhs().n()


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

find_root(f(x),0,3)


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.

more

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

[mysol.rhs().n() for mysol in solve(x^3 + 4*x - 2 == -5, x) if mysol.rhs().n().parent() == RR]
[-0.673593058218711]

more

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.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()
-1.33226762955019e-15


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()
0


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

sage: a.n()
1.25538315684475


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())
0.000000000000000


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

more