Ask Your Question
0

Vector field plot of python function

asked 0 years ago

cstaecker gravatar image

updated 0 years ago

FrédéricC gravatar image

I am encountering strange behavior when plotting a vectorfield. My example is much more complicated, but here is a simplification which still behaves badly for me:

def g(x):
    if x<0:
        return -1
    else:
        return 1

x,y = var('x,y')
plot_vector_field((g(x),0), (x,-1,1),(y,-1,1))

The plotted result shows all vectors plotted as (1,0), which is not right. I guess I'm not allowed to use bare python if/else in the definition of g? Is there some different way to call plot_vector_field so that this will work?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 0 years ago

When you pass g(x) as an argument, since g is a Python function, it tries to evaluate the function. It then can't tell whether x<0, so it returns 1. You could instead change the call to plot_vector_field to use the lambda x,y: g(x) construction. Since plot_vector_field expects two functions as input, let's use the same function twice, once with x and once with y:

plot_vector_field((lambda x, y: g(x), lambda x,y: g(y)), (x,-1,1),(y,-1,1))
Preview: (hide)
link

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: 0 years ago

Seen: 269 times

Last updated: Jun 26 '24