Question

I have a function f(x,y) whose outcome is random (I take mean from 20 random numbers depending on x and y). I see no way to modify this function to make it symbolic.

And when I run

    x,y = var('x,y') 
    d = plot_vector_field((f(x),x), (x,0,1), (y,0,1))

it says it can't cast symbolic expression to real or rationa number. In fact it stops when I write:

    a=matrix(RR,1,N)
    a[0]=x

What is the way to change this variable to real numbers in the beginning, compute f(x) and draw a vector field? Or just draw a lot of arrows with slope (f(x),x)?

Was it helpful?

Solution

I can create something sort of like yours, though with no errors. At least it doesn't do what you want.

def f(m,n):
    return m*randint(100,200)-n*randint(100,200)

var('x,y')
plot_vector_field((f(x,y),f(y,x)),(x,0,1),(y,0,1))

The reason is because Python functions immediately evaluate - in this case, f(x,y) was 161*x - 114*y, though that will change with each invocation.

My suspicion is that your problem is similar, the immediate evaluation of the Python function once and for all. Instead, try lambda functions. They are annoying but very useful in this case.

var('x,y')
plot_vector_field((lambda x,y: f(x,y), lambda x,y: f(y,x)),(x,0,1),(y,0,1))

random vectors, sort of

Wow, I now I have to find an excuse to show off this picture, cool stuff. I hope your error ends up being very similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top