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

59 次查看(过去 30 天)
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

采纳的回答

Saurav
Saurav 2024-12-24,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 个评论
Saurav
Saurav 2024-12-27,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:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Model Predictive Control Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by