I need help troubleshooting a for loop.
The problem was to solve a simple first order differential equation using Euler's method. The ODE was in the form of dx/dt = Ax(t) + Bu(t). A and B are calculated and passed-on into this file through a function. The problem is that my Psim simulation doesn't match the Matlab Simulation and the teacher proposed that i troubleshoot the for loop. Can you please point me in the right direction? I am fairly new at Matlab. The Matlab code for Euler's formula i wrote is presented below. Thank you for your help.
h1 = 0.0001; %Adjustable time-step
Tstop = 0.001; %desired simulation time (s)
iterations = Tstop/h1; clear x_2(:,1); x_2(:,1)= [0;0]; k(1)=0; % Starting of the time on x-axis
for i=2:iterations k1 = A*x_2(:,i-1)+ B*u; x_2(:,i) = x_2(:,i-1)+ h1*k1; k(i) = k(i-1)+h1; % time on X-axis
end figure(2); plot(k,x_2(1,:)); title('Euler Approximation'); xlabel('Time'); ylabel('x(t)');
Best Answer