필터 지우기
필터 지우기

Run a simulink model from a nested function in matlab code and save data

조회 수: 1 (최근 30일)
Irene Fodale
Irene Fodale 2023년 8월 24일
답변: Ishu 2023년 8월 28일
Hello everyone!
I'm simulating an electric circuit model with Simulink/Simscape. I want to estimate the parameters of this circuit by comparing the real voltage (taken from measurements) with the simulated voltage. I measure the voltage with a voltage sensor and connect its output to a "To workspace". When I run the model with known values of the parameters, the vector V_sim is shown to the base workspace thanks to "To workspace" block.
My main problem is that in my code I use this nested function
function out = mySimulation(x)
assignin('base', 'R0', x(1));
assignin('base', 'R1', x(2));
assignin('base', 'R2', x(3));
assignin('base', 'R3', x(4));
assignin('base', 'C0', x(5));
assignin('base', 'C1', x(6));
assignin('base', 'C2', x(7));
assignin('base', 'C3', x(8));
out= sim('RC_3branch');
assignin('base', 'V_sim', V_sim);
end
The command "assignin('base', 'V_sim', V_sim);" allows the function to export "V_sim" from the local workspace to the base one.
I call the function "out=mySimulation(x)" in another nested function of the code that is my objective function, the one I want to minimize in order to define the right set of parameters. The function is:
function y = objectiveFunction(x,V_real)
out = mySimulation(x);
y= sum((V_sim - V_real).^2);
end
My initial idea was to call "out=mySimulation(x)" that, since how it is defined, should give as output "V_sim" and the object function should measure sum((V_sim - V_real).^2).
This is not happening, when the code arrives at objectiveFunction, it stops running because it does not find any "V_sim" variables.
I fixed it in this way:
function y = objectiveFunction(x,V_real)
out = mySimulation(x);
out= sim('RC_3branch');
assignin('base', 'V_sim', V_sim);
y= (sum((V_sim - V_real).^2))./length(V_real);
end
but I think it's just a waste of time because to me it seems it's redundant and makes the code less clear. Do you guys have any suggestions for a more intelligent solution?
Thank you very much!!

채택된 답변

Ishu
Ishu 2023년 8월 28일
Hi Irene,
When you simulate a model programmatically inside a function, logged data is returned in the workspace for the function. Further your code is returning the simulation results in a single output and are stored in variable “out”.
When simulation results are returned in a single output, the “SimulationOutput” object contains a variable for each To Workspace block in the model. Therefore, your results are stored in “out” variable and you access the “V_Sim” using the “out”.
You can access the data logged by a To Workspace block by following methods:
  1. Using dot “.” Operator
  2. Using get function
You can change the code in your mySimulation(x) function as:
out= sim('RC_3branch'); % stores your Simulation results in out
toWksData = get(out,"V_Sim"); % getting V_Sim from out
assignin("base","toWksData",toWksData);
For further information you can refer these documentations:
Hope it helps!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Circuit Envelope Simulation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by