Wrong output when using random numbers

1 回表示 (過去 30 日間)
Craig Muir
Craig Muir 2019 年 6 月 19 日
コメント済み: Stephen23 2019 年 6 月 19 日
I'm attempting to satisfy an equation using 10 random numbers in the range [-pi,pi]. The results tend to be in an odd format and only one of the values ever seems to match, suggesting that something is going wrong. My attempt is below, any help would be apprectiated :).
x = (rand(10,1) * 2 - 1) * pi
y1 = (3*cot(x)-(cot(x).^3))/(1 - 3*(cot(x).^2))
y2 = cot(3*x)
y1 =
Columns 1 through 7
0 0 0.0047 0 0 0 0
0 0 -0.0395 0 0 0 0
0 0 -2.6571 0 0 0 0
0 0 0.0034 0 0 0 0
0 0 0.0047 0 0 0 0
0 0 0.0082 0 0 0 0
0 0 -0.0068 0 0 0 0
0 0 0.0095 0 0 0 0
0 0 -0.0573 0 0 0 0
0 0 0.1041 0 0 0 0
Columns 8 through 10
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
y2 =
-1.4220
-0.4570
-2.6571
-0.8258
-1.4221
0.4081
-0.2741
0.7070
-0.5596
0.7511
Granted the values change each time, it seems that one always seems to work and I'm not entirely sure why.

採用された回答

madhan ravi
madhan ravi 2019 年 6 月 19 日
./ % this is why? see Array vs. Matlab operations [Google it]

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by