The Usage of Dot Operation in Plot a Function
13 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 9 Nov 2021
Edited: Stephen23
on 9 Nov 2021
"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).
Answers (2)
KSSV
on 9 Nov 2021
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 Comments
cikalekli
on 9 Nov 2021
1 Comment
Steven Lord
on 9 Nov 2021
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.)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!