function myeuler(n,xs,ys,xe,s)
% or you could use
% function [x,y]= myeuler(n,xs,ys,xe,s)
% if you want the x,y values as output of program
% general format for a function file is
% function [list of output variables] = Name(list of input variables)
% your instructions or body of program
% end
% Comments are preceded by a percent sign
% This program is a simple Euler solver for the first order ODE y'=f(x,y)
% Sample RUN command for this file is:
% you probably want numerical output then use (inclusive of semi-colon)
% myeuler(1000,1,3,10,'y');
% if you don't want numerical output use
% myeuler(1000,1,3,10,'n');
% you need to understand the RUN command to use it for homework
% Explanation of variables:
% (xs,ys) is the starting point, the initial condition
% xe is the ending x value, solution is found on interval [xs,xe]
% n is the number of intervals, total number of points is n+1
% points are (x(1),y(1)) through (x(n+1),y(n+1))
% s is a character string,
% if you want numerical ouput enter 'y', otherwise enter 'n', with quotes
% f is a function given in the subroutine below. edit it for your ODE
h=(xe-xs)/n; % also named: run, increment, delta, delta x, or step-size
x=zeros(1,n); % allocating space for a vector for x values
y=zeros(1,n); % allocating space for a vector for y values
x(1)=xs; % initialize the first x value
y(1)=ys; % initialize the first y value
for i=1:n % start a "for" loop, i runs from 1 to n,
% the generated points here are x(2),...,x(n+1)
x(i+1)=x(i)+h; % increase x by the run/step-size/delta/delta x
m=f(x(i),y(i)); % m is slope, a call is made to function f, below, to calculate m
k=h*m; % k is rise=run*slope
y(i+1)=y(i)+k; % to get new y, increase old y by rise
end % end of "for" block, program repeats this block n times
plot(x,y) % plot the solution, y is on vertical axis, x is on horizontal axis
if s=='y' % start an "if" block, if the string s has value 'y' then
[x;y]' % program will print out [x;y] values as a vertical table
% Note: if v is a horizontal vector then v' is a column vector
end % end of "if" block, if the string s has any other value nothing is printed
end % end of the main function, end of myeuler
function yp=f(x,y) % this subroutine calculates slope in terms of position
% f is the right hand side of ODE, as in dfield software
% this has to be edited for each problem
yp=sin(5*x*y); % this particular example is for the ODE y'=sin(5xy).
% Note: you need a * to indicate multiplication
% Note: be consistent with name of variables
% if function is f(x,y) then use x,y inside this subrotine
% if function is f(t,y) then use t,y inside
% Edit and Save this formula for your ODE
end % end of subroutine