0

I am write a simulation for get true Velocity of a harmonic oscillator system as

enter image description here

Where P=[p1 p2;p2 p3] can find using Rung-Kutta Integration method with P(0)=[1 0; 0 1]

Now, I want to write matlab code to get the true postion z of the system and estimate x inwhich x=[x1 x2]'. This is my code to find x and z. However, the result is not correct. Could you help me to modify it. This is my matlab code

function  z=getPos(x)
  %% Function to get velocity
  v=0+1*randn; %r=1;
  z=[0 1]*x+v; 
return
    

This is my result enter image description here

Let see help me is correct implement for the solution. Thank you

Community
  • 1
  • 1
John
  • 2,838
  • 7
  • 36
  • 65

1 Answers1

0

Your code can return a negative value as randn is a normally distributed random number with standard deviation 1. In theory this could return any number and there is ~15% chance it will be less than -1.

You are also correct than you should update x at each step. I would take x as an input into getPos and xdot as an output. Then add another step to update the position.

nivag
  • 573
  • 2
  • 8
  • As the above formula. How to update x and z. Note that x=[x1 x2] is denotes as position and velocity – John Nov 17 '14 at 13:49
  • Matlab handles vectors and matrices very easily and it does matrix multiplication by default. Literally xdot=A * x+[0; 1] * w; z=[1, 0] * x + v; For x just do x=x+xdot*dt; once you know xdot. Choosing a suitable dt. For info on matlab matrices see [here](http://uk.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html) and related. – nivag Nov 17 '14 at 14:48
  • @Thank sir. I update the current solution. Let see help me the implementation result. Is it correct? I use Kalman filter to find the solution – John Nov 17 '14 at 16:20