Syntax error when dividing by a matrix variable

35 ビュー (過去 30 日間)
Robert Demyanovich
Robert Demyanovich 2025 年 11 月 17 日 19:09
コメント済み: Walter Roberson 2025 年 11 月 18 日 18:41
I keep getting an error with this line of code:
q=(R*sin(theta))./(1-cos(theta)*cos(t).);
R and theta are constants. t is given by the following
N = 100
dt = 2*pi/N;
t=(0:dt:2*pi)';
Since t contains a matrix of values, I thought the correct way to include this was through cos(t)., but Matlab keeps throwing up the following error:
"Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters."
If I just use cos(t) [without the period], then there is no error but all of the values of q in the resulting matrix are identical. I don't understand what I am doing wrong.
  2 件のコメント
Stephen23
Stephen23 2025 年 11 月 18 日 6:49
"Syntax error when dividing by a matrix variable"
That error is unrelated to any division operator.
"Since t contains a matrix of values, I thought the correct way to include this was through cos(t)., but Matlab keeps throwing up the following error"
The documentation for https://www.mathworks.com/help/matlab/ref/double.cos.html does not mention dots anywhere. It states that it accepts one "Input angle in radians, specified as a scalar, vector, matrix, multidimensional array, table, or timetable". So cos() already accepts a vector.
"If I just use cos(t) [without the period], then there is no error but all of the values of q in the resulting matrix are identical."
Lets try it:
theta = 1;
R = 1;
N = 100;
dt = 2*pi/N;
t = (0:dt:2*pi)';
q = (R*sin(theta))./(1-cos(theta)*cos(t))
q = 101×1
1.8305 1.8263 1.8137 1.7932 1.7653 1.7309 1.6909 1.6463 1.5981 1.5474 1.4949 1.4416 1.3883 1.3354 1.2835
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
They look different to me.
Walter Roberson
Walter Roberson 2025 年 11 月 18 日 18:41
You have mistaken the purpose of the dot in expressions such as A./B . It is not (A.) / B with . being some operator. Instead, it is an operator named ./ just the same way as ~= and <= are the names of single operators.

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

採用された回答

Star Strider
Star Strider 2025 年 11 月 17 日 19:19
The additional period (or decimal point) may be confusing the interpreter.
q=(R*sin(theta))./(1-cos(theta)*cos(t).);
^
That error is only thrown with the dot present, not otherwise.
q=(R*sin(theta))./(1-cos(theta)*cos(t));
q=(R*sin(theta))./(1-cos(theta)*cos(t).);
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
.

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by