Can I link optimization code with Simulink file?

조회 수: 3 (최근 30일)
Morteza Moslehi
Morteza Moslehi 2023년 5월 11일
댓글: Fangjun Jiang 2023년 5월 19일
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일
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
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일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by