Converting MATLAB to Python using ChatGPT. An example for a Kalman filter simulation.

  Рет қаралды 1,317

T.J Moir

T.J Moir

Жыл бұрын

Using a Kalman filter written from scratch on MATLAB I checked out ChatGPTs ability to convert from MATLAB to Python. It's pretty good though not quite perfect of course at present. I'm sure you'll all want to try your own examples but for reference here is my original MATLAB code:
-----------------------------------------------------------------------------------------------------------------------
% Finds Kalman Filter and shows the state estimates
x=[0 0]';% state vector
xh=[0 0]'; % Kalman estimate vector of state
Npoints = 200; % number of time steps
y=zeros(Npoints,1);
s=zeros(Npoints,1);
x1=zeros(Npoints,1);
x2=zeros(Npoints,1);
xh1=zeros(Npoints,1);
xh2=zeros(Npoints,1);
t=[1:1:200];
% Define the system
F = [0, 1; -0.8 1.5]; % system matrix
D = [0 1]'; % system process noise vector
H = [1 1]; % observation matrix
Q = D*D'*1; %Process noise covariance is unity
R=5 %Measurement noise variance - scalar
% Initialize the covariance matrix
P = eye(2)*0.1 % Covariance matrix for initial state error
%Generate Random Noise Variance =1
zeta=randn(Npoints,1);
%re-seed noise generator
rng(2);
% Additive uncorrelated white noise
rv=sqrt(10)*randn(Npoints,1);
% Loop through and perform the Kalman filter equations recursively
for k = 1:Npoints
% State equations of system
x=F*x+D*zeta(k);
y(k)=H*x;
s(k)=y(k)+rv(k);
% Store states for plotting later
x1(k)=x(1);
x2(k)=x(2);
%Update Kalman gain vector : one step ahead version
K=F*P*H'/(H*P*H'+R);
% Update the covariance from Riccati equation
P = F*P*F' + Q-K*H*P*F';
%Kalman Filter
xh=F*xh+K*[s(k)-H*xh];
%Store estimated states for plotting
xh1(k)=xh(1);
xh2(k)=xh(2);
end
%Plot results
figure
subplot(2,1,1)
plot(t,x1,'-r',t,xh1,'-.b')
legend('State 1','State 1 estimate','Location','northwest')
title('State 1 and KF Estimate')
xlabel({'Time(samples)'})
subplot(2,1,2)
plot(t,x2,'-r',t,xh2,'-.b')
legend('State 2','State 2 estimate','Location','northwest')
title('State 2 and KF Estimate')
xlabel({'Time(samples)'})
ax = gca;
set(gcf,'color','w');

Пікірлер: 5
@johnsim3722
@johnsim3722 Жыл бұрын
Firstly, it's kind of strange watching this on my Mac and seeing a Windows desktop! It's doing a good job of converting from one format to another and that can be difficult, and I guess I'll have to do this myself and learn SWIFT to continue with iPhone programming. I dread that for the amount of work I put in to the original apps, and then to convert to a language I don't quite understand yet (not having tried to learn it as yet). What I think people fear will happen is how they placed it out on Star Trek The Next Generation where they'd ask the computer to create a program which did XYZ. There's a big leap there, where the system has to firstly figure out the problem, secondly an implementation in whatever language, be it maths alone, and then create something which fits within the choose programming language framework. There's still an innovation step to go through where someone is creating the basic concept before it is written in to code by the AI. I think that's the most important takeaway from this demonstration.
@TJMoir
@TJMoir Жыл бұрын
Well in this case I did a translation but it does work from scratch too. I must have a go at something to show.
@TJMoir
@TJMoir Жыл бұрын
Watch my new video - coming up, where I get it do write an app to do a calculator from scratch
@johnsim3722
@johnsim3722 Жыл бұрын
@@TJMoir Looking forward to seeing that video. It is impressive what it can do, just when you see that it is lifting artist's work and creating something new from that base source, is it imitating or is that creation? Is it more a very expert expert system, or genuine new intellectual property? But then at the same time, isn't that what we also do as we've read the text books and learned how to program or solve equations?
@TJMoir
@TJMoir 11 ай бұрын
@@johnsim3722 what you have to watch is that everything it created I think it remembers too. don't piss it off John Connor
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 372 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 36 МЛН
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 4,5 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Nutella bro sis family Challenge 😋
00:31
Mr. Clabik
Рет қаралды 12 МЛН
Kalman Filter - Part 1
8:35
Machine Learning TV
Рет қаралды 98 М.
Kalman filter test for sensor fusion (GPS + accelerometer)
17:04
100+ Linux Things you Need to Know
12:23
Fireship
Рет қаралды 714 М.
I Made a Game Using ChatGPT
9:51
Rye
Рет қаралды 2,6 МЛН
MATLAB + ChatGPT = MatGPT 🤯
10:09
Jousef Murad | Deep Dive
Рет қаралды 32 М.
SciLab's XCOS - A Matlab Simulink Alternative
7:18
CircuitBread
Рет қаралды 21 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 795 М.
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 36 МЛН