Populating a vector between upper and lower bound?

조회 수: 5 (최근 30일)
raymond bryant
raymond bryant 2021년 11월 27일
댓글: raymond bryant 2021년 11월 27일
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에서 Nonlinear Dynamics에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by