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!