0

I'm having some trouble with this code. My professor asked us to create a function "feuler.m" in MATLAB to solve the initial-value problem given by the differential equation u′(t) = (2+2t)e^t and the initial condition u(0) = 0 over the interval [0, 5] that uses (forward) Euler’s method to graph the exact solution along with the approximate solution.

The input should be: n, the number of subintervals into which the interval [0,5] should be divided.

The output should be a graph of the exact solution and the numerical solution and print the value of the maximum error between the true solution and the numerical solution.

Note that the exact solution is given by u(t) = 2tet.

So far I have written the code:

function myeuler(N)
   t = linspace(0, 5, N+1)';
   ua = zeros(N+1,1);
   ue = 2*t.*exp(t);
   h = 5/N;
   A = zeros(N,N);
   A(2:N,1:N-1) = -eye(N-1);
   A = A + eye(N);
   b = h*(2+2*t(1:N)).*exp(t(1:N));
   b(1) = b(1) + ua(1);
   ua(2:N+1) = A\b;
   plot(t, ua, 'r', t, ue, 'g')
end

I'm unsure if this is right.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • Hi and welcome to SO, please read [ask]. Your post is missing a real problem / question. Just to say that you are "unsure" is not really a programming question. – Irreducible Nov 25 '19 at 08:16
  • Why don't you just implement the Euler method, which amounts to a cumulative summation of function values in the given case? – Lutz Lehmann Nov 25 '19 at 10:51

0 Answers0