Expert Answer:
:
Below is an implementation in MATLAB I have done of the Euler's Method for solving a pair of coupled 1st order DE's. It solves a harmonic oscillator of represented by the following:
y1(t+h) = y1(t) + h*y2(t)
y2(t+h) = y2(t) + h*(-A/M y1(t) -B/M y1(t)/|y1(t)|)
% Do the integration using the Euler Method while(T<=T1) % Update the position of the pt mass using current velocity Y1(i+1) = Y1(i) + H*Y2(i); % Update the velocity of the pt mass checking if we are turning % within the C/A band if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) Y2(i+1) = Y2(i); else Y2(i+1) = Y2(i) + H * ( ((-A/M)*Y1(i)) - (B/M)*sign(Y2(i)) ); end % Incriment the time by H T = T + H; % Increase the looping index variable i = i + 1; end
Without explicitly solving YOUR homework question, I hope this example helps. A few things to note: the if statement is accounting for static friction in this particular example -- so disregard that and only look at what happens after the 'else'.
Also note that I have initial conditions y1(0) and y2(0) defined as Y1(i=1) and Y2(i=1), so starting with Yj(i+1) gives Yj(2). Note that T1 is the ending time of the simulation.
Below is the same problem using the Improved Euler Method. If you derive the update equations for this system you should be able to walk through the code.
% Do the integration using the Improved Euler Method % Ki_j = K^i_j while(T<=T1) % Calculate K^i_j's K1_1 = Y2(i); % Must check if we are turning within C/A if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) K1_2 = 0; else K1_2 = (-A/M)*Y1(i) - (B/M)*sign(Y2(i)); end K2_1 = Y2(i)+H*K1_2; % Checking if we are turning within C/A if ( (abs(Y2(i) < th) && abs(Y1(i)) < C/A) ) K2_2 = 0; else K2_2 = (-A/M)*(Y1(i) + H*K1_1) - (B/M)*sign(Y2(i)+ H*K1_2); end % Update the position and velocity Y1(i+1) = Y1(i)+ (H/2)*(K1_1+K2_1); Y2(i+1) = Y2(i) + (H/2)*(K1_2+K2_2); % Incriment the time by H T = T + H; % Increase the looping index variable i = i + 1; end