条件ごとに計算させるにはどうすればよいか
1 view (last 30 days)
Show older comments
ある行列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
参考文献
0 Comments
Accepted Answer
J. Alex Lee
on 26 Oct 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));
More Answers (1)
J. Alex Lee
on 28 Oct 2022
Gomen, kana tukaemasen.
% set up
x = rand(1,10)-0.5;
myfun = @(x)(sin(x));
% check return values
x
myfun
% indexing
condition = x<0
indices = find(condition)
x(condition)
x(indices)
% using anonymous function
y = myfun(x)
y = sin(x)
See Also
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!