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

3 views (last 30 days)
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

Accepted Answer

VBBV
VBBV on 14 Apr 2021
%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 Comment
Hirak
Hirak on 14 Apr 2021
Edited: Hirak on 14 Apr 2021
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));

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!