im using Matlab function in Simulink and use data store memory to access the simulation time and implement something according to the time
13 views (last 30 days)
Show older comments
So i build my code to control a buck boost converter by the matlab function i have four inputs a simulation time using clock block , Dsw4 which is a data store write and also lastUpdateTime which is also data store write so, in my code im trying when the simulation time reach the 0.08 and excute the else if statment as shown in the code to make step in Dsw4 acoording to the simulation time which mean every 16mili second the dsw4 increase step so, to know the lastupdateTime also and Dsw4 i used a data store read block but when i use a display to show the values of the data store reads and data store writes the value does not change and i also connect them with data inspector to check if the value of them change with the time and they not change and i tried to solve but i dont know where the problem
the code will be attach and a screen shot for matlab function connection:
function [voltage1, voltage2, voltage3, voltage4, lastUpdateTimeOut, Dsw4Out] = pulseGenerator(counter, t, lastUpdateTime, Dsw4)
% Parameters
frequency = 100e3; % 100 kHz
amplitude = 6; % Amplitude of the pulse
voltage1 = -3; % Initialize outputs
voltage2 = -3; % Initialize other outputs
voltage3 = -3;
voltage4 = -3;
% Calculate period in terms of simulation steps
period1 = round(1 / frequency * 1e6); % Convert to microseconds
period3 = round(1 / frequency * 1e6); % Same period for the second condition
% Adjust duty cycle based on simulation time
if 0 <= t && t < 0.035
Dsw1 = 0.25; % Duty cycle for voltage1
if mod(counter, period1) < Dsw1 * period1
voltage1 = amplitude; % High state
else
voltage1 = -3; % Low state
end
elseif 0.035 <= t && t < 0.08
Dsw1 = 0.5; % Duty cycle for voltage1
if mod(counter, period3) < Dsw1 * period3
voltage1 = amplitude; % High state
else
voltage1 = -3; % Low state
end
elseif 0.08 <= t && t < 0.1
Dsw1 = 0.5; % Duty cycle for voltage1
if mod(counter, period1) < Dsw1 * period1
voltage1 = amplitude; % High state
else
voltage1 = -3; % Low state
end
% Gradually increase Dsw4 from 0 to 0.88 in 5 steps
if (t - lastUpdateTime) >= 0.016 % Check if 16 ms has passed
lastUpdateTimeOut = t; % Update last update time
Dsw4Out = min(Dsw4 + 0.176, 0.88); % Update Dsw4 in steps
else
lastUpdateTimeOut = lastUpdateTime; % No update
Dsw4Out = Dsw4; % No update
end
if mod(counter, period1) < Dsw4Out * period1
voltage4 = amplitude; % High state
else
voltage4 = -3; % Low state
end
elseif 0.1 <= t && t < 0.15
voltage4 = -3;
voltage1 = -3;
elseif 0.15 <= t && t < 0.4
voltage1 = -3;
voltage4 = -3;
Dsw3 = 0.24; % Duty cycle for voltage3
if mod(counter, period1) < Dsw3 * period1
voltage3 = amplitude; % High state
else
voltage3 = -3; % Low state
end
else
voltage1 = 0; % Initialize outputs
voltage2 = 0; % Initialize other outputs
voltage3 = 0;
voltage4 = 0;
end
% Outputs for Data Store Write
lastUpdateTimeOut = lastUpdateTime; % Output updated lastUpdateTime
Dsw4Out = Dsw4; % Output updated Dsw4
end

6 Comments
Walter Roberson
on 20 Dec 2024
Suppose you are "at" a certain time and state, and suppose you are using continuous time. Then the ode15s, ode45, or ode23t integrator will probe around at a number of different times and states, trying to estimate the best new state. When you are near one of the breakpoint times such as near the end of 0.1 <= t && t < 0.15 then some of the locations probed will be inside the time band, but some of the probed times will be inside 0.15 <= t && t < 0.4. The function will react differently, non-continuously between the two bands. Because the mathematics of RK calculations requires continuous second derivatives, the resulting estimated location will be wrong. If you are lucky then the integrator will notice and complain about being unable to integrate at that time. If you are not lucky then the integrator will silently accept the incorrect location and proceed as-if it were correct. At the very least, the waveform will be wrong near the boundaries; at worst the calculated location might be non-physical, somewhere that is not physically realistic.
You can reduce the danger by configuring a fixed-step integrator https://www.mathworks.com/help/simulink/ug/fixed-step-solvers-in-simulink.html -- but you will still get wrong results every time the starting time is on one side of a time boundary and the new step is on the other side of the time boundary. You can remove that effect if you ensure that the fixed step size is the greatest common divisor (GCD) of the time ranges (end time minus start time of the various bands)
Paul
on 20 Dec 2024
Edited: Paul
on 20 Dec 2024
I would think that a model of an electrical system is best simulated with a continuous solver. If the pulseGenerator block is running with a discrete sample time, then the continuous solver should have a variable step size, or fixed step size that that is an integer divide down of the pulse generator sample time (assuming no other discrete blocks in the model). If the pulseGenerator has to run with a continuous sample time then it might be best to bite the bullet and implement that logic in native blocks that can operate with and take advantage of Simulink's zero crossing detection.
Answers (0)
See Also
Categories
Find more on Analog Input and Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!