Which way is correct of defining function...I am getting two differnt surfaces if I specify same equation differently...?
Mostrar comentarios más antiguos
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 comentario
- f is not a function, but it is a variable (probably numeric).
- Different operators do different things, and the dot (element-wise) and matrix operators are very different:
Respuesta aceptada
Más respuestas (2)
John D'Errico
el 4 de Sept. de 2015
Editada: John D'Errico
el 4 de Sept. de 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
el 4 de Sept. de 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.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!