How to call two sets of arrays in a nested loop?

1 回表示 (過去 30 日間)
Hirak
Hirak 2021 年 4 月 14 日
編集済み: Hirak 2021 年 4 月 14 日
Hello Experts,
I have taken two sets of arrays. First one is 'alpha', which consists of 251 elements, the second one is 't', which consists of 721 data points.
For each alpha, I need to evaluate 'M', and thereby, (M*cos(t), M*sin(t)), which describes a 2 dimensional family of curves.
I need to create a dataset for 'M' considering each value of 'alpha' varying the values of 't' from 0 to 2*pi, and constructed the code expecting a matrix of [251*721] order. But the code returns only one value for M. There is no error message. Please help with your enlightened view.
Hirak
a=45;
x1=1:.02:6;
alpha=x1.^4;
t = 0:(pi/360):2*pi;
array=zeros(251,721);
for k1=1:.02:6
for k2=0:(pi/360):2*pi
M=a.*sqrt(cos(2.*k2)+sqrt(k1+(sin(2*k2).^2)));
x = M.*cos(k2);
y = M.*sin(k2);
end
end

採用された回答

VBBV
VBBV 2021 年 4 月 14 日
%if true
a=45;
x1=1:.02:6;
alpha=x1.^4;
t = 0:(pi/360):2*pi;
%rray=zeros(251,721);
p = 0:(pi/360):2*pi;
K = 1:0.02:6;
for k1= 1:length(K)
for k2=1:length(p)
M(k1,k2) =a*sqrt(cos(2*p(k2))+sqrt(K(k1)+(sin(2*p(k2).^2)));
x(k2) = M(k1,k2)*cos(p(k2));
y(k2) = M(k1,k2)*sin(p(k2));
end
end
Try this
  1 件のコメント
Hirak
Hirak 2021 年 4 月 14 日
編集済み: Hirak 2021 年 4 月 14 日
Tried the code, but it appears k1 and k2 is not changing for x and y. I have just chnged it to
x(k1,k2) = M(k1,k2).*cos(p(k2));
y(k1,k2) = M(k1,k2).*sin(p(k2));

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by