I would like to know if there is a way to use an script to run multiple AC sweep simulations in PLECS standalone.
I want to be able to run an AC sweep simulation for multiple changes of a parameter. For example, in the attached figure I have a an RC circuit and I am obtaining the frequency response for different capacitance values C=1,C=10, and C=100 (to get the attached figure I ran the simulation 3 times manually.)
I notice that there is an example called “Buck Converter with Parameter Sweep” which explains how to make the parameter change; therefore, I would like to know how to call the AC-sweep analysis directly from the script to make a parameter sweep while running multiple AC-sweeps for each case.
It looks like you have already found the “Frequency Response of Passive Circuit” demo model included with PLECS 4.4. Did you open the included simulation script? You can access it from the Simulation + Simulation Scripts menu.
The key command here is:
plecs('analyze', 'AC Sweep', simStruct); % start AC Sweep analysis
where ‘AC Sweep’ refers to the Description field of the preconfigured Analysis Tool. The scope data is read from a script.
Another good reference to refer to is the “Simulation Scripts in PLECS Standalone” section of the user manual. The “Starting an Analysis” section of the help describes an alternative to reading the scope data, where the transfer function is returned directly as an array.
I haven’t noticed that the example has the script with exactly what I needed.
I have an additional query. Is it possible to change the magnitude axis of the AC analysis scope to display results in linear format? It looks like the default configuration is in dB. Is there any way to change it?
Within PLECS there is not a way to change the magnitude scale to a linear format. You’ll have to use the return values from the plecs(‘analyze’,…) command. As of PLECS 4.4 you can now generate Octave plots from within PLECS, or you could save the results to a file for post processing in your tool of choice.
Here’s a sample script:
% parameter definitions, create simStruct with field 'ModelVars'
mdlVars = struct('R', 1, 'C', 100e-6, 'fc', 10e3);
simStruct = struct('ModelVars', mdlVars);
% clear all traces in frequency response plot window in the current model
plecs('scope', './Analyses/AC Sweep', 'ClearTraces');
fcValues = [10e2, 10e3, 10e4]; % cutoff frequencies
legendValues = {};
for ix = 1:length(fcValues)
simStruct.ModelVars.C = 1/(2*pi*simStruct.ModelVars.R*fcValues(ix)); % RC filter capacitance
simResult=plecs('analyze', 'AC Sweep', simStruct); % start AC Sweep analysis
legendStr = ['fc=' mat2str(fcValues(ix)/1000) 'kHz'];
legendValues(ix) = legendStr;
plecs('scope', './Analyses/AC Sweep', 'HoldTrace', legendStr); % hold and label trace
subplot(2,1,1);
semilogx(simResult.F,abs(simResult.Gr+1i*simResult.Gi));
hold on;
subplot(2,1,2);
semilogx(simResult.F,angle(simResult.Gr+1i*simResult.Gi)*180/pi);
hold on;
end
% Label plots
subplot(2,1,1);
title('Example of Bode Plot with Linear Magnitude');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend(legendValues);
subplot(2,1,2);
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
legend(legendValues);
I’m facing a similar situation, where I have three different blocks (analogue transfer function, and two versions of it, one discretized by Tustin, other by MMPZ). and I want to dispaly their frequency response on the same Bode diagram, any advice on how to achieve that? many thanks in advance.
The above script should cover your use case. The only notable change is that you likely want to place the different blocks into a Configurable Subsystem and then use the simulation script to set the active configuration via the ModelVars option (as is done in the above script). Then you can hold the trace of each output. You could remove most of the plotting related code if you’re OK with the axes of the analysis tools scope windows.
The other advance since this post was made is the ability to have simulations run in parallel when using simulation scripts (including the various scripted analyses). I would recommend starting with a basic sequential script in your case, but this is something for future work.
If you get stuck you are welcome to post your model and script.
Many thanks for your support, while @Bryan_Lieblick response is quite informative and provides more detailed solution using scripts, for the current application on hand I used the solution proposed by @Munadir_Ahmed since it’s much faster to set up, and I don’t need more at the moment. Neverthelss, I’ll try to implement @Bryan_Lieblick approach, at least to have it ready for future reference.
I have attached my work in case you want to have a look, and for sure any comment or advice is highly appreciated.
After a second inspection at my work, it seems that I have another problem related with this post:
I’m defining the discrete controllers coefficients as global variables in the C-script, then when I perform the Frequency sweep analysis, the phase magnitude of the controllers (13~14 degrees) is way below the analogue counterpart (50 degree) at cross over frequency (8 kHz) . Is this due to the incorrect implemetation of the C code or did I mess something up in the Frequency Sweep configuration?