Populating a vector between upper and lower bound?
5 次查看(过去 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
0 个评论
采纳的回答
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'
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!