clear % good to start by clearing out all old values dt=0.001 % dt is the time step t=0:dt:10; % t is a vector of numbers from 0 to 10 in steps of dt x=cos(t); % x is a vector the same size as t, with each element the cosine of the same element in t y(1)=0.0; % redundant because next line sets all values of y to zero y=zeros(size(t)); % y is a vector the same size as t with all elements set to zero for i=2:length(t) % beginning of a loop, that starts with i=2, then i=3, then i=4 until i=no. of elements in t y(i) = y(i-1) + dt*x(i-1); % change in y is dt*x(i-1) solves the eqn: dy/dt = x end % this end marks the end of the for loop, so go back to line 7 and increase i unless i=length(t) already subplot(2,1,1); % plot a figure with 2 rows and one column of graphs: start at position 1. plot(t,x); % plot x against t subplot(2,1,2); % go to graph in the second position of the 2x1 set of graphs plot(t,y); % plot y against t hold on; % don't erase this plot so we can get two curves on one graph for i=2:length(t) %loop in the same way as before y(i) = y(i-1) + dt*x(i-1) + sqrt(dt)*2*(rand-0.5); % generate y as before but with an extra noise term end % go back to line 15 to increase i, unless i has reached length(t) plot(t,y,'r'); % plot the new set of values of y against t, in red.