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 件のコメント
Dyuman Joshi
2023 年 4 月 2 日
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
