Você está na página 1de 4

% ----------------------------------------------------------------

% PRINCIPAL Realiza testes para os Métodos Runge-Kutta:


% Runge-Kutta de Segunda Ordem (Euler Aperfeiçoado).
% Runge-Kutta de Quarta Ordem.
% ----------------------------------------------------------------

% ----------------------------------------------------------------
% Testes da Parte I
% tamanho do passo
h=0.1;
% vetores para a solução
x=(1:h:2)';
y2=zeros(11,1);
y4=zeros(11,1);
% condição inicial
y0=-1;
y2(1)=y0;
y4(1)=y0;
% função y'=f
f=@fdifpartei;
% Runge-Kutta de Segunda Ordem (Euler Aperfeiçoado)
for i = 1:10
y2(i
+1)=y2(i)+(h/2)*(f(x(i),y2(i))+f(x(i)+h,y2(i)+h*f(x(i),y2(i))));
end
% Runge-Kutta de Quarta Ordem
for i = 1:10
k1=h*f(x(i),y4(i));
k2=h*f(x(i)+(h/2),y4(i)+(k1/2));
k3=h*f(x(i)+(h/2),y4(i)+(k2/2));
k4=h*f(x(i)+h,y4(i)+k3);

y4(i+1)=y4(i)+(1/6)*(k1+2*k2+2*k3+k4);
end
% Gráfico das Soluções
f1=figure('Name','Soluções para o PVI - Parte I','NumberTitle','off');
% solução analítica
f=@fpartei;
plot(x,f(x),'^-r')
hold on
% solução Euler Aperfeiçoado
plot(x,y2,'*-b')
hold on
% solução Runge-Kutta de Quarta Ordem
plot(x,y4,'x-g')
hold off
% cria título e legenda
title('Soluções para o PVI - Parte I')
%legend('Analítica','RK2Ordem','RK4Ordem')
% ----------------------------------------------------------------
% Testes da Parte II
% solução por ode23 do MATLAB

1
xspan = [0 80];
y0=76.1;
[xode23,yode23]=ode23(@fdifparteii,xspan,y0);
% Gráfico das Soluções
f2 = figure('Name','Soluções para o PVI - Parte
II','NumberTitle','off');
% solução analítica
f=@fparteii;
xii=(0:10:80)';
plot(xii,f(xii),'^-r')
hold on
% solução por ode23
plot(xode23,yode23,'*-b')
hold off
% cria título e legenda
title('Soluções para o PVI - Parte II')
%legend('Analítica','Ode23')
% ----------------------------------------------------------------
% Definições das Funções
% definição da função da equação diferencial y'=f(x,y) da Parte I
function f = fdifpartei(x,y)
f=(1/(x^2))-(y/x)-(y^2);
end
% definição da solução analítica da Parte I
function f = fpartei(x)
f=-(1.0/x);
end
% definição da função da equação diferencial y'=f(x,y) da Parte II
function f = fdifparteii(x,y)
f=(0.02)*y-(0.00004)*(y^2);
end
% definição da função P(t) da Parte II
function p = fparteii(t)
p=(89.7617*exp(0.02*t))/(1+0.1795*exp(0.02*t));
end

2
3
Published with MATLAB® R2018a

Você também pode gostar