Solve polynomial between two values interval

Asked

Viewed 326 times

2

x = eval(input())
x = eval(input())
polinomio = 3*x**3- 5*x + 0.8
for x in range (x,x,0.5):
    print(polinomio)

Traceback (Most recent call last): File "C:/Users/User/Appdata/Local/Programs/Python/Python36-32/fdrew.py", line 4, in for x in range (x,x,0.5): Typeerror: 'float' Object cannot be Interpreted as an integer

  • You really want something that varies between [x,x) varying from 0.5?

  • See here how to use the range: https://answall.com/a/204411/64969

  • But they’re two different values. For example, if the program reads the values 2.4 and 4.6, it should print the value of that polynomial for the following points (values of x): 2.4, 2.9, 3.4, 3.9 and 4.4.

  • range starting at the lower end and spaced 0.5

  • You expect the interpreter to know which is the first x and what the second x? For him it’s all x, then you have lost any and all reference to the first information read. Not to mention what you call polinomio is not a polynomial, but a real number obtained by calculating a polynomial for x worth its second value

  • You can give me a suggestion then pff how to enter another value for x and resolve for those values spaced between 0.5?

  • Am I a novice in python and would like an answer to my mistake or suggestion? Thank you.

Show 2 more comments

1 answer

5

We will solve your problem step by step.

First, let’s see the use of range. Let’s look at the documentation and... For life, it only accepts integer numbers! This function accepts up to 3 parameters (check that answer for more chewed details). How we will proceed then?

An alternative is to proceed arithmetically. If we want to take the values between m and M leaping from 0.5 in 0.5 (being i iterator), this is identical to picking the values between 2*m and 2*M leaping from 1 in 1, iterating on the variable i_dobro which can easily be transformed into i = i_dobro/2. But this only works in Python 3 (for Python 2 you have to divide by 2.0) and if m and M be whole...

So maybe we can use while? Let’s start with m, as small as M, increasing 0.5 every step... it seems, it seems reasonable.

Now, how about we find out who it is m and M that I so much quoted above? Well, m is the lower margin of the range, while M is the upper margin. As this data is read from the standard entry, and has no restriction or complementary text to this problem, I can only believe that the two entries will be numbers possibly out of order. Then let’s go n1 to the first number and n2 for the second number. To determine m and M that’s how it is:

(M, m) = (n1, n2) if n1 > n2 else (n2, n1)

Okay, so generally our iteration goes like this (I’ll keep your reading as much as I disagree with it):

n1 = eval(input())
n2 = eval(input())

(M, m) = (n1, n2) if n1 > n2 else (n2, n1)

i = m
while i < M:
    # ação interessante da iteração 
    i += 0.5

So, how to make the action interesting of iteration? The desired result is the impression of the evaluation of the polynomial before the i past. I see the following options:

  • calculate directly in the print
  • define a priori function and call it cheerfully
  • create a lambda and call it even more cheerfully

For the first option, it is only necessary to change the commented section:

print(3* i**3 -5*i + 0.8)

For the second option, before taking the proper readings, set the function like this:

def polinomio(x):
    return 3* x**3 -5*x + 0.8

Then call inside the print:

print(polinomio(i))

For lambda alternative, before iteration, create lambda function and assign to variable polinomio:

polinomio = (lambda x: 3* x**3 -5*x + 0.8)

And call inside the print as if it were a traditional function:

print(polinomio(i))
  • Thank you very much for your answer and patience. I understood everything you said and I understood everything but I ran the program and it’s giving me this error: Traceback (Most recent call last): File "C: Users User Appdata Local Programs Python Python36-32 fdrew.py", line 4, in <module> M, m = n1, N2 if n1 > N2 Else N2, n1 Valueerror: Too Many values to unpack (expected 2)

  • @Ricegum correcting...

  • It seems I trusted too much in the resolution of tuples of Python... see here something similar working: https://ideone.com/fVoCqz; I took advantage and corrected the answer properly

  • I’m sorry but I didn’t understand it at all. Did you give fixed values? They can’t be fixed it has to be the ones I introduce. I ran what I presented and the output was: 1 2 3 5

  • 1

    I didn’t even enter values

  • Example was the syntax used to catch the largest and the smallest, it was not of your program as a whole. I’m not going to go here for an example of your program from start to finish because it is my intention that you have the effort to sew, with the information I gave, the program so that you have training in Python and acquire expertise. If you go to see, it’s only about the part that had given error of many values to unpack. The error no longer happens, using the code in the correct way

  • I think I got it, I’m gonna post it like I have the show. Regarding the values requested in the statement is giving me 1 less value at the beginning and one more value at the end,

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.