clear,clc % Solve ODE-IVP using Euler's Explicit Method % y' = (x-1)(y-y^2) % y(0) = 1/2 x0 = 0; xEnd = 2; y0 = 0.5; h = 0.04; N = (xEnd - x0)/h %% Initializing variables X = [x0:h:xEnd]'; Y = zeros(N+1,1); Y(1) = y0; %ERR_GLOBAL = 0; nodo = 0; err_local = 0; exact_value = 0; aprox_value = 0; % Exact solution of the EDO-PVI A = 0.5*X.^2-X; EXPO = exp(A); YTrue = EXPO./(1+EXPO); %% Solving using Euler's Explicit Method for i = 1:N fi = (X(i)-1)*(Y(i)-Y(i)^2); nodo = X(i); Y(i+1) = Y(i) + h*fi; err_local = abs(Y(i+1)-Y(i)) exact_value = YTrue(i+1); aprox_value = Y(i+1); end exact_value aprox_value % Error global err_global = abs(exact_value - aprox_value) %% Plot results and errors plot(X,Y); title('h=0.04, N=50'); xlabel('X'); ylabel('Y'); text (0.6, 0.46, "(MEE)E_{50}=|y(x_{50})-y_{50}|=0.010362"); legend ('Solucion aproximada'); hold on plot(X,YTrue,'--r'); %ERR_GLOBAL = abs(YTrue - Y); %cálculo del error global %MAX_ERR = max(ERR_GLOBAL)