Can I link optimization code with Simulink file?

I want to link my m.file with simulink file to cut the complex equations and codes.
I just want to know, Can I define my specific parameter as variables and link the file with smulink?
cost =zeros(size(popmatrix,1),1);
for ii= 1:size(popmatrix)
P=popmatrix(ii,2);
I=popmatrix(ii,1);
LowerBand = [-400 -400];
UpperBand = [400 400];
Xls=.19;
Rs=.003;
Xd=1.8;
Rfd=.000929;
..
sim('PMSM')

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2023 年 5 月 11 日

0 投票

In the most common case, you run the code above, all the variables will be created in the base workspace. If your Simulink model PMSM uses any of these variables in its block diagram, then, Yes, the Simulink model will use the updated variable value for its simulation. In your word, they are "linked".

12 件のコメント

Morteza Moslehi
Morteza Moslehi 2023 年 5 月 11 日
Dear Fangjun Jiang
Thank you to reply me. yes, at first it does, but after changing them I encounter with this code:
Error using My_Model_P
Error evaluating parameter 'P' in 'MyModel/PID Controller'
Caused by:
Error using My_Model_P
Unrecognized function or variable 'P'. - Show complete stack trace
Error using My_Model_P
Variable 'P' has been deleted from base workspace.
Suggested Actions:
• Undo the deletion. - Fix
• Load a file into base workspace. - Fix
• Create a new variable. - Fix
- Show complete stack trace
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 11 日
You might have command like "clear" somewhere else that clears the variable. For this error, go to MATLAB Command Window and type "P", you should be able to verify whether variable "P" exists or not in base workspace.
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 11 日
I define the workspace from here. When I define workspace I will encounter with not enough input arguments. Is there another way to define my workspace?
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 11 日
Is your code a function? For this error, go to MATLAB Command Window, Type Kc or E if there are values. If not, that is the problem.
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 11 日
yes, at first, i give them measure and run. it works correctly.
but when I put them in the function of Optimization and run, it gives me error. I defined the file in the model workspace part, and reinitialize from source, it gives me again the error of Not enough input arguments.
is it possible to send you my code here or email for you to check it ?
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 11 日
"I defined the file in the model workspace part". It seems that a few things are not clear to you.
base workspace, model workspace, function workspace
If your code is inside a function, all variables are in function workspace. They are not accessible in base workspace and model workspace.
Most common case is to use base workspace. I assume you don't know what is model workspace from your statement above.
The simple solution is for you to create those variables in the base workspace and the problem will be resolved.
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 11 日
Would you please give me a link to read or watch that? or would you please give me an example to find out that?
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 12 日
Search those terms in doc
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 12 日
function workspace is in the model explorer?
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 12 日
I think, I found out that. I have to go under mask of PID block. But I go there but I really can't find specific part to upload my cost function file.
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 12 日
No need to touch the mask of the PID controller. You will be confused by yet another term: mask workspace.
Design your cost function as
function [Kc, E]=MyFun(x,y,z)
%some code here
end
And then run this in Command Window and you will get Kc and E in base workspace
[Kc, E]=MyFun(x,y,z)
Morteza Moslehi
Morteza Moslehi 2023 年 5 月 18 日
移動済み: Fangjun Jiang 2023 年 5 月 19 日
@Fangjun Jiang thank you so much to give your time to fix my code.
But, my problem was in the simulink, I had to put Fobj for controlling. Thank you so much for your kind again.

サインインしてコメントする。

Morteza Moslehi
Morteza Moslehi 2023 年 5 月 12 日

0 投票

Can I do it by Matlab?
function cost = Curve_Pth_Pattern1_P_I(popmatrix)
global LowerBand UpperBand
for ii = 1:length(LowerBand)
popmatrix(:,ii) = LowerBand(ii)+(UpperBand(ii)-LowerBand(ii))*popmatrix(:,ii);
end
% change the bounds of x from 0<=x<=1 to a<=x<=b
cost =zeros(size(popmatrix,1),1);
for ii= 1:size(popmatrix)
P=popmatrix(ii,2);
I=popmatrix(ii,1);
LowerBand = [1 1];
UpperBand = [4 10];
%equations
sim('PMSM')
end

2 件のコメント

Morteza Moslehi
Morteza Moslehi 2023 年 5 月 18 日
移動済み: Fangjun Jiang 2023 年 5 月 19 日
FIrst, We have to descibe a block as objective function to model and make a circle in the simulink and after that define m.files for parameters as workspace. it is completely okay.
Fangjun Jiang
Fangjun Jiang 2023 年 5 月 19 日
"descibe a block as objective function", "make a circle in the simulink"
I am sorry I couldn't understand these. Now I think I didn't understand your question at all.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSimulink Design Optimization についてさらに検索

製品

リリース

R2022b

質問済み:

2023 年 5 月 11 日

コメント済み:

2023 年 5 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by