title: Writing an exact equation for Matlab differential equation
body: <p>I'm having trouble writing an exact equation for this matlab differential problem. I'm using the Forward Euler, Backward Euler, and Crank-Nicolson method. A friend hinted as to how to write the exact solution yet I'm still having trouble. Currently my error is "Matrix Dimensions must agree". I've tried doing some trial and error but nothing has come out to work so far. Error is on the second to last line. Could someone guide me towards the right direction for the exact solution equation? </p>
  
  <pre><code>clear;                                 % clears all memory
  clc;                                   % clears Command Window screen
  close all;                             % closes all open figure windows 
  
  for input_dt = 1:1:3
      prompt = 'enter dt: ';
  
  
  % 1. Initialize
  dt          =  input(prompt);          % choose delta t
  t_start     = 0;                       % starting time
  t_end       = 50;                       % end time
  nstep       = ( t_end - t_start ) / dt;    
  x           = zeros(2,1);
  y           = zeros(2,1);
  z           = zeros(2,1);
  b           = zeros(2,1);
  I           = eye(2);                       
  all_t       = ( 1 : nstep) * dt;
  all_Euler_x = zeros( nstep, 2);
  all_exact = zeros( nstep, 2);
  all_Euler_y = zeros( nstep, 2);
  all_Euler_z = zeros( nstep, 2);
  initial_displacement = 0.5;
  initial_velocity = 1.0;
  
  % 1.2 System Characteristics
  m        = 1.0;
  c        = 0.05;
  k        = 4.0;
  A(1,1) = 1.0 ; A(1,2) = 0.5;
  A(2,1) = -k/m; A(2,2) = -c/m;
  
  x(1) = initial_displacement;
  x(2) = initial_velocity;
  
  
  % 2. Forward Euler time integration
  for n = 1 : nstep     
      x = x + dt * (A * x + b);     
      all_Euler_x (n, :) = x;                  
  end
  
  y(1) = initial_displacement;
  y(2) = initial_velocity;
  
  for n = 1 : nstep
      y = (I - dt * A) \ (y + dt * b);
      all_Euler_y (n, :) = y;
  end
  
  z(1) = initial_displacement;
  z(2) = initial_velocity;
  
  for n = 1 : nstep
      z = 0.5*(y + x); %Crank-Nicolson
      all_Euler_z (n, :) = z;
  end
  
  
  %3. Compute the exact solution
  
  %3.1 Parameters needed to compute the exact solution
  c_c      = 2.0 * sqrt (k*m);
  zeta     = c / c_c;
  omega_n  = c_c / (2.0 * m );
  c1       = (initial_velocity + zeta * omega_n * initial_displacement) / (omega_n * sqrt (1.0 - zeta^2));     
  c2       = initial_displacement;
  
  %3.2 exact solution
  for n = 1 : nstep
      t = n * dt;
      all_exact (n,1) =  all_exact - all_Euler_x / all_Euler_y / all_Euler_z; %Error on this line
  end
  </code></pre>