Linearization of non-linear model
3 views (last 30 days)
Show older comments
I was trying to linearize a non-linear model of an invterted pendulum and i used the following method :
created a S-function file and then used simulink to construct the model
then used linearize command and got the requuired ABCD matrices
but when i tried the same thing on a different model with more number of states , i am not getting the expected result ,
i'll attach both the simulink and S function files ,
And here is the S function code:.
function [sys,x0,str,ts] = myfunc(t,x,u,flag)
% MYFUNC An example M-file S-function for defining a
% nonlinear time-varying state space system of the form:
%
% dx/dt = f(x,u,t)
% y = g(x,u,t)
%
% See sfuntmpl.m for a general S-function template.
% See csfunc.m for a linear system implementation.
% Edited version of csfunc.m
% Copyright (c) 1990-97 by The MathWorks, Inc.
% $Revision: 1.4 $
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1
sys=mdlDerivatives(x,u,t);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3
sys=mdlOutputs(x,u,t);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 2, 4, 9 }
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end myfunc
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2; % ENTER state dimension
sizes.NumOutputs = 1; % ENTER output dimension
sizes.NumInputs = 1; % ENTER input dimension
sizes.NumDiscStates = 0;
sizes.DirFeedthrough = 0; %this will always be zero
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
ts = [0 0];
x0 = [0,0]; % ENTER initial conditions
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(x,u,t)
% syms a b c m l k
l = 1;
k = 0.9;
a = 9.8/l;
m = 1;
b = k/m;
c = 143;
%c = 1/(m*l^2);
x1dot = x(2);
x2dot = -a*sin(x(1)+pi) -b*x(2) + c*u;
sys = [x1dot,x2dot];
% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(x,u,t)
sys = x(1); % ENTER output equation
% end mdlOutputs
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/455164/image.png)
For my second model ,i'll just post the S-fucntion code as the simulink model is the same:
function [sys,x0,str,ts] = magsuspension(t,x,u,flag)
% MYFUNC An example M-file S-function for defining a
% nonlinear time-varying state space system of the form:
%
% dx/dt = f(x,u,t)
% y = g(x,u,t)
%
% See sfuntmpl.m for a general S-function template.
% See csfunc.m for a linear system implementation.
% Edited version of csfunc.m
% Copyright (c) 1990-97 by The MathWorks, Inc.
% $Revision: 1.4 $
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1
sys=mdlDerivatives(x,u,t);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3
sys=mdlOutputs(x,u,t);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 2, 4, 9 }
sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end myfunc
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 3; % ENTER state dimension
sizes.NumOutputs = 1; % ENTER output dimension
sizes.NumInputs = 1; % ENTER input dimension
sizes.NumDiscStates = 0;
sizes.DirFeedthrough = 0; %this will always be zero
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
ts = [0 0];
x0 = [0,0,0]; % ENTER initial conditions
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(x,u,t)
m = 0.1;
k = 0.001;
g = 9.81;
a = 0.05;
Lo= 0.01;
L1 = 0.02;
R = 1;
x1_dot = x(2);
x2_dot = g - k*x(2)/m - (Lo*a*x(3)^2)/(2*m*(a+x(1)))^2;
x3_dot = (-R*x(3) + Lo*a*x(2)*x(3))*(L1 + Lo/(1+x(1)/a));
sys = [x1_dot,x2_dot,x3_dot];
% end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(x,u,t)
sys = x(1); % ENTER output equation
% end mdlOutputs
0 Comments
Answers (0)
See Also
Categories
Find more on Linearization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!