MATLAB: Which way is correct of defining function…I am getting two differnt surfaces if I specify same equation differently…

dotequationMATLABmeshsurface

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

Best Answer

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.
Related Question