How to save "y" and "dy" in the same vector A each time the loop WHILE is incremented?

function cam()
clc
clear all
close all
N_i = input('N de intervalos:');
k=1;
while(k<=N_i)
k
beta_i(k) = input('Beta do intervalo:');
L(k) = input('Lift do intervalo:');
mov(k) = input('Movimento do intervalo:');
k=k+1;
end
R_0 = input('Raio base:');
teta=0;
passo=30;
n=1;
T(n)=teta;
j=1;
while(j<=N_i)
y=0;
dy=0;
teta=0;
T=0;
j
while(teta<=beta_i(j))
switch mov(j)
case 1
y=0.5*L(j)*(1-cos(pi*teta/beta_i(j)));
dy=((pi*L(j))/(2*beta_i(j)))*sin(pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 2
y=L(j)*((teta/beta_i(j))-0.5*(pi^-1)*sin(2*pi*(teta/beta_i(j))));
dy=(L(j)/beta_i(j))*(1-cos(2*pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 3
y=0.5*L(j)*((1-cos(pi*teta/beta_i(j)))-0.25*(1-cos(2*pi*teta/beta_i(j))));
dy=0.5*pi*(L(j)/beta_i(j))*(sin(pi*teta/beta_i(j))-0.5*sin(2*pi*teta/beta_i(j)));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 4
y=L(j)*(1-cos(0.5*pi*teta/beta_i(j)));
dy=((pi*L(j))/(2*beta_i(j)))*sin(0.5*pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 5
y=L(j)*sin(pi*0.5*teta/beta_i(j));
dy=0.5*pi*(L(j)/beta_i(j))*cos(0.5*pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 6
y=L(j)*((teta/beta_i(j))-(pi^-1)*sin(pi*(teta/beta_i(j))));
dy=(L(j)/beta_i(j))*(1-cos(pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 7
y=L(j)*((teta/beta_i(j))+(pi^-1)*sin(pi*(teta/beta_i(j))));
dy=(L(j)/beta_i(j))*(1+cos(pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 8
y=0.5*L(j)*(1+cos(pi*teta/beta_i(j)));
dy=-((pi*L(j))/(2*beta_i(j)))*sin(pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 9
y=L(j)*(1-(teta/beta_i(j))-0.5*(pi^-1)*sin(2*pi*(teta/beta_i(j))));
dy=-(L(j)/beta_i(j))*(1-cos(2*pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 10
y=0.5*L(j)*((1+cos(pi*teta/beta_i(j)))-0.25*(1-cos(2*pi*teta/beta_i(j))));
dy=-0.5*pi*(L(j)/beta_i(j))*(sin(pi*teta/beta_i(j))+0.5*sin(2*pi*teta/beta_i(j)));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 11
y=L(j)*(cos(0.5*pi*teta/beta_i(j)));
dy=-((pi*L(j))/(2*beta_i(j)))*sin(0.5*pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 12
y=L(j)*(1-sin(pi*0.5*teta/beta_i(j)));
dy=-0.5*pi*(L(j)/beta_i(j))*cos(0.5*pi*teta/beta_i(j));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 13
y=L(j)*(1-(teta/beta_i(j))+(pi^-1)*sin(pi*(teta/beta_i(j))));
dy=-(L(j)/beta_i(j))*(1-cos(pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 14
y=L(j)*(1-(teta/beta_i(j))-(pi^-1)*sin(pi*(teta/beta_i(j))));
dy=-(L(j)/beta_i(j))*(1+cos(pi*(teta/beta_i(j))));
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
case 15
y=L(j);
dy=0;
u=(R_0+y)*sin(teta)+dy*cos(teta);
v=(R_0+y)*cos(teta)-dy*sin(teta);
T(n)=teta;
end
A(n,:,j)=[y,dy,j]
P(n,:,j)=[u,v,j]
n=n+1;
teta=teta+passo;
end
figure
plot(T,A(:,1,j))
figure
plot(P(:,1,j),P(:,2,j))
j=j+1;
end
end
Given the following input:
N de intervalos:2
k =
1
Beta do intervalo:180
Lift do intervalo:2
Movimento do intervalo:1
k =
2
Beta do intervalo:180
Lift do intervalo:2
Movimento do intervalo:8
Raio base:0.025
I got as result for vector A:
A(:,:,1) =
0 0 1.0000
0.1340 0.0087 1.0000
0.5000 0.0151 1.0000
1.0000 0.0175 1.0000
1.5000 0.0151 1.0000
1.8660 0.0087 1.0000
2.0000 0.0000 1.0000
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
A(:,:,2) =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
2.0000 0 2.0000
1.8660 -0.0087 2.0000
1.5000 -0.0151 2.0000
1.0000 -0.0175 2.0000
0.5000 -0.0151 2.0000
0.1340 -0.0087 2.0000
0 -0.0000 2.0000
The problem is when j=2 (the second time the loop runs) the vector A seems to clear the values computed when j=1 (the first time the loop runs). How can I solve this problem?
I am just crashing into a wall over and over again.

 Accepted Answer

It actually is not clearing them. Think of ‘A’ as a deck of cards. Here, ‘A(:,:,1)’ is the first card, ‘A(:,:,2)’ is the second card, and so for the rest. All the values are still there.
Experiment with preallocating a new matrix ‘Ae’ and then concatenating to it in each loop to see if that works better for you:
Ae = [];
:
:
Ae = [Ae; y,dy,j];

5 Comments

Unfortunately, that tells me nothing useful.
What did it do?
What about it didn’t work?
Also, to clarify, this assignment goes before your second while loop:
Ae = [];
and this goes next to your ‘A’ assignment:
Ae = [Ae; y,dy,j];
j =
1
Ae =
0 0 1
Ae =
0 0 1.0000
0.5000 0.0302 1.0000
Ae =
0 0 1.0000
0.5000 0.0302 1.0000
1.5000 0.0302 1.0000
Ae =
0 0 1.0000
0.5000 0.0302 1.0000
1.5000 0.0302 1.0000
2.0000 0.0000 1.0000
j =
2
Ae =
2 0 2
Ae =
2.0000 0 2.0000
1.5000 -0.0302 2.0000
Ae =
2.0000 0 2.0000
1.5000 -0.0302 2.0000
0.5000 -0.0302 2.0000
Ae =
2.0000 0 2.0000
1.5000 -0.0302 2.0000
0.5000 -0.0302 2.0000
0 -0.0000 2.0000
This is now the output I get.
Then, I plot the results this way:
figure
plot(T,Ae(:,1,j))
and says "Index exceeds matrix dimensions."
I have no idea what you’re doing. I don’t understand your indexing.
In your plot call, the index will exceed matrix dimensions because ‘Ae’ is (Nx3) — a 2-dimensional matrix — not a 3-dimensional matrix. You need to re-write your plot call.
Apparently the preallocation assignment:
Ae = [];
is being reset. Put it before the first while loop and see it that works.

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!