How to incorporate disturbances via scripts in MATLAB using a Model Predictive Controller.

72 views (last 30 days)
I have the code mentioned here, and I want to assign `u(4)` and `u(5)` to the measured disturbances (MD) in the same way that `u(1)`, `u(2)`, and `u(3)` were assigned to the manipulated variables (MV). However, I am encountering an error with the MD assignment. What could be the reason for this?
Code:
nlobj = nlmpc(3,2,'MV',[1 2 3],'MD',[4 5]);
nlobj.States(1).Name = 'SoCbat'; % Battery's state-of-charge
nlobj.States(2).Name = 'SoCsc'; % Super-capacitor's state-of-charge
nlobj.States(1).Units = '%';
nlobj.States(2).Units = '%';
nlobj.OV(1).Name = 'SoCbat';
nlobj.OV(2).Name = 'SoCsc';
nlobj.OV(1).Units = '%';
nlobj.OV(2).Units = '%';
nlobj.MV(1).Name = 'Pbat'; % super capacitor's power
nlobj.MV(2).Name = 'Psm'; % Synchronous machine's power
nlobj.MV(3).Name = 'Pload'; % Load demand
nlobj.MV(1).Units = 'kW';
nlobj.MV(2).Units = 'kW';
nlobj.MV(3).Units = 'kW';
nlobj.MD(1).Name = 'Ppv'; % Solar panel generated power
nlobj.MD(2).Name = 'Pwt'; % Wind turbine generated power
nlobj.MD(1).Units = 'kW';
nlobj.MD(2).Units = 'kW';
U(1) = nlobj.MV(1); % Pbat
U(2) = nlobj.MV(2); % Psm
U(3) = nlobj.MV(3); % Pload
U(4) = nlobj.MD(1); % Pbat

Accepted Answer

Saurav
Saurav on 24 Dec 2024 at 12:23
I understand that you are encountering an error with the MD assignment in your code above and would like to know the reason behind it.
The error you're encountering when assigning U(4) and U(5) to the measured disturbances (MD) is likely due to a mismatch in the structure types between the manipulated variables (MV) and the measured disturbances (MD). In MATLAB, when you assign different types or structures to elements of the same array, it results in a "Subscripted assignment between dissimilar structures" error.
  1. Initial Assignment:
  • When you assign nlobj.MV(1) to U(1), MATLAB initializes U as a structure array based on the structure of nlobj.MV(1). This works without issue because U is being defined for the first time.
2. Subsequent Assignments:
  • Assignments like U(2) = nlobj.MV(2); continue to work as long as the structures are the same.
3. Error with Different Structures:
  • The error occurs at U(4) = nlobj.MD(1); because nlobj.MD(1) might have a different structure than nlobj.MV(1). MATLAB requires all elements of a standard array to have the same structure.
To handle this, you should ensure that all elements in U are compatible. If nlobj.MV and nlobj.MD are fundamentally different structures, consider using a cell array instead. Cell arrays can hold different types of data:
% Use a cell array to store different structures
U = cell(1, 5);
U{1} = nlobj.MV(1); % Pbat
U{2} = nlobj.MV(2); % Psm
U{3} = nlobj.MV(3); % Pload
U{4} = nlobj.MD(1); % Ppv
U{5} = nlobj.MD(2); % Pwt
  • By using a cell array (U = cell(1, 5);), you can mix different structures without encountering errors.
  • Use curly braces (U{}) to assign and access elements in a cell array, allowing for flexible data storage.
Refer to the following documentation link to learn more about cell arrays:
This approach will enable you to assign both manipulated variables and measured disturbances without encountering structure-related errors.
I hope this helps.
  3 Comments
Saurav
Saurav on 27 Dec 2024 at 5:13
@jana nassereddine Ensure that you are using curly braces with U{}.If the issue arises in some other part of the code, consider using the function struct2cell to convert the structures into a cell array:

Sign in to comment.

More Answers (0)

Categories

Find more on Model Predictive Control Toolbox 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!