7 views (last 30 days)
Mike Buba on 17 Mar 2024
Answered: Namnendra on 24 Jul 2024 at 19:24
Are there any MATLAB Simulink optimization examples I could look at to get a general understanding of the method and the process?
For example, the extremely simplified version of the problem I am trying to solve, and maybe use & learn optimization while doing it, is: for a voltage signal, find the optimal offset value so that after the PWM Generator block, the PWM and are equal for the each switching period.
So, I know that for the PWM signal and , duration to be the same, the PWM Generator input signal should be half of the counter value, i.e., offset = - signal.
But how would I get to this conclusion using the optimization method and PWM signals?
A more complex version of the problem is where I have several input signals and now the sum of the PWM signals should be minimum. And I need to calculate the optimum common offset value for all the signals.

Namnendra on 24 Jul 2024 at 19:24
Hi Mike,
Optimization in MATLAB and Simulink can be a powerful way to solve complex engineering problems, including those involving PWM signals. To get started with optimization in Simulink, you can use the Optimization Toolbox and Simulink Design Optimization. Here, I'll walk you through a simplified version of your problem and provide some resources and examples to help you understand the process.
Simplified Problem: Finding Optimal Offset for PWM Signals
Problem Statement
You have a voltage signal, and you want to find the optimal offset value so that after passing through a PWM Generator block, the PWM signal's "on" and "off" durations are equal for each switching period.
Steps to Solve Using Optimization
1. Define the Model:
- Create a Simulink model with a PWM Generator block.
- Add a block for the voltage signal input.
- Add a block to apply the offset to the voltage signal.
- Add a block to calculate the difference between the "on" and "off" durations of the PWM signal.
2. Set Up the Optimization Problem:
- Define the objective function, which in this case is to minimize the difference between the "on" and "off" durations.
- Define the optimization variable, which is the offset value.
3. Run the Optimization:
- Use the Optimization Toolbox to find the optimal offset value.
Here is a step-by-step guide to creating a simplified Simulink model for this problem:
sim('new')
- Voltage Signal Input: Use a `Sine Wave` block or a `Constant` block.
- Offset Block: Use a `Sum` block to add the offset to the voltage signal.
- PWM Generator Block: Use a `PWM Generator` block from the Simulink library.
- Difference Calculation: Use a `MATLAB Function` block or a combination of `Compare To Constant` and `Sum` blocks to calculate the difference between the "on" and "off" durations.
3. Connect Blocks:
- Connect the voltage signal to the `Sum` block.
- Connect the output of the `Sum` block to the `PWM Generator` block.
- Connect the output of the `PWM Generator` block to the `MATLAB Function` block for difference calculation.
2. Set Up the Optimization Problem
1. Define the Objective Function:
Create a new MATLAB script to define the objective function. This function will simulate the model and calculate the difference between "on" and "off" durations.
function diff = objectiveFunction(offset)
% Set the offset value in the model
set_param('your_model_name/OffsetBlock', 'Value', num2str(offset));
% Run the simulation
simOut = sim('your_model_name', 'SimulationMode', 'normal');
% Extract the PWM signal
pwmSignal = simOut.get('yout').signals.values;
% Calculate the "on" and "off" durations
onDuration = sum(pwmSignal == 1);
offDuration = sum(pwmSignal == 0);
% Calculate the difference
diff = abs(onDuration - offDuration);
end
2. Run the Optimization:
Use the `fmincon` function from the Optimization Toolbox to find the optimal offset value.
% Initial guess for the offset
initialOffset = 0;
% Set optimization options
options = optimoptions('fmincon', 'Display', 'iter');
% Run the optimization
optimalOffset = fmincon(@objectiveFunction, initialOffset, [], [], [], [], -inf, inf, [], options);
% Display the optimal offset value
disp(['Optimal Offset: ', num2str(optimalOffset)]);
Resources and Examples
Example: More Complex Problem
For a more complex problem where you have several input signals and you need to find a common offset that minimizes the sum of the PWM signals, you can follow a similar approach but with a more sophisticated objective function.
sim('new')
- Multiple Voltage Signal Inputs: Use multiple `Sine Wave` or `Constant` blocks.
- Offset Block: Use multiple `Sum` blocks to add the common offset to each voltage signal.
- PWM Generator Blocks: Use multiple `PWM Generator` blocks.
- Sum of PWM Signals: Use a `Sum` block to add the outputs of all PWM Generator blocks.
3. Connect Blocks:
- Connect each voltage signal to a corresponding `Sum` block.
- Connect the common offset to each `Sum` block.
- Connect the outputs of the `Sum` blocks to the `PWM Generator` blocks.
- Connect the outputs of the `PWM Generator` blocks to the `Sum` block for summing the PWM signals.
2. Set Up the Optimization Problem
1. Define the Objective Function:
Create a new MATLAB script to define the objective function. This function will simulate the model and calculate the sum of the PWM signals.
function sumPWM = objectiveFunction(offset)
% Set the offset value in the model
set_param('your_model_name/OffsetBlock', 'Value', num2str(offset));
% Run the simulation
simOut = sim('your_model_name', 'SimulationMode', 'normal');
% Extract the PWM signals
pwmSignals = simOut.get('yout').signals.values;
% Calculate the sum of the PWM signals
sumPWM = sum(pwmSignals);
end
2. Run the Optimization:
Use the `fmincon` function from the Optimization Toolbox to find the optimal common offset value.
% Initial guess for the offset
initialOffset = 0;
% Set optimization options
options = optimoptions('fmincon', 'Display', 'iter');
% Run the optimization
optimalOffset = fmincon(@objectiveFunction, initialOffset, [], [], [], [], -inf, inf, [], options);
% Display the optimal offset value
disp(['Optimal Offset: ', num2str(optimalOffset)]);
Conclusion
By following these steps, you can set up and solve optimization problems in Simulink. The key steps involve defining the Simulink model, setting up the objective function for optimization, and using MATLAB's Optimization Toolbox to find the optimal parameters.
For more complex problems, you can extend the basic approach by adding more input signals and defining a more sophisticated objective function that captures the desired optimization criteria. The resources provided will help you deepen your understanding and apply optimization techniques to a wide range of problems in Simulink.
I hope the above information helps you.
Thank you.

### Categories

Find more on Signal Generation 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!