# How to use script to run multiple AC sweep simulations?

**URL:** <https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879>\
**Category:** PLECS\
**Tags:** \#analysis\_tools\
**Created:** [October 18, 2020, 8:58pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879 "2020-10-18T20:58:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erick\_P](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@Erick\_P](https://forum.plexim.com/u/Erick_P)\
**Post date:** [October 18, 2020, 8:58pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/1 "2020-10-18T20:58:21Z")

</div>

Hello,

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.

Kind regards.

 ![AC sweep.PNG](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/1X/664527fe6d44bed7b48c0ebacd1b0932214574ce.png)

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [October 19, 2020, 11:09am UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/2 "2020-10-19T11:09:59Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Erick\_P](https://avatars.discourse-cdn.com/v4/letter/e/3da27b/32.png) [@Erick\_P](https://forum.plexim.com/u/Erick_P)\
**Post date:** [October 20, 2020, 10:02pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/3 "2020-10-20T22:02:42Z")

</div>

Thank you.

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?

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [October 21, 2020, 10:21am UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/4 "2020-10-21T10:21:08Z")

</div>

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:

```auto
% 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);

```

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [October 21, 2020, 10:21am UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/5 "2020-10-21T10:21:48Z")

</div>

Here’s the resulting output:

 ![61](https://europe1.discourse-cdn.com/flex006/uploads/plecs/original/1X/447f4f8e9618a57281bcb62d8295ada5ad18df53.jpeg)

---

<div class="post-metadata">

**Author:** ![Omar](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@Omar](https://forum.plexim.com/u/Omar)\
**Post date:** [September 19, 2026, 1:11pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/6 "2026-09-19T13:11:13Z")

</div>

Hi,

I have already posted my inquiry to the thread:

# **[Multiple AC sweep simulation script](https://forum.plexim.com/t/multiple-ac-sweep-simulation-script/1336)**

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.

Omar

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [September 22, 2026, 8:33pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/7 "2026-09-22T20:33:16Z")

</div>

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](https://docs.plexim.com/plecs/latest/components-by-category/conf_subsystem/#component-conf_subsystem) and then use the simulation script to set the active configuration via the [ModelVars](https://docs.plexim.com/plecs/latest/scripting/#scripted-simulation-and-analysis-options) 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.

---

<div class="post-metadata">

**Author:** ![Munadir\_Ahmed](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/munadir_ahmed/32/1540_2.png) [@Munadir\_Ahmed](https://forum.plexim.com/u/Munadir_Ahmed)\
**Post date:** [September 23, 2026, 1:07pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/8 "2026-09-23T13:07:51Z")

</div>

For a simple comparison of transfer functions you could utilize something like this:

[transferFunction.plecs](https://forum.plexim.com/uploads/short-url/sAu21eTAy4UoBPkdCjybfm8bOoE.plecs) (8.7 KB)

---

<div class="post-metadata">

**Author:** ![Omar](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@Omar](https://forum.plexim.com/u/Omar)\
**Post date:** [September 23, 2026, 10:51pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/9 "2026-09-23T22:51:12Z")

</div>

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.

[discreteFunctionFrequencySweep.plecs](https://forum.plexim.com/uploads/short-url/kviyM255kfTr2JWDdufRck17UPo.plecs) (29.7 KB)

Kind regards,

Omar

---

<div class="post-metadata">

**Author:** ![Omar](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@Omar](https://forum.plexim.com/u/Omar)\
**Post date:** [September 26, 2026, 12:49pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/10 "2026-09-26T12:49:27Z")

</div>

Hello @Bryan_Lieblick & @Munadir_Ahmed , again 😅

After a second inspection at my work, it seems that I have another problem related with this post:

> [@The difference between DiscState and global variables in a C script](https://forum.plexim.com/t/the-difference-between-discstate-and-global-variables-in-a-c-script/2319):
>
> I set the sampling time of the C script as Ts and implemented a simple discrete PI controller. For the previous error value, the example indicates that it should be defined using DiscState and updated within the update function. However, I chose to define the previous error as a global variable in the code declaration section. I found that when updating it in the update function, the global variable seemed to be reinitialized to zero. What could be the cause of this behavior? Theoretically, if t…

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?

Kind regards,

Omar

---

<div class="post-metadata">

**Author:** ![Bryan\_Lieblick](https://dub1.discourse-cdn.com/flex006/user_avatar/forum.plexim.com/bryan_lieblick/32/10_2.png) [@Bryan\_Lieblick](https://forum.plexim.com/u/Bryan_Lieblick)\
**Post date:** [September 29, 2026, 2:29pm UTC](https://forum.plexim.com/t/how-to-use-script-to-run-multiple-ac-sweep-simulations/879/11 "2026-09-29T14:29:02Z")

</div>

Hi Omar, please create a new thread with your questions. It is difficult to analyze the mismatch without your model.
