- 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.
Generate repeated sequence block uisng matlab code
7 views (last 30 days)
Show older comments
Hello, please I want to generate repeated sequence plot using MATLAB codes but not simulink. I have written the code and the interpolation. However, I think my 'for loop' to pick all the points in the sequence is not capturing all. I have given the scripts below and I have attached one of the similar plots I have genrated using SIMULINK's repeating sequence block.
dt=1e-4;
T = 200;
n0=1;
time=0:dt:T;
nt=length(time);
ref=zeros(nt,1);
%time_point=[0 100 700 1000 1300 1600 1780 2080 2260 2560 2680 2980 3040 3340 3400 3700]
%pow_point=[1 1 0.9 0.9 0.8 0.8 0.65 0.65 0.95 0.95 0.55 0.55 0.15 0.15 0.95 0.95]
%time_point=[0 20 30 50 60 80 90 110 130 150];%work with only this time
%time_point=[0 10 40 70 80 100 110 130 140 150];
%time_point=[0 30 40 50 60 70 90 100 140 150];
time_point=[0 20 30 50 60 80 90 110 130 200];
%pow_point=[0.8 0.8 0.4 0.4 0.8 0.8 0.4 0.4 0.8 0.8];power Transient
%pow_point=[1 1 0.4 0.4 1 1 0.4 0.4 1 1];
%pow_point=[1 1 0.4 0.4 0.6 0.6 0.8 0.8 1 1];
%pow_point=[0.9 0.9 0.3 0.3 0.9 0.9 0.3 0.3 0.9 0.9];
pow_point=[0.7 0.7 0.4 0.4 0.4 0.4 0.6 0.6 0.7 0.7];
%pow_point=[0.5 0.5 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1];
ref_old=pow_point(1);
for it=1:nt %to vectorize nt or make each point of nt
if(it>1)
time(it)=time(it-1)+dt;%to be able to make equal time step of the time
ref_old=ref(it-1); %for each time slot
end
ref(it)=ref_old;
i1=1;
i2=1;
for ii=1:length(time_point)-1
if(time_point(ii)<=time(it) && time(it)<=time_point(ii+1))
i1=ii;
i2=ii+1;
frac1=(time_point(ii+1)-time(it))/(time_point(ii+1)-time_point(ii));
frac2=1.0-frac1;
ref(it)=frac1*pow_point(i1)+frac2*pow_point(i2);
break
end
end
end
ref(:)=ref(:);
yref=(ref.*n0);
for s =1:nt
setpoint=yref(s);
end
0 Comments
Accepted Answer
Yash
on 21 Mar 2024
Edited: Yash
on 21 Mar 2024
Hi Kamal,
There are a few issues and improvements in your code to generate repeated sequence block's functionality:
Given these observations, here is a simplified version of the original code that should accomplish the objective:
dt = 1e-4;
T = 200;
n0 = 1;
time = 0:dt:T;
nt = length(time);
ref = zeros(nt, 1);
% Define the time points and corresponding power values
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)
% Find the indices in 'time' that correspond to the current and next time points
startIndex = find(time >= time_point(i), 1, 'first');
endIndex = find(time < time_point(i + 1), 1, 'last');
% Check if interpolation is necessary
if pow_point(i) == pow_point(i + 1)
% Direct assignment if the power values are the same
ref(startIndex:endIndex) = pow_point(i);
else
% Linear interpolation when the power values differ
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;
end
end
end
% Handle the case for the last interval if needed
if time_point(end) <= T
lastIndex = find(time >= time_point(end), 1, 'first');
ref(lastIndex:end) = pow_point(end);
end
yref = ref.*n0; % Apply scaling
% Plot the result
figure;
plot(time, yref);
xlabel('Time');
ylabel('Power');
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!