Help with implicit functions in vector form

I've been trying to plot a line, for example: 3x+y+4=0 in implicit form as follow:
w=[3 1 4];
fimplicit(@(x,y) [x y 1]*w')
The output is correct, however it takes ages to compute and this warning sign appears:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
When I write the code this way:
w=[3 1 4];
fimplicit(@(x,y) w(1)*x+w(2)*y+w(3))
it works the same but without the warning, and with short computation time.
I have no clue why this happens since the two expressions are exactly the same.
Any help?

1 个评论

fimplicit is optimized for array operations. (My guess is that fimplicit evaluates the input function on a meshgrid of x and y values, for which array operations are faster and robust as well)
From the documentation - "Use element-wise operators for the best performance and to avoid a warning message." Also "Use array operators instead of matrix operators for the best performance. For example, use .* (times) instead of * (mtimes)."
The first piece of code uses matrix operation and thus you get the warning. The final expressions are same, but the method used are different.

请先登录,再进行评论。

回答(1 个)

I have no clue why this happens since the two expressions are exactly the same.
The reason is that [x y 1]*w' cannot be evaluated for x,y being vectors, but w(1)*x+w(2)*y+w(3) can.
So you always should try to write your function such that it can evaluate for vector inputs.
If this is not possible, you may use:
fimplicit(@(X,Y)arrayfun(@(x,y)fun(x,y),X,Y))
e.g.
w=[3 1 4];
fun = @(x,y) [x y 1]*w';
fimplicit(@(X,Y)arrayfun(@(x,y)fun(x,y),X,Y))

类别

帮助中心File Exchange 中查找有关 Line Plots 的更多信息

产品

版本

R2022b

提问:

2023-4-2

编辑:

2023-4-2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by