# Populating a vector between upper and lower bound?

9 views (last 30 days)
raymond bryant on 27 Nov 2021
Commented: raymond bryant on 27 Nov 2021
I'm trying to simulate a cyclic loading in Matlab by varying strain between a positive upper bound and negative lower bound.
The code below stops at the upper bound, but ignores the lower bound. Ideally, I would be able to set however many loops I want and get values that incrementally alternate between e_max and -emax.
For the code below, the vector values should have gone up to 4 and down to -4 several times. Instead they continue down to -16 after hitting the upper bound.
e = [0 1]; %Initial strain values
e_max = 4; %Max strain values
e_t = 1; %Strain rate
delta_t = 1; %Time interval
for i = 2:1:24
if e(i) < e_max && e(i) > e(i-1); %Increases strain as long as strain is below upper bound
e(end+1) = e(i) + e_t*delta_t;
else e(i) > -e_max && e(i-1) > e(i); %Decreases strain as long as strain is below upper bound
e(end+1) = e(i) - e_t*delta_t;
end
end
>> e
e =
0 1 2 3 4 3 2 1 0 -1 -2 -3 -4 -5
-6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16

Chunru on 27 Nov 2021
e0 = 0; % Initial strain values
e_max = 4; % Max strain values
inc = 1; % +ve increase, -ve decrease
e_t = 1; % Strain rate
delta_t = 1; % Time interval
n = 24;
e = zeros(n, 1);
e(1) = e0;
for i = 2:1:n
e(i) = e(i-1) + inc * e_t * delta_t;
if e(i) >= e_max
e(i) = e_max;
inc = -1;
elseif e(i)<=-e_max
e(i) = -e_max;
inc = 1;
end
end
e'
ans = 1×24
0 1 2 3 4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3 4 3 2 1
raymond bryant on 27 Nov 2021
e0 = 0; % Initial strain values
e_max = 0.01; % Max strain values
inc = 0.001; % +ve increase, -ve decrease
delta_t = 1; % Time interval
n = 50;
e = zeros(n, 1);
e(1) = e0;
for i = 2:1:n
e(i) = e(i-1) + inc * delta_t;
if e(i) >= e_max
e(i) = e_max;
inc = -0.001;
elseif e(i)<=-e_max
e(i) = -e_max;
inc = 0.001;
end
end

### Categories

Find more on General Applications in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!