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

1 view (last 30 days)
Maruti Patil on 4 Sep 2015
Answered: Stefan Karlsson on 4 Sep 2015
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
Stephen23 on 4 Sep 2015
Edited: Stephen23 on 4 Sep 2015
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 on 4 Sep 2015
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.

John D'Errico on 4 Sep 2015
Edited: John D'Errico on 4 Sep 2015
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 on 4 Sep 2015
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.