条件ごとに計算させるにはどうすればよいか

20 visualizaciones (últimos 30 días)
優輔 井内
優輔 井内 el 25 de Oct. de 2022
Comentada: 優輔 井内 el 4 de Nov. de 2022
ある行列A,B(A,Bはサインカーブをえがく)に対して
A>=0,B>=0,|A|>=|B| ならば arctan(B/A)
A>=0,B>=0,|A|<|B|  ならば π/2-arctan(A/B)
A<0,B>=0,|A|<|B|  ならば π/2+arctan(|A|/B)
A<0,B>=0,|A|>=|B|  ならば π-arctan(B/|A|)
A<0,B<0,|A|>=|B|   ならば π+arctan(B/A)
A<0,B<0,|A|<|B|  ならば 3π/2-arctan(A/B)
A>=0,B<0,|A|<|B|  ならば 3π/2+arctan(A/|B|)
A>=0,B<0,|A|>=|B|  ならば 2π-arctan(|B|/A)
の計算を適用したいと考えています.1)
以下は途中経過なのですが,上の条件で場合分けして計算させるのがうまくいきません.
助言していただけると幸いです.
%Start of script(初期化)
close all; % close all figures
clear; % clear all variables
clc; % clear the command terminal
%変数の型の定義
format long
%読み込みたいファイルを現在のフォルダに入れ,読み込みたいファイルの名前を入力
M1=readmatrix('データ.txt');
%読み込んだファイルのサイズの確認
sz1=size(M1);
Asz1=sz1(1,1);
%格納する空の配列
%sinAとsinBの値を取得
A1=zeros(Asz1,1);
A1(1,1)=M1(1,1);
B1=zeros(Asz1,1);
B1(1,1)=M1(1,2);
T1=zeros(Asz1,1);
%サンプリングタイム
tc = 1/1000000;
t1 = 1/1000000;
for i=2:Asz1
T1(i,1)=t1;
%サンプリング間隔
t1=t1+tc;
%X軸移動量
A1(i,1)=M1(i,1);
B1(i,1)=M1(i,2);
end
%時間と信号値の行列
CA1 = cat(2,T1,A1);
CB1 = cat(2,T1,B1);
n1 = zeros(Asz1,1);
for j = 1:Asz1
n1(j,1) = atan(B1(j,1)/A1(j,1));
end
参考文献
1) 藤澤正司:リニアモータ制御に適した高速応答可能なエンコーダの高分解能技術とその誤差低減方法,気学会論文誌D(産業応用部門誌)12012(2000)1426-1432

Respuesta aceptada

J. Alex Lee
J. Alex Lee el 26 de Oct. de 2022
Do you know about the function "atan2", which might be helpful
M1 = readmatrix("tmp.txt");
tc = 1/1000000;
% no need to use loops
A = M1(:,1);
B = M1(:,2);
T = transpose(0:tc:(tc*(numel(A)-1)));
aA = abs(A);
aB = abs(B);
mask{1} = A>=0 & B>=0 & aA>=aB;
mask{2} = A>=0 & B>=0 & aA <aB;
mask{3} = A< 0 & B>=0 & aA <aB;
mask{4} = A< 0 & B>=0 & aA>=aB;
mask{5} = A< 0 & B< 0 & aA>=aB;
mask{6} = A< 0 & B< 0 & aA <aB;
mask{7} = A>=0 & B< 0 & aA <aB;
mask{8} = A>=0 & B< 0 & aA>=aB;
funs{1} = @(a,b)(atan(b./a));
funs{2} = @(a,b)(pi/2-atan(a./b));
funs{3} = @(a,b)(pi/2+atan(abs(a)./b));
funs{4} = @(a,b)(pi -atan(b./abs(a)));
funs{5} = @(a,b)(pi +atan(b./a));
funs{6} = @(a,b)(3*pi/2-atan(a./b));
funs{7} = @(a,b)(3*pi/2+atan(a./abs(b)));
funs{8} = @(a,b)(2*pi-atan(abs(b)./a));
C = nan(size(A));
for i = 1:numel(mask)
C(mask{i}) = funs{i}(A(mask{i}),B(mask{i}));
end
D = atan2(B,A);
figure; cla; hold on;
plot(T,A);
plot(T,B);
plot(T,C);
plot(T,D);
plot(T,atan(B./A));
  1 comentario
優輔 井内
優輔 井内 el 28 de Oct. de 2022
J. Alex Leeさん,回答ありがとうございます.非常にたすかりました.
以下の箇所についてですが,
mask{1} = A>=0 & B>=0 & aA>=aB;
funs{1} = @(a,b)(atan(b./a));
についてはどのような関数をつかっているのでしょうか.
MATLABのヘルプをつかってもわかりませんでした.

Iniciar sesión para comentar.

Más respuestas (1)

J. Alex Lee
J. Alex Lee el 28 de Oct. de 2022
Gomen, kana tukaemasen.
mask ni kanshiteha "logical indexing" wo search shitemitekudasai: link
@(x)fun ni kanshiteha "anonymous functions": link
% set up
x = rand(1,10)-0.5;
myfun = @(x)(sin(x));
% check return values
x
x = 1×10
0.4874 -0.1227 0.3681 0.2856 0.2727 0.3685 -0.0754 0.4382 -0.4070 0.2057
myfun
myfun = function_handle with value:
@(x)(sin(x))
% indexing
condition = x<0
condition = 1×10 logical array
0 1 0 0 0 0 1 0 1 0
indices = find(condition)
indices = 1×3
2 7 9
x(condition)
ans = 1×3
-0.1227 -0.0754 -0.4070
x(indices)
ans = 1×3
-0.1227 -0.0754 -0.4070
% using anonymous function
y = myfun(x)
y = 1×10
0.4683 -0.1224 0.3599 0.2817 0.2693 0.3602 -0.0753 0.4243 -0.3959 0.2042
y = sin(x)
y = 1×10
0.4683 -0.1224 0.3599 0.2817 0.2693 0.3602 -0.0753 0.4243 -0.3959 0.2042
  1 comentario
優輔 井内
優輔 井内 el 4 de Nov. de 2022
J. Alex Leeさん,回答ありがとうございます.
勉強になりました.
今後ともよろしくお願いします.

Iniciar sesión para comentar.

Categorías

Más información sobre Particle Swarm en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by