필터 지우기
필터 지우기

For Loop Segment Troubleshooting

조회 수: 1 (최근 30일)
Thierry Kayiranga
Thierry Kayiranga 2012년 8월 8일
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)');

채택된 답변

Laura Proctor
Laura Proctor 2012년 8월 8일
You can add a breakpoint in your script by clicking on the horizontal line to the left of the number in the MATLAB Editor. This will produce a red dot. Next time you run the code, it will stop when it encounters this red dot. Then, you can type in the Command Window or hover over the variables in the script, or even use the Workspace Window to see what's happening. I suspect you'll want to check the dimensions of A, B, x_2(:,i-1), and u - you can do this with the SIZE function in MATLAB. To get out of debugging mode, you can type dbquit in the Command Window. When you are done debugging, you'll want to remove the red dot by clicking on it, so that your code doesn't keep stopping at that point.
  댓글 수: 1
Thierry Kayiranga
Thierry Kayiranga 2012년 8월 13일
Thank you for your help. Your method allowed me to find what i think it is the really problem. This loop:
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
during the first iteration when i=2, everything is worked out perfectly. Now during the second iteration when i=3, i think the loop fail because for k1, A*x_2(:,i-1) and B*u cannot be summed together because they have different sizes but how come MATLAB doesn't pick up on that? or maybe i am wrong? A and B are 2x1 matrices and u is 1x1. I am trying to fix the loop but i am coming empty handed. Any pointers?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by