The Usage of Dot Operation in Plot a Function
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi, how can I decide whether I should use dot for describing a function. There is a code below which has been written by myself. If you look closely, there is a difference if I write (3.*cos(x)). or (3.*cos(x))x or (3*cos(x)) three of them results a different shape of plot function. So, how does this dot change whole the game? I have searched but couldn't exact source to understand it. Here is my test code: clear; close;
x =-4:0.1:9; f = ((3.*cos(x))/(0.5.*x+exp(-0.5.*x)))-(4.*x/7);
plot(x, f, '--');
댓글 수: 4
Stephen23
2021년 11월 9일
편집: Stephen23
2021년 11월 9일
"When I remove the dot which comes after cos(x), the function plot changes."
There is no such thing as a "dot operator" in the sense that you wrote in the question tags and title.
"Can you please explain this reason?"
The link in my previous answer explains the reason: did you read it?
" I couldn't understand that, in my example I have a function. However, how ould I solve it with matrices please?"
It is very simple: if you are doing linear algebra then use matrix operations, otherwise use array operations. If you are not sure what linear algebra is, then most likely you are not doing linear algebra (hint: you are not).
답변 (2개)
KSSV
2021년 11월 9일
You missed element-by-element division. Now check
x =-4:0.1:9;
f1 = ((3.*cos(x))./(0.5.*x+exp(-0.5.*x)))-(4.*x/7); % <---- used./ instead of /
f2 = ((3.*cos(x)).*x./(0.5.*x+exp(-0.5.*x)))-(4.*x/7); % <------ used ./ instead of /
plot(x, f1, '--',x,f2,'--');
댓글 수: 0
cikalekli
2021년 11월 9일
댓글 수: 1
Steven Lord
2021년 11월 9일
Let me show you the difference with a concrete example involving two small matrices.
A = [1 2; 3 4];
B = [5 6; 7 8];
When you use the .* operator you're performing the operation element by element. In this example each element in A is multiplied by the corresponding element in B.
elementByElement = A.*B
expectedResult = [1*5, 2*6; 3*7, 4*8]
When you use the * operator you're performing matrix multiplication, where element (r, c) of the result is the dot product of row r of A with column c of B.
matrixMultiplication = A*B
expectedResult = [dot(A(1, :), B(:, 1)), dot(A(1, :), B(:, 2));
dot(A(2, :), B(:, 1)), dot(A(2, :), B(:, 2))]
The division and power operators also have both element-wise and matrix versions, and like with the multiplication operator those versions generally won't give the same result. In fact, there are some pairs of inputs for which only one of those versions is mathematically defined (you can matrix multiply a 3-by-4 and a 4-by-5 matrix, but you can't element-wise multiply them.)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!