- 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:
Which way is correct of defining function...I am getting two differnt surfaces if I specify same equation differently...?
조회 수: 2 (최근 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
채택된 답변
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.
댓글 수: 0
추가 답변 (2개)
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.
댓글 수: 0
Guillaume
2015년 9월 4일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!