title: Forward Euler Method to solve first order ODEs in Matlab
body: <p>I wrote this Matlab program that is supposed to solve the IVP du/dx= -5000(u(t) - cos(t)) - sin(t) with u(0)=1. My exact solution should be u(t) = cos(t) but the solution I am getting from Euler's forward in my code is huge in comparison to what it should be and what I calculated but I'm not sure where I've gone wrong in my code. Can you find my error?</p>
  
  <pre><code>function main
  
  dt=5;
  u0 = 1;
  n=50;
  
  [T,U] = euler(dt, u0, n);
  
  uexact = cos(T);
  
  plot(T,U)
  
  hold on 
  
  plot(T, uexact, 'r')
  
  end
  
  function [T,U]= euler( dt, u0, n)
  
  R= dt/n;
  T=zeros(1,n+1);
  U=zeros(1,n+1);
  U(1)=u0;
  T(1) = 0;
  
  
  for j=1:n   
  
   U(j+1)= U(j)+ R*(rhs(T(j),U(j)));
   T(j+1)= T(j) + R;
  end
  
  end
  
  
  function dP = rhs(t, P)
  P = zeros(1,1);
  
  dP = (-5000)*(P - cos(t)) - sin(t);
  
  end
  </code></pre>