

% The first part is for approximating y(1), and graphs the approximate solution to y at the end, along with the actual solution y = e^(-x) + x -1. Anything that follows a "%" sign is considered to be commented out in MATLAB, meaning the program doesn't read it.

%we set up a code that will take a certain step size, create a list of x values at which we will approximate y
        
    h = 2^(-2); %set the step size
    x = [0:h:1]; % create list of x's between 0 and 1 that we will approximate at

        
    y = zeros(size(x)); % creates a zero vector that we will fill in with our approximate values for y at each value of x.
  
        
    for k = 2:length(x), %this loop runs us over the entire list of x's and finds the approximate value of y at each x
       
       y(k) = y(k-1) + h*(x(k-1) - y(k-1)); % computes y(x_k) using Euler's method

    end

    y(length(x)) % prints the final approximation value for y(1).
    
    plot(x, y, x, exp(-x) +x -ones(size(x)))  % plots the approximation to y on [0,1] along with the actual solution


        

        
