2 views (last 30 days)

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

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.

Sign in to comment.

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.

Sign in to comment.

Guillaume
on 4 Sep 2015

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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/239752-which-way-is-correct-of-defining-function-i-am-getting-two-differnt-surfaces-if-i-specify-same-equ#comment_308092

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/239752-which-way-is-correct-of-defining-function-i-am-getting-two-differnt-surfaces-if-i-specify-same-equ#comment_308092

Sign in to comment.