Ask Your Question
1

Plotting error message regarding numerical type of function arguments

asked 2023-05-22 23:51:56 +0100

toni gravatar image

I am trying to plot the following function:

x = var('x')
def y(x): return RR(len(prime_range(floor(0.95 * x), floor(1.25 * x))) / round(1.25 * x - 0.95 * x))

I am not sure whether it is well-constructed mathematically, but it's trying to check the density of prime numbers around a given value x I am using the floor() function because I'm not sure that prime_range() accepts non-integers.

If I test the function with random real values of x I get a plausible output without errors.

However, when I try to plot the function as in:

plot(y(x), (x, 5, 20))

I get the following error message:

TypeError: unable to convert floor(0.950000000000000*x) to an integer
and argument is also not real: unable to simplify to float approximation

I don't understand why this input cannot be understood as real by sagemath, or why the floor is not an integer.

edit retag flag offensive close merge delete

Comments

1

To find out if prime_range accepts non-integers, you could just try it and see what happens. Try prime_range(30.438).

John Palmieri gravatar imageJohn Palmieri ( 2023-05-23 02:29:06 +0100 )edit

Thank you! @John Palmieri

toni gravatar imagetoni ( 2023-05-23 02:37:53 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2023-05-23 02:31:13 +0100

Use plot(y, (x, 5, 20)) — use y instead of y(x). When you pass y(x) as an argument, where y is a Python function, it gets evaluated right away: it doesn't plug in different values for x, it just tries to evaluate y(x) as a number. That's where the error messages come from. If you plug in y instead, it tests to see if it is a function and if so, it plugs in x-values in the given range.

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

1 follower

Stats

Asked: 2023-05-22 23:51:56 +0100

Seen: 299 times

Last updated: May 23 '23