When to use . matlab for equations

78 ビュー (過去 30 日間)
Stephen Bejide
Stephen Bejide 2019 年 12 月 19 日
コメント済み: Stephen Bejide 2019 年 12 月 20 日
When do I use . in matlab
For example:
theta = 0:1:90;
g = 9.81;
u = ((-6*g)./(((cos(theta)).^2)-sin(2.*theta))).^1/2;
plot(theta, u)
gives me a a graph with negative values but when I enter for theta for the same equation I get positive values...
Where do I place the '.''s within u?

採用された回答

John D'Errico
John D'Errico 2019 年 12 月 19 日
編集済み: John D'Errico 2019 年 12 月 19 日
First, I think you do not understand that MATLAB uses RADIANS for trig functions. (Pretty standard, at least once you get past high school trig.) So if theta is in degrees, then you CANNOT use sin and cos. Instead, use sind and cosd, which assume degree inputs.
theta = 0:1:90;
g = 9.81;
u = ((-6*g)./(((cosd(theta)).^2)-sind(2.*theta))).^1/2;
Next, use the dotted operators, thus .*, ./ and .^ whenever you have vector or array operands, and you wish to do the same computation for every element of the vector or array.
So, if X is a vector or array and you just want to square every element of the vector or array, then X^2 will not work. But X.^2 will work.
You can get away with something like 2*X, instead of needing to write 2.*X, because MATLAB sees the scalar as one of the operands.
Becareful though, as 1/X will be a problem, if you only want to take the reciprocal of every element in X. Instead, you need to use 1./X there.
Next, you need to learn the order of operations in MATLAB. The same order applies to most languages. For example, if you want to take the square root of a number, do NOT type this:
(1:5).^1/2
ans =
0.5 1 1.5 2 2.5
Did it work? Of course not! MATLAB raised each element of the vector to the FIRST power, then divided that result by 2. Why? Because .^ comes higher in the precedence table.
Anyway, use sqrt.
sqrt(1:5)
ans =
1 1.4142 1.7321 2 2.2361
As far as the expression you pose, I have no idea what it is that you expect it to do, or what expression you really want to plot.
  4 件のコメント
John D'Errico
John D'Errico 2019 年 12 月 20 日
編集済み: John D'Errico 2019 年 12 月 20 日
If you wanted to raise X to the 2/5 = 0.4 power, you could write it as
X.^(2/5)
Or you could write it as
X.^0.4
In the second case, MATLAB is able to recognize 0.4 as a scalar value, so it will get things correct.
In general, if you are not sure about the order of operations, it never hurts to wrap things in parens. Too many extra sets of parens don't cost anything extra, but they can make your code more difficult to read sometimes.
Or, I suppose you could write it as
nthroot(X.^2,5)
For example...
X = 1:3;
X.^0.4
ans =
1 1.3195 1.5518
X.^(2/5)
ans =
1 1.3195 1.5518
nthroot(X.^2,5)
ans =
1 1.3195 1.5518
Or you might get tricky, and do this:
exp(0.4*log(X))
Personally, I'd stick with X.^(2/5). ;-) But every once in a while, the alternatives might be useful.
Stephen Bejide
Stephen Bejide 2019 年 12 月 20 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2019 年 12 月 19 日
The '.' is for element wise computations. Multipling a scalar by a vector or matrix does not require it.
u = ((-6*g)./(((cos(theta)).^2)-sin(2*theta))).^1/2;% 1./(matrix or vector) needs it (you are taking the recipical of each element) and (vector or matrix) .^ power needs it (you are raising each element to the power), but sin(2*theta) does not.
Any time you are multiplying, dividing, raising a vector or matrix by another matrix or vector of the same size and you want to perform the operation element by element you need the dot.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by