MATLAB: I cant put the loop output in a matrix format. I want to put the loop output (p) as a vector or matrix in order to plot all the outputs later.

loop outputmatricesvectors

p=0.5;
h=0.01;
dpdt= @(t,p) (kmax)*(1/(K+(1/p)));
p_plot=zeros(0,10000);
t=0:h:100;
for t=0:h:100
k1=dpdt(t,p);
k2=dpdt(t+(h/2), p+(h*k1/2));
k3=dpdt(t+(h/2), p+(h*k2/2));
k4=dpdt(t+h, p+(h*k3));
p=p+((1/6)*(k1+(2*k2)+(2*k3)+k4)*h)
p_plot=[p,t];
end

Best Answer

h=0.01;
dpdt= @(t,p) (kmax)*(1/(K+(1/p))); % define K and Kmax where is t here?
t=0:h:100;
p=zeros(1,numel(t));
p(1)=0.5;
for i=2:numel(t)
k1=dpdt(t,p(i-1));
k2=dpdt(t+(h/2), p(i-1)+(h*k1/2));
k3=dpdt(t+(h/2), p(i-1)+(h*k2/2));
k4=dpdt(t+h, p(i-1)+(h*k3));
p(i)=p(i-1)+((1/6)*(k1+(2*k2)+(2*k3)+k4)*h);
end
plot(p,t)