top of page

function [y] = rdir(f,a,b,n)
h = (b-a)/n
x = linspace(a,b,n+1)
y=0;
for i = 1:n
y = y + h*f(x(i+1))
end
endfunction

function [y] = resq(f,a,b,n)
h = (b-a)/n
x = linspace(a,b,n+1)
y=0;
for i = 2:(n+1)
y = y + h*f(x(i-1))
end
endfunction

function [y] = rpm(f,a,b,n)
h = (b-a)/n
x = linspace(a,b,n+1)
y=0;
for i = 2:(n+1)
y = y + h*f(.5*(x(i-1)+x(i)))
end
endfunction

function [y] = trap(f,a,b,n)
h = (b-a)/n
x = linspace(a,b,n+1)
y = h*(f(x(1)) + f(x(n+1)))/2
for i = 2:n
y = y + h*f(x(i))
end
endfunction

function [y] = simpson(f,a,b,n)
h = (b-a)/(2*n)
x = linspace(a,b,2*n+1)
y = f(x(1))
for i = 1:n-1
y = y + 2*f(x(2*i+1))
end
for i = 1:n
y = y + 4*f(x(2*i))
end
y = y + f(x(2*n+1))
y = h*y/3
endfunction

function [u,t]=euler(f,t0,tf,u0,N)
h=(tf-t0)/N;
t=zeros(1,N+1);
u=zeros(1,N+1);
t(1)=t0;
u(1)=u0;
for i=1:N;
    u(i+1)=u(i)+h*f(u(i),t(i));
    t(i+1)=t(i)+h;
end
plot(t,u)
endfunction

function [u,t]=taylor2(f,df,t0,tf,u0,N)
h=(tf-t0)/N;
t=zeros(1,N+1);
u=zeros(1,N+1);
t(1)=t0;
u(1)=u0;
for i=1:N;
    u(i+1)=u(i)+h*f(u(i),t(i))+(h^2 /2)*df(u(i),t(i));
    t(i+1)=t(i)+h;
end
plot(t,u)
endfunction

bottom of page