Saving maximum values from last iteration.

Hi, I want to save maximum values from last iteration for every "ii". If i run code for a single "ii" it saves up the data properly, but when I run it in range from 1 to 10 it just saves the first one. What am I missing?
*Edited code as asked.
d = 2;
v=700;
om = 500;
dp=5;
dC=20;
m = 1;
S = pi * ((d^2) / 4);
r = 1.500;
g=8;
dt = 1e-5;
i = 1;
t(i) = 0;
t0 = 0;
max_values=zeros(10,3);
v0=zeros(1,10);
numb = [ 0 0 0 0;1 1 1 1; 2 2 2 2;3 3 3 3;4 4 4 4;5 5 5 5;6 6 6 6;7 7 7 7;8 8 8 8;9 9 9 9];
for ii = 1:10
x(i)=numb(ii,1);
y(i)=numb(ii,2);
z(i)=numb(ii,3);
v0(ii) = om * numb(ii,4) * 2 * pi;
data = [0 0 300 240 180 120 60 0 330 300];
if ii==1
vz(i) = v;
vy(i) = 0;
vx(i) = 0;
elseif ii>1 && ii<=7
vz(i) = v;
vy(i) = -v0(ii)*cosd(data(ii));
vx(i) = v0(ii)*cosd(90-data(ii));
elseif ii>7 && ii<=10
vz(i) = v;
vy(i) = -v0(ii)*cosd(data(ii));
vx(i) = v0(ii)*cosd(90-data(ii));
end
while t < 500e-4
dvz(i) = (-1/(2*m))*r*S*v*vz(i);
dz(i) = vz(i);
vz(i+1) = vz(i) + dvz(i) * dt;
z(i+1) = z(i) + dz(i) * dt;
dvy(i) = (-1/(2*m))*r*S*v*vy(i)-g;
dy(i) = vy(i);
vy(i+1) = vy(i) + dvy(i) * dt;
y(i+1) = y(i) + dy(i) * dt;
dvx(i) = (-1/(2*m))*r*S*v*vx(i);
dx(i) = vx(i);
vx(i+1) = vx(i) + dvx(i) * dt;
x(i+1) = x(i) + dx(i) * dt;
t(i+1) = t(i) + dt;
i=i+1;
max_values(ii,1)=max(x(i));
max_values(ii,2)=max(y(i));
max_values(ii,3)=max(z(i));
end
end

댓글 수: 4

Alexander
Alexander 2024년 3월 30일
I don't know what is x, y or z and I don't know what is i and t. Hence, it is impossible to run your code and I don't want to guess what you want. Please supply some numbers to get a script which is showing the problem you have.
@Alexander, I feel here the issue in within the scope of the variables, they are not being reset for each iteration of 'ii', which means the data from the previous iterations are being carried over into the next ones. This should be why only the results from the first iteration are seen.
Can initialise these variables inside the 'ii' loop:
d = 2;
v=700;
om = 500;
dp=5;
dC=20;
m = 1;
S = pi * ((d^2) / 4);
r = 1.500;
g=8;
dt = 1e-5;
max_values=zeros(10,3);
v0=zeros(1,10);
numb = [ 0 0 0 0;1 1 1 1; 2 2 2 2;3 3 3 3;4 4 4 4;5 5 5 5;6 6 6 6;7 7 7 7;8 8 8 8;9 9 9 9];
for ii = 1:10
i = 1;
t = zeros(1, 50000);
t(i) = 0;
x = zeros(1, 50000);
y = zeros(1, 50000);
z = zeros(1, 50000);
vx = zeros(1, 50000);
vy = zeros(1, 50000);
vz = zeros(1, 50000);
x(i)=numb(ii,1);
y(i)=numb(ii,2);
z(i)=numb(ii,3);
v0(ii) = om * numb(ii,4) * 2 * pi;
data = [0 0 300 240 180 120 60 0 330 300];
if ii==1
vz(i) = v;
vy(i) = 0;
vx(i) = 0;
elseif ii>1 && ii<=7
vz(i) = v;
vy(i) = -v0(ii)*cosd(data(ii));
vx(i) = v0(ii)*cosd(90-data(ii));
elseif ii>7 && ii<=10
vz(i) = v;
vy(i) = -v0(ii)*cosd(data(ii));
vx(i) = v0(ii)*cosd(90-data(ii));
end
while t(i) < 500e-4
dvz(i) = (-1/(2*m))*r*S*v*vz(i);
dz(i) = vz(i);
vz(i+1) = vz(i) + dvz(i) * dt;
z(i+1) = z(i) + dz(i) * dt;
dvy(i) = (-1/(2*m))*r*S*v*vy(i)-g;
dy(i) = vy(i);
vy(i+1) = vy(i) + dvy(i) * dt;
y(i+1) = y(i) + dy(i) * dt;
dvx(i) = (-1/(2*m))*r*S*v*vx(i);
dx(i) = vx(i);
vx(i+1) = vx(i) + dvx(i) * dt;
x(i+1) = x(i) + dx(i) * dt;
t(i+1) = t(i) + dt;
i=i+1;
max_values(ii,1)=max(x);
max_values(ii,2)=max(y);
max_values(ii,3)=max(z);
end
end
This way, for iternation of 'ii' it starts with fresh variables, and the results from the previous iterations won't affect the current one.
Alexander
Alexander 2024년 3월 31일
So I think you are pleased now.
Daniel J
Daniel J 2024년 3월 31일
Sorry for late response, indeed now it works as inteded. Thank you so much and happy easter!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 3월 30일

댓글:

2024년 3월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by