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일

1 개 추천

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

Stephen Bejide
Stephen Bejide 2019년 12월 19일
Thank you, worked straight away!
What would you do if you had for example x^2/5 then?
I don't understand the question. OK, so let's say you had that expression:
y = x .^ 2 / 5; % 0.2 * (x squared)
What are you asking about it? Asking about y or whatever you assigned that expression to? How are we supposed to answer this?
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일

0 개 추천

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.

카테고리

도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2019년 12월 19일

댓글:

2019년 12월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by