You are on page 1of 7

clc

clearvars
F = @(t,y) y-t*t+1; %Derivada de y
G = @(t) t*t +2*t+1-exp(t)/2;%solucion real de y
%condiciones iniciales
a=0;
b=2;
c=0.5;

%Para H1=0.2
H1=0.2;%Tamaño de paso

[X,Y] = MUT(a,b,c,H1,G);
[X1,Y1]=mimetododeRK(a,b,c,H1,F);
[X2,Y2]=RK4(a,b,c,H1,F);
plot(X,Y,X,Y1,X,Y2)
xlabel('t')
ylabel('y')

errorRK4=abs(Y-Y2);
errordemiRK=abs(Y-Y1);

A = [X', Y1', Y2',Y',errorRK4',errordemiRK'];


T = array2table(A,'VariableNames',{'t','Mi Metodo de RK' ...
,'RK4','valor real','Error de RK4','Error de mi RK'})

T = 11×6 table

1
t Mi Metodo de RK RK4 valor real Error de RK4 Error de mi RK

1 0 0.5000 0.5000 0.5000 0 0


2 0.2000 0.8295 0.8293 0.8293 5.2876e-06 0.0002
3 0.4000 1.2144 1.2141 1.2141 1.1441e-05 0.0004
4 0.6000 1.6495 1.6489 1.6489 1.8583e-05 0.0005
5 0.8000 2.1279 2.1272 2.1272 2.6851e-05 0.0007
6 1.0000 2.6417 2.6408 2.6409 3.6393e-05 0.0009
7 1.2000 3.1810 3.1799 3.1799 4.7368e-05 0.0010
8 1.4000 3.7335 3.7323 3.7324 5.9944e-05 0.0011
9 1.6000 4.2846 4.2834 4.2835 7.4289e-05 0.0011
10 1.8000 4.8161 4.8151 4.8152 9.0573e-05 0.0009
11 2.0000 5.3061 5.3054 5.3055 1.0895e-04 0.0006

%Para H2=0.05
H2=0.05;%Tamaño de paso
[XP,YP] = MUT(a,b,c,H2,G);
[XP1,YP1]=mimetododeRK(a,b,c,H2,F);
[XP2,YP2]=RK4(a,b,c,H2,F);
plot(XP,YP,XP,YP1,XP,YP2)
xlabel('t')
ylabel('y')

2
errorPRK4=abs(YP-YP2);
errorPdemiRK=abs(YP-YP1);

B = [XP', YP1', YP2',YP',errorPRK4',errorPdemiRK'];


T1 = array2table(B,'VariableNames',{'t','Mi Metodo de RK','RK4','valor real' ...
,'Error de RK4','Error de mi RK'})

T1 = 41×6 table
t Mi Metodo de RK RK4 valor real Error de RK4 Error de mi RK

1 0 0.5000 0.5000 0.5000 0 0


2 0.0500 0.5769 0.5769 0.5769 5.1974e-09 3.8216e-06
3 0.1000 0.6574 0.6574 0.6574 1.0594e-08 7.7311e-06
4 0.1500 0.7416 0.7416 0.7416 1.6196e-08 1.1727e-05
5 0.2000 0.8293 0.8293 0.8293 2.2012e-08 1.5809e-05
6 0.2500 0.9205 0.9205 0.9205 2.8047e-08 1.9975e-05
7 0.3000 1.0151 1.0151 1.0151 3.4310e-08 2.4222e-05
8 0.3500 1.1130 1.1130 1.1130 4.0807e-08 2.8548e-05
9 0.4000 1.2141 1.2141 1.2141 4.7546e-08 3.2950e-05
10 0.4500 1.3184 1.3183 1.3183 5.4535e-08 3.7424e-05
11 0.5000 1.4257 1.4256 1.4256 6.1783e-08 4.1967e-05
12 0.5500 1.5359 1.5359 1.5359 6.9296e-08 4.6573e-05
13 0.6000 1.6490 1.6489 1.6489 7.7083e-08 5.1237e-05
14 0.6500 1.7648 1.7647 1.7647 8.5154e-08 5.5952e-05
15 0.7000 1.8832 1.8831 1.8831 9.3515e-08 6.0713e-05
16 0.7500 2.0041 2.0040 2.0040 1.0218e-07 6.5511e-05
17 0.8000 2.1273 2.1272 2.1272 1.1115e-07 7.0337e-05
18 0.8500 2.2528 2.2527 2.2527 1.2043e-07 7.5181e-05
19 0.9000 2.3803 2.3802 2.3802 1.3005e-07 8.0034e-05
20 0.9500 2.5097 2.5096 2.5096 1.3999e-07 8.4882e-05
21 1.0000 2.6409 2.6409 2.6409 1.5029e-07 8.9713e-05
22 1.0500 2.7738 2.7737 2.7737 1.6093e-07 9.4513e-05
23 1.1000 2.9080 2.9079 2.9079 1.7194e-07 9.9265e-05
24 1.1500 3.0435 3.0434 3.0434 1.8332e-07 1.0395e-04
25 1.2000 3.1801 3.1799 3.1799 1.9509e-07 1.0855e-04
26 1.2500 3.3174 3.3173 3.3173 2.0724e-07 1.1305e-04
27 1.3000 3.4555 3.4554 3.4554 2.1979e-07 1.1742e-04
28 1.3500 3.5939 3.5938 3.5938 2.3276e-07 1.2163e-04

3
t Mi Metodo de RK RK4 valor real Error de RK4 Error de mi RK

29 1.4000 3.7325 3.7324 3.7324 2.4613e-07 1.2567e-04


30 1.4500 3.8711 3.8709 3.8709 2.5994e-07 1.2949e-04
31 1.5000 4.0093 4.0092 4.0092 2.7418e-07 1.3308e-04
32 1.5500 4.1469 4.1468 4.1468 2.8886e-07 1.3638e-04
33 1.6000 4.2836 4.2835 4.2835 3.0400e-07 1.3937e-04
34 1.6500 4.4192 4.4190 4.4190 3.1959e-07 1.4201e-04
35 1.7000 4.5532 4.5530 4.5530 3.3565e-07 1.4424e-04
36 1.7500 4.6853 4.6852 4.6852 3.5218e-07 1.4603e-04
37 1.8000 4.8153 4.8152 4.8152 3.6919e-07 1.4731e-04
38 1.8500 4.9427 4.9426 4.9426 3.8669e-07 1.4804e-04
39 1.9000 5.0672 5.0671 5.0671 4.0468e-07 1.4816e-04
40 1.9500 5.1883 5.1882 5.1882 4.2316e-07 1.4759e-04
41 2.0000 5.3056 5.3055 5.3055 4.4213e-07 1.4627e-04

function [x,y] = RK4(a,b,c,h,F)%Metodo de RK4


n=(b-a)/h;%numero de intervalos
%condiciones iniciales
x(1)=a;
y(1)=c;
for i=1:1:n
x(i+1)=x(i)+h;
k1=F(x(i),y(i));
k2=F(x(i)+h/2,y(i)+k1*h/2);
k3=F(x(i)+h/2,y(i)+k2*h/2);
k4=F(x(i)+h,y(i)+k3*h);
y(i+1) = y(i)+h*(k1/6 +2*k2/6 + 2*k3/6 +k4/6);

end
end

function [x,y] = mimetododeRK(a,b,c,h,F)%Mi metodo de RK


n=(b-a)/h;%numero de intervalos
%condiciones iniciales
x(1)=a;
y(1)=c;
for i=1:1:n
x(i+1)=x(i)+h;
k1=F(x(i),y(i));
k2=F(x(i)+h/2,y(i)+k1*h/2);
k3=F(x(i)+h,y(i)+k2*h);
k4=F(x(i)+h/2,y(i)+k3*h/2);

4
y(i+1) = y(i)+h*(k1/5 +2*k2/5 + k3/5 +k4/5);

end
end
function [x,y] = MUT(a,b,c,h,G)%nos da el valor de y
n=(b-a)/h;
x(1)=a;
y(1)=c;
for i=1:1:n
x(i+1) = x(i) + h;
y(i+1) = G(x(i+1));

end
end

You might also like