手計算の概算とMatlabの計算が合わない

8 views (last 30 days)
優輔 井内
優輔 井内 on 12 Mar 2023
Commented: Atsushi Ueno on 13 Mar 2023
マトラボ上で以下の計算を行おうと考えています.
Xsin = sin(x*10*pi);
Ycos = cos(x*10*pi);
theta = atan2(Xsin,Ycos);
a=90*pi/180;
R = sin(a)/sqrt(1-cos(a)*sin(2*theta));
分子のsin(a)は1で,分母のcos(a)は0,-1 <= sin(2*theta) <= 1なのでRは1に近い値が出るはずですが,
マトラボの計算結果では,値が0と表示されます.
また,sqrt(1-cos(a)*sin(2*theta))のみ計算すると1が計算結果として出てきます.
原因がわからないので教えていただけると幸いです.

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 13 Mar 2023
Edited: Atsushi Ueno on 13 Mar 2023
> 原因がわからないので教えていただけると幸いです
原因は「x が列ベクトルなのに要素毎の除算をしていないから」だと予想します。
MATLABの除算演算子 ( / ) は除数がスカラでなければ「線形方程式の求解(行列の除算)」を行います。
今回の意図は「要素毎の除算」なので、演算子にドットを付けて ( ./ ) 除算する必要があります。
x = (-1:0.5:1)'; % x はズバリ列ベクトルでしょう
Xsin = sin(x*10*pi);
Ycos = cos(x*10*pi);
theta = atan2(Xsin,Ycos);
a=90*pi/180;
R = sin(a)/sqrt(1-cos(a)*sin(2*theta)) % 意図した結果にならない
R = 1×5
1 0 0 0 0
R = sin(a)./sqrt(1-cos(a)*sin(2*theta)) % 意図した結果になった
R = 5×1
1 1 1 1 1
  1 Comment
優輔 井内
優輔 井内 on 13 Mar 2023
Atsushi Ueno 様
ありがとうございます.
解決いたしました.

Sign in to comment.

More Answers (1)

Hernia Baby
Hernia Baby on 12 Mar 2023
自分の計算では1となります。
入力する x で何か間違えたりはしていますか?
x = linspace(0,180,10)./180;
Xsin = sin(x*10*pi);
Ycos = cos(x*10*pi);
円を書いてプロットしてみます
t = linspace(0,2*pi,100);
figure
hold on
plot(sin(t),cos(t),':','Color',[.3 .3 .3])
xline(0,'k')
yline(0,'k')
axis([-2,2,-2,2])
axis square
plot(Ycos,Xsin,'r*')
hold off
計算してみます
theta = atan2(Xsin,Ycos);
a=90*pi/180;
R = sin(a)./sqrt(1-cos(a)*sin(2*theta));
表示します
disp(R)
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
  2 Comments
優輔 井内
優輔 井内 on 13 Mar 2023
Edited: 優輔 井内 on 13 Mar 2023
回答ありがとうございます.
Atsushi Uenoさんのご指摘の通りに行うと,つまり,下記式のsin(a)のあとに「.」を入れるといけました.
R = sin(a)./sqrt(1-cos(a)*sin(2*theta));
Atsushi Ueno
Atsushi Ueno on 13 Mar 2023
因みに入力が行ベクトルの場合、そのままだと被除数と除数の行列サイズが矛盾している為エラーが出て計算出来ません。@Hernia Babyさんはこのエラーを見た瞬間すぐさま除算演算子にドットを付けたはずです。

Sign in to comment.

Categories

Find more on プログラミング in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!