function[sol]=RK4(f,x0,u0,h,n) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %MTODO DE RUNGE-KUTTA DE ORDEM 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Algoritmo para calcular a soluo de PVIs de ordem 1 %Exemplo: Para calcular a soluo de y'(t)=t+y, y(0)=-1, 0 <= t <= 1. %Na "Janela de Comando" (Command Window) digite: % f=@(t,y) t+y := ED a ser calculada % x0= 0 := tempo inicial % u0=-1 := condio inicial % n=10 := nmero de iteraes % h=0.1 := passo ou incremento % Em seguida, digite na Command Window: % [sol]=RK4(f,x0,u0,h,n) % Ir aparecer a soluo da em cada passo h, % inclusive o valor inicial u0, na 1 iterao. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clc vi=u0; for i=1:n, k1=h*f(x0,u0); k2=h*f(x0+(1/2)*h,u0+(1/2)*k1); k3=h*f(x0+0.5*h,u0+(1/2)*k2); k4=h*f(x0+h,u0+k3); u1=u0+(1/6)*(k1+2*k2+2*k3+k4); u0=u1; x0=x0+h; sol(i)=u0; end sol=[vi;sol']; end