I am using lsqnonlin in MATLAB to align a rigid skeleton model to raw joint center data recorded from a device that does not necessarily yield a rigid skeleton. Therefore, my objective function to be minimized is the distance between model joint locations and raw data joint locations.
My algorithm does a pretty good job (red is the rigid model and blue is the raw data):
except for some select frames where my skeleton is off:
I am interested in examining the solution process graphically so that I can visualize what lsqnonlin is trying to do for every frame. Ideally, I would output a History but this seems to be only available for other solvers.
For lsqnonlin it seems that I need to specify a plotting function for the PlotFcn option in optimoptions. I have extracted from the optimation Toolbox the function optimplotx.m which displays a bar graph of the state variables during the iteration process as lsqnonlin converges. My plan is to tweak this function to reproduce the plot for the red skeleton previously displayed.
My problem is that the quantity to be optimized is not the joint locations themselves, but joint angles. I have another function which does forward kinematics and gives me joint positions from joint angles. I would like to pass model parameters into optimplotx.m so that I may call my forward kinematics function and get the plot done but I am unsure how to do that.
These are the inputs to optimplotx.m:
It seems that somewhere deep in the Optimization Toolbox, MATLAB expects these inputs, and they are not to be changed. It is required that I call #optimplotx anonymously anyway, without passing in arguments. It seems that the only way I might be able to get around this is to make my model parameters global in both my main file and optimplotx. Is there any way around this?
I know there are several functions, such as aryule and arborg, which can estimate coefficients of AR models. But these functions cannot deal with AR models with input-output delay.
I also learned that the newest Matlab includes a function named 'arx' that can estimate AR parameters including the input-output delay. Unfortunately, the current version of Matlab I used is 2013a and I didn't find this 'arx' function inside.
I am wondering if anybody would kindly help me to tackle this issue.
Hopefully, I will be able to explain my question well.
I am working on Nonlinear model predictive control implementation.
I have got 3 files:
1). a simulink slx file which is basically a nonlinear pendulum model.
2). A function file, to get the cost function from the simulink model.
3). MPC code.
code snippet of cost function
**simOut=sim('NonlinearPendulum','StopTime', num2str(Np*Ts));**
%Linearly interpolates X to obtain sampled output states at time instants.
T=simOut.get('Tsim');
X=simOut.get('xsim');
xt=interp1(T,X,linspace(0,Np*Ts,Np+1))';
U=U(1:Nu);
%Quadratic cost function
R=0.01;
J=sum(sum((xt-repmat(r,[1 Np+1])).*(xt-repmat(r,[1 Np+1]))))+R*(U-ur)*...
(U-ur)';
Now I take this cost function and optimize it using fmincon to generate a sequence of inputs to be applied to the model, using my MPC code.
A code snippet of my MPC code.
%Constraints -1<=u(t)<=1;
Acons=[eye(Nu,Nu);-eye(Nu,Nu)];
Bcons=[ones(Nu,1);ones(Nu,1)];
options = optimoptions(#fmincon,'Algorithm','active-set','MaxIter',100);
warning off
for a1=1:nf
X=[]; %Prediction output
T=[]; %Prediction time
Xsam=[];
Tsam=[];
%Nonlinear MPC controller
Ubreak=linspace(0,(Np-1)*Ts,Np); %Break points for 1D lookup, used to avoid
% several calls/compilations of simulink model in fmincon.
**J=#(v) pendulumCostFunction(v,x0,ur,r(:,a1),Np,Nu,Ts);**
U=fmincon(J,U0,Acons,Bcons,[],[],[],[],[],options);
%U=fmincon(J,U0,Acons,Bcons);
U0=U;
UUsam=[UUsam;U(1)];%Apply only the first selected input
%Apply the selected input to plant.
Ubreak=[0 Ts]; %Break points for 1D lookup
U=[UUsam(end) UUsam(end)];
**simOut=sim('NonlinearPendulum','StopTime', num2str(Ts));**
In both the codes, I have marked the times we call our simulink model. Now, issue is that to run this whole simulation for just 5 seconds it takes around 7-8 minutes on my windows machine, MATLAB R2014B.
Is there a way to optimize this? As, I am planning to extend this algorithm to 9th order system unlike 2nd order pendulum model.
If, anyone has suggestion on using simulink coder to generate C code:
I have tried that, and the problem I face is that I don't know what to do with the several files generated. Please be as detailed as possible.
From the code snippets, it appears that you are solving a linear time invariant model with a quadratic objective. Here is some MATLAB (and Python) code for an overhead crane pendulum and inverted pendulum, both with state space linear models and quadratic objectives.
One of the ways to make it run faster is to avoid a Simulink interface and a shooting method for solving the MPC. A simultaneous method with orthogonal collocation on finite elements is faster and also enables higher index DAE model forms if you'd like to use a nonlinear model.
I know that usually you don't have local minima in the error surface using a perceptron (no hidden layers) with linear output. But is it possible to get stuck in local minima with a perceptron using a sigmoid function since it is not linear?
I'm using the functions.MultilayerPerceptron in WEKA (uses a sigmoid activation function and Backpropagation) with no hidden layers. I train it on a linearly separable dataset with 4 different classes. When I change the seed for the random generator (used for the initial weights of the nodes) most of the time it classifies only 60% right (it doesn't fully learn the target concept). But I found a specific seed where it classifies 90% right (which is the optimum). I already played with momentum, training time and learning rate but it doesn't change anything. It seems like it gets stuck in a local minimum..
or what else could be the explanation?
I'm thankful for any help
Simgoid activation function changes nothing, this is still a linear model. So there is no local optima. The only reason for wrong behavior is some weird stopping criterion and/or errors in data processing/methdo implementation.
I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).