Hi Kamal,
There are a few issues and improvements in your code to generate repeated sequence block's functionality:
- The line "time(it)=time(it-1)+dt;" inside the loop is unnecessary because you've already defined your "time" array using "time=0:dt:T;".
- The line "ref_old=pow_point(1);" and the subsequent use of "ref_old" inside the loop are unnecessary since you're already handling the assignment of "ref(it)" within the loop.
- When two consecutive "pow_point" values are the same, there's no need for interpolation, and we can assign the value directly to the entire interval between the two consecutive "time_point" corresponding to the same "power_point".
- The final loop that sets "setpoint=yref(s);" doesn't seem to serve a purpose in the context of generating the "ref" sequence, as "setpoint" is overwritten at every iteration and not used elsewhere.
Given these observations, here is a simplified version of the original code that should accomplish the objective:
time_point = [0 10 40 70 80 100 110 130 140 150];
pow_point = [0.9 0.9 0.3 0.3 0.9 0.9 0.3 0.3 0.9 0.9];
for i = 1:(length(time_point) - 1)
startIndex = find(time >= time_point(i), 1, 'first');
endIndex = find(time < time_point(i + 1), 1, 'last');
if pow_point(i) == pow_point(i + 1)
ref(startIndex:endIndex) = pow_point(i);
for it = startIndex:endIndex
frac = (time(it) - time_point(i)) / (time_point(i + 1) - time_point(i));
ref(it) = pow_point(i) * (1 - frac) + pow_point(i + 1) * frac;
lastIndex = find(time >= time_point(end), 1, 'first');
ref(lastIndex:end) = pow_point(end);