-2

I am having some trouble writing this equation in Python and having it produce correct results:

y = 2.95710^-7 * x^4 – 2.34310^-5 * x^3 – 1.67*10^-4 * x^2 + 0.04938 * x – 1.083.

I have tried the following code:

y = 0.0000002957*x**4 - 0.00002343*x**3 - 0.000167*x**2 + 0.04938*x - 1.083

and also:

y = (2.957*10**-7)*x**4 - (2.343*10**-5)x**3 - (1.67*10**4)*x**2 + 0.04938*x - 1.083

any advice would be helpful, I think the problem might be the scientific notation or the exponents and the way I am inputting them

EDIT in response to questions, the equation spits out an incorrect number than what I get on a calculator

2 Answers2

1

What you have can easily be converted to Python in a very direct way. The equivalent Python statement (after fixing a flaw in the original equation...see below) is :

y = 2.95710e-7 * x**4 - 2.34310e-5 * x**3 - 1.67e-4 * x**2 + 0.04938 * x - 1.083

This involves only the simple substitution of the ^ characters, where you replace that character with e in the exponential floating point constants and with ** for raising x to an integer power. For reference to be able to more easily compare the source with the result, here's the original equation with the one slight fix mentioned below:

y = 2.95710^-7 * x^4 – 2.34310^-5 * x^3 – 1.67^-4 * x^2 + 0.04938 * x – 1.083

UPDATE: Thanks to Pranav for pointing out a flaw in the source equation. The term 1.67*10^-4 should be changed to 1.67^-4 to match the other similar terms, and the equivalent fix made to the resulting Python equation. Those fixes were made and commented on above.

I always like to see what I'm working with in situations like this. I used matplotlib to plot this function, using an unchanged version (except for the function and the bounds on x) of this sample code. Here's what I got for x values between -500 and 500:

enter image description here

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

Thanks for answering everybody. The most helpful answer was using "e" instead of scientific notation (*10^x).