Trying to implement the fourth order AM method in MATLAB using fourth order RK to get the first four starting values. Code is as follows:
% Use the fourth order Adams-Moulton (AM) method with h = 0.1
% Define the exact solution
y = @(t) (7*exp(3*t)+6*t+2)/9;
% Define the RHS of the differential equation
f = @(t,y) 3*y-2*t;
% Define the step size and create an interval of the solution from 0 to 1
h = 0.1;
t = 0:h:1;
% Initialize the approximate solution, ya
ya = zeros(size(t));
% Use fourth order Runge-Kutte to obtain the first four starting values
for j = 1:4
ya(1) = f(t(j), y(j));
ya(2) = f(t(j) + h/2, y(j) + h/2*ya(1));
ya(3) = f(t(j) + h/2, y(j) + h/2*ya(2));
ya(4) = f(t(j) + h, y(j) + h*ya(3));
end
% Implement the four step AM method for the remaining time intervals
for j = 1:(length(t)-4)
ya(j+4) = ya(j+3) + (h/24)*(9*f(t(j+4),y(j+4))+19*f(t(j+3),ya(j+3))-5*f(t(j+2),ya(j+2))+f(t(j+1),ya(j+1)));
end
% Plot the exact solution over the interval [0,1] along with the numerical
% solution for comparison
t1 = linspace(0,1);
plot(t1,y(t1),t,ya,'x','MarkerSize',12,'LineWidth',2)
xlabel('\leftarrow t \rightarrow');ylabel('\leftarrow y \rightarrow');
legend('y(t)-exact solution','y_i - Approx solution, 0\leq i \leq n','Location','NorthWest')
title('Exact solution vs 4^{th} order Adams-Moulton (AM) method')
% Print the numerical solution
var = {'t','Numerical_Values','Exact_Values'};
T = table(t',ya',y(t)','VariableNames',var);
disp('The result of the fourth order Adams-Moulton (AM) method with step size h=0.1 is:')
disp(T)
Now I'm getting output, but the values of y(1) through y(4) from the RK4 cannot be correct because the values are as follows:
The result of the fourth order Adams-Moulton (AM) method with step size h=0.1 is:
| t | Numerical_Values | Exact_Values |
|---|---|---|
| 0 | 379769.246644342 | 1 |
| 0.1 | 436734.533640994 | 1.33877907255911 |
| 0.2 | 445279.326690492 | 1.7727590669704 |
| 0.3 | 513352.84465149 | 2.33524686423318 |
Thoughts?

You have defined $y$ as an anonymous function
This means that you can treat it as a vector
The only way to fix this is to have another vector to store your values per iteration.