From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simulate Kalman Filter Using Mathscript

I am trying to follow a example from MATLAB using Mathscript Kalman Filter Design

When I run the code, it shows "Error in function lsim at line 39 column 7:  The inputs to this function must be scalar." but I have no idea how to fix it. Any help will be appreciated.

 

 

A=[1.1269, -0.4940, 0.1129;   1, 0, 0 ; 0, 1, 0]

B = [-0.3832; 0.5919; 0.5191];

C = [1, 0 , 0];

D = 0;

plant = ss(A,B,C,D,1)

Q = 2.3;
R = 1;

%Time update: 
%x[n+1|n] = Ax[n|n-1] + Bu[n]
%  Measurement update:
%x[n|n] = x[n|n-1] + M (yv[n] - Cx[n|n-1])

[SysKal, L, P, M, Z] = kalman(plant, Q, R);

a = A;
b = [B B 0*B];
c = [C;C];
d = [0 0 0;0 0 1];
P = ss(a,b,c,d,-1)

sys = parallel(P,SysKal,1,1,[],[]);

SimModel = feedback(sys,1,4,2,1);
SimModel = SimModel([1 3],[1 2 3]);     % Delete yv form I/O

t = (0:100)';
u = sin(t/5);

w = sqrt(Q)*randn(length(t),1);
v = sqrt(R)*randn(length(t),1);

out = lsim(SimModel,u,t);

y = out(:,1);   % true response
ye = out(:,2);  % filtered response
yv = y + v;     % measured response

y = out(:,1);   % true response
ye = out(:,2);  % filtered response
yv = y + v;     % measured response


clf
plot(t,y,'b',t,ye,'r--')
xlabel('No. of samples'), ylabel('Output')
title('Kalman filter response')
plot(t,y-yv,'g',t,y-ye,'r--')
xlabel('No. of samples'), ylabel('Error')

 

2015-01-30_16-38-05.png

0 Kudos
Message 1 of 4
(3,259 Views)

When I copy/paste into MathScript, it gives me an error at line 9.

I am used to this because I don't have toolboxes so MathWorks toolbox content doesn't exist there.  It is the base platform.

 

Question for you: what version of LabVIEW and MathScript are you using?  Do you have MatLab with control system toolbox (or similar) installed?

0 Kudos
Message 2 of 4
(2,766 Views)

No. If you put the Matlab code inside Mathscript Node, it wouldn't work. Unless you put your Matlab code into Matlab script, then it will work. Are you already install the Matlab on your computer? Otherwise, it wouldn't work because it needs the Matlab runtime engine to run it.

0 Kudos
Message 3 of 4
(2,381 Views)

If you refer to the example on this help page:

 

http://zone.ni.com/reference/en-XX/help/371894J-01/lvcdtextmath/cdmc_lsim/

 

It suggests to me that your t and u need to be transposed compared to what you have. so try

t = (0:100);
u = sin(t/5)';
Message 4 of 4
(2,368 Views)