How can I loop through an equation with X different values of length at an angle Y, store coordinates then repeat for different angles

2 views (last 30 days)
This is a snippet of my code which is to calculate a coordinate system. In this example for these values of N, I should get 500 Y and 500 Z values but the code just saves the last 100 points calculated. Any help would be appreciated!
N=5;
NN=100;
z =linspace(0,pi/2,N);
x = linspace (0.005,0.4,NN);
for k=1:N
for i=1:NN
Hr3(i) = somefunctionofx;
Wr3(i) = somefunctionofx;
Z03(i) = somefunctionofx;
Nr3(i) = somefunctionofx;
Ar3(i) = Hr3(i)/2;Br3(i) = Wr3(i)/2;
r3(i)=(((Ar3(i)*Br3(i))^Nr3(i))/(((Ar3(i).*sin(z(k)))^Nr3(i))+((Br3(i).*cos (z(k))^Nr3(i))))^(1/Nr3(i));
Y3 (:,i) = r3(i) .* sin(z(k));
Z3 (:,i) = (r3(i) .* cos (z(k))) + Z03(i);
end
end

Accepted Answer

Roger Stafford
Roger Stafford on 17 Nov 2013
As your code is written, the 100 values of y and z are entered into the Y3 and Z3 arrays at the same places for each of the five values of k. This means that those for the first four values of k are necessarily overwritten and therefore destroyed. You need to provide for some altered position in Y3 and Z3 as k changes. If you want these to be two dimensional arrays, you can write:
Y3 (k,i) = r3(i) .* sin(z(k));
Z3 (k,i) = (r3(i) .* cos (z(k))) + Z03(i);
instead of the two lines you have.
It is advisable to allocate the memory for these before entering the for-loop with:
Y3 = zeros(N,NN);
Z3 = zeros(N,NN);

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!