% Usage: [y t] = solver1(f,a,b,ya,n,step) or y = solver1(f,a,b,ya,n,step) % Single step method for initial value problems % % Input: % f - Matlab inline function f(t,y) % a,b - interval % ya - initial condition % n - number of subintervals (panels) % step - one step of the solver, for example, @eulerstep % % Output: % y - computed solution % t - time steps % % Examples: % [y t]=solver1(@myfunc,0,1,1,10,@eulerstep); here 'myfunc' is a user-defined function in M-file % y=solver1(inline('sin(y*t)','t','y'),0,1,1,10,@heunstep); % f=inline('sin(y(1))-cos(y(2))','t','y'); % y=solver1(f,0,1,1,10,rk4step); function [y t] = solver1(f,a,b,ya,n,step) h = (b - a) / n; y(1,:) = ya; t(1) = a; for i = 1 : n t(i+1) = t(i) + h; y(i+1,:) = step(f,t(i),y(i,:),h); end;