Which way is correct of defining function...I am getting two differnt surfaces if I specify same equation differently...?

1 回表示 (過去 30 日間)
f=(8.*x.^2)+(14.*y.^2)+(24.*x.*y)-(31.*y)+(25.*x) %--------1
f=(8*x^2)+(14*y^2)+(24*x*y)-(31*y)+(25*x) %----------------2
Although above surface equations are same but written in different fashion. while Using 'dot(.)' I am getting different surface than equation 2(without dot) which one is correct?? or tell me when to use dot(.) I am not sure when to use it Thanks in advance
  1 件のコメント
Stephen23
Stephen23 2015 年 9 月 4 日
編集済み: Stephen23 2015 年 9 月 4 日
  1. f is not a function, but it is a variable (probably numeric).
  2. Different operators do different things, and the dot (element-wise) and matrix operators are very different:

サインインしてコメントする。

採用された回答

Stefan Karlsson
Stefan Karlsson 2015 年 9 月 4 日
There are 2 interpretations of your shared code (i.e. two ways i see that you would be able to run it without error).
1. you have defined two symbolic variables. ie:
syms x y
f1=(8.*x.^2)+(14.*y.^2)+(24.*x.*y)-(31.*y)+(25.*x); %notice i changed the name to f1, f2
f2=(8*x^2)+(14*y^2)+(24*x*y)-(31*y)+(25*x);
simplify(f1 == f2) %they are INDEED THE SAME
2. you have defined two numeric matrices, x and y, of equal size, such as:
[x,y] = meshgrid(linspace(-1,1,10));
f1=(8.*x.^2)+(14.*y.^2)+(24.*x.*y)-(31.*y)+(25.*x);
f2=(8*x^2)+(14*y^2)+(24*x*y)-(31*y)+(25*x);
subplot(1,2,1); surf(f1);subplot(1,2,2); surf(f2);
Lets assume the second option. If you change your code above slightly to:
[x,y] = meshgrid(linspace(-1,1,10),linspace(-1,1,11));
f1=(8.*x.^2)+(14.*y.^2)+(24.*x.*y)-(31.*y)+(25.*x);
f2=(8*x^2)+(14*y^2)+(24*x*y)-(31*y)+(25*x); %%%%ERROR %%%%
you will get an error for the line of f2. The reason for this is that
x^2
and
y*x
are matrix multiplications, not element-wise multiplications. This is one of the most annoying and common source of hard-to-find bugs, even for experienced developers in Matlab.

その他の回答 (2 件)

John D'Errico
John D'Errico 2015 年 9 月 4 日
編集済み: John D'Errico 2015 年 9 月 4 日
Those are NOT the same equations! Using different operators is a good way to ensure that you will get different results.
The dot operators are elements-wise operations. So
C = A.*B
is equivalent to a loop over i and j, where we would have
C(i,j) = A(i,j)*B(i,j)
whereas
C = A*B
is a matrix multiply, i.e., using dot products.
In general, it is best (ok, safest) unless you are using linear algebra, to use the dotted operators. However, things like
2*x
or
x*2
are treated as element-wise operations, where the scalar is expanded to have the same implicit shape as the variable x. Be careful however, as
2/x
is NOT treated in that manner.

Guillaume
Guillaume 2015 年 9 月 4 日
See the documentation for the difference between dotted (array) and undotted (matrix) operators.
We can't tell you definitively which of the two equations is correct, it all depends on what the purpose is, but most likely the first one is what you intended.
Note that you don't need to put a dot for operations involving scalars, so you could have written:
f = 8*x.^2 + 14*y.^2 + 24*x.*y - 31*y + 25*x
Als note that f is not a function in the programming sense, it is just a variable.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by