The Usage of Dot Operation in Plot a Function
조회 수: 6(최근 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, '--');
답변(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.)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!