The Usage of Dot Operation in Plot a Function

22 views (last 30 days)
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
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."
Because / and ./ are different operators (matrix mrdivide and array rdivide respectively).
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).
cikalekli
cikalekli on 9 Nov 2021
Frankly, thank you. You have elaborated my confusion cleary. Now I got it the difference of them with your explanation and the link that you have provided me.

Sign in to comment.

Answers (2)

KSSV
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,'--');

cikalekli
cikalekli on 9 Nov 2021
Hi, thank you. Here when I write this:
f=((3.*cos(x)./(0.5.*x + exp(-0.5.*x))) - (4.*x/7)
and
f=((3.*cos(x)/(0.5.*x + exp(-0.5.*x))) - (4.*x/7)
When I remove the dot which comes after cos(x), the function plot changes.
Can you please explain this reason? Because why we again put dot after writing the whole function which was
(3.*cos(x)).
Here is the photo of function which I used for this code as an example:
  1 Comment
Steven Lord
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
elementByElement = 2×2
5 12 21 32
expectedResult = [1*5, 2*6; 3*7, 4*8]
expectedResult = 2×2
5 12 21 32
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
matrixMultiplication = 2×2
19 22 43 50
expectedResult = [dot(A(1, :), B(:, 1)), dot(A(1, :), B(:, 2));
dot(A(2, :), B(:, 1)), dot(A(2, :), B(:, 2))]
expectedResult = 2×2
19 22 43 50
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.)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!