Skip to content

Commit

Permalink
fix matlab code for linearization (#10403)
Browse files Browse the repository at this point in the history
* fix matlab code for linearization

* fix matlab formatting

* expected output
  • Loading branch information
arun3688 committed Mar 14, 2023
1 parent a9f1ae0 commit 80e3f4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -5212,7 +5212,7 @@ template functionlinearmodelMatlab(ModelInfo modelInfo, String modelNamePrefix)
<<
const char *<%symbolName(modelNamePrefix,"linear_model_frame")%>()
{
return "function [sys, x0, u0, n, m, p, Ts] = linearized_model()\n"
return "function [A, B, C, D, stateVars, inputVars, outputVars] = linearized_model()\n"
"%% <%modelNamePrefix%>\n"
"%% der(x) = A * x + B * u\n%% y = C * x + D * u\n"
" n = <%varInfo.numStateVars%>; %% number of states\n m = <%varInfo.numInVars%>; %% number of inputs\n p = <%varInfo.numOutVars%>; %% number of outputs\n"
Expand All @@ -5224,9 +5224,10 @@ template functionlinearmodelMatlab(ModelInfo modelInfo, String modelNamePrefix)
<%matrixB%>
<%matrixC%>
<%matrixD%>
" stateVars = {<%getVarNameMatlab(vars.stateVars, "x0")%>};\n"
" inputVars = {<%getVarNameMatlab(vars.inputVars, "u0")%>};\n"
" outputVars = {<%getVarNameMatlab(vars.outputVars, "y0")%>};\n"
" Ts = %g; %% stop time\n\n"
" %% The Control System Toolbox is required for this. Alternatively just return the matrices A,B,C,D instead.\n"
" sys = ss(A,B,C,D,'StateName',{<%getVarNameMatlab(vars.stateVars, "x")%>}, 'InputName',{<%getVarNameMatlab(vars.inputVars, "u")%>}, 'OutputName', {<%getVarNameMatlab(vars.outputVars, "y")%>});\n"
"end";
}
const char *<%symbolName(modelNamePrefix,"linear_model_datarecovery_frame")%>()
Expand Down Expand Up @@ -5334,7 +5335,7 @@ template getVarNameMatlab(list<SimVar> simVars, String arrayName) "template getV
<<
<%simVars |> var hasindex arrindex fromindex 1 => (match var
case SIMVAR(__) then
<<'<%arrayName%>_<%crefStrMatlabSafe(name)%>'>>
<<'<%crefStrMatlabSafe(name)%>'>>
end match) ;separator=","%>
>>
end getVarNameMatlab;
Expand Down
7 changes: 4 additions & 3 deletions testsuite/openmodelica/linearization/test_dump_languages.mos
Expand Up @@ -88,7 +88,7 @@ readFile("linearized_model.py"); getErrorString();
// end SimulationResult;
// "Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->Show additional information from the initialization process, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
// "
// "function [sys, x0, u0, n, m, p, Ts] = linearized_model()
// "function [A, B, C, D, stateVars, inputVars, outputVars] = linearized_model()
// % simple_test
// % der(x) = A * x + B * u
// % y = C * x + D * u
Expand All @@ -109,10 +109,11 @@ readFile("linearized_model.py"); getErrorString();
//
// D = [4.007476581092785];
//
// stateVars = {'num_x(1)','num_x(2)'};
// inputVars = {'u'};
// outputVars = {'y'};
// Ts = 0.5; % stop time
//
// % The Control System Toolbox is required for this. Alternatively just return the matrices A,B,C,D instead.
// sys = ss(A,B,C,D,'StateName',{'x_num_x(1)','x_num_x(2)'}, 'InputName',{'u_u'}, 'OutputName', {'y_y'});
// end"
// ""
// true
Expand Down

0 comments on commit 80e3f4b

Please sign in to comment.