%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Function to solve the initial value problem % % dy/dx = F(x,y), x = [a,b], y(a) = ya % % with Euler's method. The vector u(2:N+1) approximates the true solution % y(1:N) at x = a + ih, i = 1:N. Notice u(1) = y(a). % % With Matlab you need to define F with somename = @(x,y) expression before % invoking this function (with arglist (...,somename)). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function u = int_euler(a, b, ya, N, F) h = (b-a) / N; u = zeros(1,N+1); u(1) = ya; for i = 1:N x = a + (i-1)*h; u(i+1) = u(i) + h*F(x,u(i)); end