Ask Your Question
0

Vector field plot of python function

asked 2024-06-26 14:29:22 +0100

cstaecker gravatar image

updated 2024-06-26 14:55:41 +0100

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2024-06-26 19:21:09 +0100

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))
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: 2024-06-26 14:29:22 +0100

Seen: 164 times

Last updated: Jun 26