Accumulating data in a loop while

6 views (last 30 days)
gravy
gravy on 29 Jan 2024
Commented: gravy on 29 Jan 2024
Good evening, I need to collect data into an array with accumulation.
For example, there are 9 time values t=9, the array should look like this (0 t1 t1+t2 t1+t2+t3 and...) I want to do this in a loop so that it would be possible to regulate the number of t values.
So far I've thought of this, but it doesn't work correctly:
index=1;
n=10;
c=[];
while index<10
t = b(index)+b(index+1)
if t>b(index) && index<10
t1=0;
t1 = t + b(index)
index=index+1;
end
c=[c t1];
end
Unrecognized function or variable 'b'.
c
  3 Comments
gravy
gravy on 29 Jan 2024
"b" this is part of the previous code
gravy
gravy on 29 Jan 2024
Edited: Walter Roberson on 29 Jan 2024
This is what it looks like in simple form
%t0=0;
%t1=delt0;
%t2=delt0+delt1;
%t3=delt0+delt1+delt2;
%t4=delt0+delt1+delt2+delt3;
%t5=delt0+delt1+delt2+delt3+delt4;
%t6=delt0+delt1+delt2+delt3+delt4+delt5;
%t7=delt0+delt1+delt2+delt3+delt4+delt5+delt6;
%t8=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7;
%t9=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7+delt8;
%t10=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7+delt8+delt9;
%t = [t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10];
I want to automate this process and not write new variables myself every time.
Here is the full code, maybe it will become clearer:
X = linspace(0,100,12);
for x = X
if (0<=x) && (x<5.5)
y1=((x-0)/(5.5-0))*(7.74-9.19)+9.19;
end
if (5.5<=x) && (x<14.5)
y2=((x-5.5)/(14.5-5.5))*(6.45-7.74)+7.74;
end
if (14.5<=x) && (x<22.5)
y3=((x-14.5)/(22.5-14.5))*(5.56-6.45)+6.45;
end
if (22.5<=x) && (x<30)
y4=((x-22.5)/(30-22.5))*(4.79-5.56)+5.56;
end
if (30<=x) && (x<43.5)
y5=((x-30)/(43.5-30))*(3.43-4.79)+4.79;
end
if (43.5<=x) && (x<49)
y6=((x-43.5)/(49-43.5)).*(3.04-3.43)+3.43;
end
if (49<=x) && (x<56)
y7=((x-49)/(56-49))*(2.39-3.04)+3.04;
end
if (56<=x) && (x<64)
y8=((x-56)/(60-56)).*(0.8-2.39)+2.39;
end
if (64<=x) && (x<73)
y9=((x-60)/(70-60))*(-1.98-0.8)+0.8;
end
if (73<=x) && (x<83)
y10=((x-70)/(80-70))*(-3.88+1.98)-1.98;
end
if (83<=x) && (x<93)
y11=((x-80)/(90-80))*(-5.45+3.88)-3.88;
end
if (93<=x) && (x<=100)
y12=((x-90)/(100-90))*(-6.74+5.45)-5.45;
end
end
Y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12];
X = linspace(0,100,12);
v = [ 0:10:100 ];
delv=10;
dzeta = 120;
w0=0;
v1=0;
a=[];
while v1<100 %&& w0<3
fycp = abs(((interp1(X,Y,v1)-w0)+((interp1(X,Y,v1+delv))-w0)))/2;
v1=v1+10;
% w0=w0+1;
a=[a fycp];
end
b=[];
index=1;
while index<11
delt = delv/(dzeta*a(index));
index=index+1;
b=[b delt];
end
index=1;
n=10;
c=[];
while index<10
t = b(index)+b(index+1)
if t>b(index) && index<10
t1=0;
t1 = t + b(index)
index=index+1;
end
c=[c t1];
end

Sign in to comment.

Accepted Answer

Voss
Voss on 29 Jan 2024
t = rand(1,9) % 9 random t values
t = 1×9
0.7787 0.5803 0.4362 0.3903 0.0329 0.8313 0.1897 0.7956 0.7963
c = cumsum([0 t]) % c = [0, t(1), t(1)+t(2), t(1)+t(2)+t(3), etc.]
c = 1×10
0 0.7787 1.3590 1.7951 2.1854 2.2183 3.0496 3.2394 4.0349 4.8312
  4 Comments
gravy
gravy on 29 Jan 2024
Edited: gravy on 29 Jan 2024
still not the same, it should look like you wrote before:
t = rand(1.9) % 9 random t values
c = cumsum([0 t]) % c = [0, t(1), t(1)+t(2), t(1)+t(2)+t(3), etc.]
But the summation in the array should be done automatically, I want to change the value of t, but not rewrite the array every time. There will be a graph with "b" and "c" and their number in the array should converge.
I tried to set some kind of condition, but I couldn’t figure it out yet.
gravy
gravy on 29 Jan 2024
I'm an idiot, your previous answer was correct. Thanks a lot

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!