Populating a vector between upper and lower bound?

9 次查看(过去 30 天)
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
Chunru 2021-11-27
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
  1 个评论
raymond bryant
raymond bryant 2021-11-27
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by