Extended kalman filter jacobian function

16 views (last 30 days)
Camilla Ancona
Camilla Ancona on 26 Jul 2024
Answered: Rahul on 20 Aug 2024
I am trying to code my extended Kalman filter on a system and I know the function of the jacobian of the nonlinear output function. I read the documentation and look at the MeasurementJacobianFcn example, but I would need to pass to the function some parameters, is this possible?
  5 Comments
Camilla Ancona
Camilla Ancona on 29 Jul 2024
The following error shows when I try to use my custom function to evalueate the measurement jacobian
Error using matlabshared.tracking.internal.ExtendedKalmanFilter/validateMeasurementJacobianFcn (line 1990). The number of optional arguments in the call to residual does not match the number of additional arguments expected by the MeasurementJacobianFcn. Check that all additional arguments of MeasurementJacobianFcn are provided as optional input arguments the residual.
obj_p = extendedKalmanFilter(@my_sist_gradino_d,...
@my_volt_td_p,conc_in2(1:q,:),'ProcessNoise',0,'MeasurementNoise',varianza);
obj_p.MeasurementJacobianFcn = @pos_electrodeMeasurementJacobianFcn;
for k = 1:num_steps_short
[Residual,~] = residual(obj_p,Volt_td_short_p(k),...
q,d_N,d_N1, d_N2, beta_p, c_p_max);
[CorrectedState,~] = correct(obj_p,Volt_td_short_p(k),...
q,d_N,d_N1, d_N2, beta_p, c_p_max);
Theta1pos(ss,k) = d2*CorrectedState(1) + d3*CorrectedState(2);
ThetaNpos_d(ss,k) = d_N1*CorrectedState(q) + d_N2*CorrectedState(q-1) + (d_N*beta_p/c_p_max);
[PredictedState,~] = predict(obj_p, Ad_p,Bd_p, u_new(1));
residBuf_p(ww,ss,k) = Residual;
xcorBuf_p(ww,ss,k,:) = CorrectedState';
xcorBuf_tot_pos(ww,ss,k,:) = [Theta1pos(ss,k) CorrectedState' ThetaNpos_d(ss,k)];
xpredBuf_p(ww,ss,k,:) = PredictedState';
end
function C_P = pos_electrodeMeasurementJacobianFcn(Theta_td, rho)
d_N2 = -(rho(N)-rho(N-1))^2/((rho(N-1)-rho(N-2))*(2*rho(N)-rho(N-1)-rho(N-2)));
d_N1 = (rho(N)-rho(N-2))^2/((rho(N-1)-rho(N-2))*(2*rho(N)-rho(N-1)-rho(N-2)));
d_N = ((rho(N)-rho(N-1))*(rho(N)-rho(N-2)))/(2*rho(N)-rho(N-1)-rho(N-2));
theta_p = d_N1*Theta_td(:,q) + d_N2*Theta_td(:,q-1) + (d_N*beta_p/c_p_max);
%%theta_p = ThetaNpos(kk); questo ti serve per ricordarti cosa passargli
C_tildeP2 = d_N2*((6*85.681)*theta_p^5 -(5*373.7)*theta_p^4 + (4*613.89)*theta_p^3 -(3*555.65)*theta_p^2 +...
(2*281.06)*theta_p -76.648 -(115*5.657*theta_p^114)*(0.30987)*exp(5.657*theta_p^115));
C_tildeP1 = d_N1*((6*85.681)*theta_p^5 -(5*373.7)*theta_p^4 + (4*613.89)*theta_p^3 -(3*555.65)*theta_p^2 +...
(2*281.06)*theta_p -76.648 -(115*5.657*theta_p^114)*(0.30987)*exp(5.657*theta_p^115));
C_P = [zeros(1,N-4) C_tildeP2 C_tildeP1];
end
Umar
Umar on 29 Jul 2024
Hi Camilla,
To resolve this issue, ensure that all additional arguments required by the MeasurementJacobianFcn are passed as optional input arguments to the residual function. Check the implementation of the pos_electrodeMeasurementJacobianFcn function to verify the correct number of arguments and their types are being passed. Make sure the arguments match between the residual and MeasurementJacobianFcn functions to align the optional input arguments properly.

Sign in to comment.

Answers (1)

Rahul
Rahul on 20 Aug 2024
I understand that you wish to pass some parameters to the Jacobian function of your extended Kalman Filter.
One of the ways you can achieve this is by defining your own Jacobian function.
function J = myJacobianFunction(state, param1, param2)
J = [param1, 0; 0, param2];
end
% This is an example it can be changed as per requirements.
param1 = 1.0;
param2 = 2.0;
jacobianFcnHandle = @(state) myJacobianFunction(state, param1, param2);
% Function handle for the Jacobian function
obj = extendedKalmanFilter(...
@stateTransitionFcn, ...
@measurementFcn, ...
'MeasurementJacobianFcn', jacobianFcnHandle);
% Adding the function handle as the 'MeasurementJacobianFcn' of the extended Kalman filter
You can refer to the following documenatations for the detailed instructions on how to use these functions:
Hope this helps! Thanks.

Community Treasure Hunt

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

Start Hunting!