0% found this document useful (0 votes)
2 views7 pages

Ecuaciones Diferenciales: Runge-Kutta 2 Del Punto Medio

The document discusses the implementation of the Runge-Kutta 2 midpoint method for solving a specific differential equation. It outlines the steps to define the function, approximate the solution using MATLAB's ode45, and calculate the error relative to the exact solution. The document also includes a vectorial implementation of the method and presents the results of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Ecuaciones Diferenciales: Runge-Kutta 2 Del Punto Medio

The document discusses the implementation of the Runge-Kutta 2 midpoint method for solving a specific differential equation. It outlines the steps to define the function, approximate the solution using MATLAB's ode45, and calculate the error relative to the exact solution. The document also includes a vectorial implementation of the method and presents the results of the calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ecuaciones diferenciales My Solutions

Runge-Kutta 2 del punto medio


Sea la ecuación diferencial

y′ = f (t, y), y(t0) = y0

Dado un tamaño de paso h , se tiene el método queda definido por las fórmulas

k1 = f (ti, yi)

1 1
k2 = f ti + h, yi + hk1
( )
2 2

yi + 1 = yi + hk2

Se quiere simular los valores de la ecuación diferencial


−t
2 e
y′ − y = , y(2) = 0
2
ln (t) t

en el intervalo t ∈ [2, 3] , considerando un tamaño de paso h por determinar.

Realice lo siguiente

1. Defina la función f (t, y) para la ecuación diferencial a estudiar. Guarde el valor de f (2, 0) en la variable fvalue.
2. Use la función de MATLAB ode45 para aproximar y(3) , sea N la cantidad de iteraciones empleadas. Calcule h = 1/ N , almacene el resultado en la variable h.
3. Implemente la función rk2_midlle para el método de Runge-Kutta 2 del punto medio empleando la plantilla dada.
4. Aproxime el valor de y(3) con el método implementado, considere el valor de h previamente calculado. Almacene el resultado en la variable y_rk2.
5. Se sabe que el valor exacto de y(3) es 0.069899. Guarde el error relativo de la aproximación en la variable xi_rk2.
6. Corrobore que la implementación del método de Runge-Kutta 2 del punto medio trabaja de manera vectorial; es decir, puede resolver el problema dado en la plantilla

Script   Save  Reset  MATLAB Documentation (https://www.mathworks.com/help/) Open Item in MATLAB Online 
A

1
2
3
4
5
6
% f(t,y)
7
f=@ (t,y) ( exp(-t)/t^2 )+( 2*y/log(t) )
8
y0=0 % y(2)=0
9
fvalue=f(2,0)
10
% h
11
[tsol,sol]=ode45(f,[2 3],y0);
12
N=length(sol)
13
h=1/N
14
15
% RK2
16
[t_rk2,y_rk2]=rk2_midle(f,[2 3],y0,h);
17
y_rk2=y_rk2(:,end)
18
% Error de la aproximación
19
exacto=0.069899
20
xi_rk2=abs(exacto-y_rk2)/abs(exacto)
21
22
% Funcionamiento vectorial
23
tspan = [0 10], h = 0.1; y0 = [1; 0]; % Initial condition and solver parameters
24
[t_vec, y_vec] = rk2_midle(@ftest, tspan, y0, h);
25
z_vec = [t_vec; y_vec]
26
27
function dydt = ftest(t,y)
28
dydt = [y(2); -y(1)];
29
end
30
31
function [t,y] = rk2_midle(f, tspan, y0, h)
32
% Integrate a system of ordinary differential equations in the form
33
% dy/dt = f(t,y) as an initial value problem using Middle Point method.
34
35
% Initialize the output solution arrays.
36
nSteps = floor( (tspan(2)-tspan(1))/h );
37
m = length(y0);
38
t = zeros(1,nSteps+1);
39
y = zeros(m,nSteps+1);
40
t(1) = tspan(1);
41
y(:,1) = y0;
42
43 A
% Integrate the ODE's using Middle Point method.
44
%
45
% Complete here
46
%
47
for i = 1:nSteps
48
k1 = f(t(i), y(:,i));
49
k2 = f(t(i) + h/2, y(:,i) + (h/2)*k1);
50
y(: i+1) = y(: i) + h * k2;

 Run Script 

Tests: 3 of 5 Tests Passed (60%) Run Pretest  Submit (max. reached) 

  Define correctamente f(t,y) (Pretest) 20% (20%)

 Determina correctamente el tamaño de paso h 20% (20%)

 Aproxima y(3) correctamente con el método de Runge-Kutta 2 del punto medio 0% (20%)
Variable y_rk2 has an incorrect value.

 Calcula el error relativo de la aproximación 0% (20%)


Variable xi_rk2 has an incorrect value.

 El método de Runge-Kutta 2 del punto medio implementado trabaja de manera vectorial 20% (20%)

Total: 60%

Output
A
f =

function_handle with value:

@(t,y)(exp(-t)/t^2)+(2*y/log(t))
y0 =

fvalue =

0.033833820809153

N =

49

h =

0.020408163265306

y_rk2 =

0.069862957926970

exacto =

0.069899000000000
A

xi_rk2 =

5.156307390703199e-04
tspan =

0 10

z_vec =

Columns 1 through 9

0 0.100000000000000 0.200000000000000 0.300000000000000 0.400000000000000 0.500000000000000 0.6000


1.000000000000000 0.995000000000000 0.980025000000000 0.955224875000000 0.920848000625000 0.877238765621875 0.8248
0 -0.100000000000000 -0.199000000000000 -0.296007500000000 -0.390049950000000 -0.480184500312500 -0.5655

Columns 10 through 18

0.900000000000000 1.000000000000000 1.100000000000000 1.200000000000000 1.300000000000000 1.400000000000000 1.5000


0.620507623055981 0.538970697569426 0.452028552416600 0.360552647472168 0.265459915339205 0.167703570236663 0.0682
-0.784343873712754 -0.842472916649789 -0.892157621823482 -0.932899688956025 -0.964290455258462 -0.986014994516090 -0.9978

Columns 19 through 27

1.800000000000000 1.900000000000001 2.000000000000000 2.100000000000001 2.200000000000001 2.300000000000001 2.4000


-0.230165601960146 -0.326353044497550 -0.419271202449929 -0.507988489551696 -0.591615409977885 -0.669313476592057 -0.7403
-0.973382705472044 -0.945499231748669 -0.908136431140171 -0.861668628739477 -0.806561436640610 -0.743367088459618 -0.6727

Columns 28 through 36

2.700000000000001 2.800000000000001 2.900000000000001 3.000000000000001 3.100000000000001 3.200000000000002 3.3000


-0.906286272396483 -0.944101081940442 -0.972452223508186 -0.991055240313800 -0.999723393410688 -0.998369536192461 -0.9870
-0.423462409059409 -0.330716469774464 -0.234652779231547 -0.136234292984571 -0.036447597488268 0.063706979840242 0.1632 A

Columns 37 through 45

3.600000000000002 3.700000000000002 3.800000000000002 3.900000000000002 4.000000000000002 4.100000000000001 4.2000


-0.894497686986399 -0.845217355282953 -0.787462487584503 -0.721811864576326 -0.648923436360197 -0.569527728483850 -0.4844
0.448078432685137 0.535287809220351 0.617133105702544 0.692793688932482 0.761510906945452 0.822595696046745 0.8754

Columns 46 through 54

4.500000000000000 4.600000000000000 4.699999999999999 4.799999999999999 4.899999999999999 4.999999999999998 5.0999


-0.203595059961603 -0.104614098257184 -0.004581905693694 0.095498721279190 0.194624475586924 0.291801597670757 0.3860
0.979629864046115 0.995091220722044 1.000577174444153 0.996032479141301 0.981502444617676 0.957132484835895 0.9231

Columns 55 through 63

5.399999999999997 5.499999999999996 5.599999999999996 5.699999999999996 5.799999999999995 5.899999999999995 5.9999


0.642034395647914 0.715579857310972 0.781953469541029 0.840489657579242 0.890601400204925 0.931786116587119 0.9636
0.767556336412973 0.699515115166117 0.624459553859189 0.543141909135790 0.456377233832187 0.365035207642533 0.2700

Columns 64 through 72

6.299999999999994 6.399999999999993 6.499999999999993 6.599999999999993 6.699999999999992 6.799999999999992 6.8999


1.000415346958592 0.992683151516785 0.974999114176136 0.947540268614938 0.910581645389736 0.864493517203921 0.8097
-0.027301187070141 -0.127206215830650 -0.225838499903175 -0.322209218821273 -0.415352199588660 -0.504333603129690 -0.5882

Columns 73 through 81

7.199999999999990 7.299999999999990 7.399999999999990 7.499999999999989 7.599999999999989 7.699999999999989 7.7999


0.599351830186449 0.516194046710448 0.427859338971587 0.335233132991843 0.239243899198706 0.140853845585257 0.0410
-0.801610243250691 -0.857537375053083 -0.904869092848862 -0.943130681281776 -0.971938341174552 -0.991003039388550 -1.0001

Columns 82 through 90

8.099999999999987 8.199999999999987 8.299999999999986 8.399999999999986 8.499999999999986 8.599999999999985 8.6999


-0.256835703875825 -0.352301845642654 -0.444238548060460 -0.531724057451520 -0.613881220304363 -0.689886277852726 -0.7589
-0.967503202862081 -0.936982116460189 -0.897067021313622 -0.848157831401008 -0.790744636498851 -0.725402791285921 -0.6527 A

Columns 91 through 99

8.999999999999984 9.099999999999984 9.199999999999983 9.299999999999983 9.399999999999983 9.499999999999982 9.5999


-0.918223722090240 -0.953522163661918 -0.979262428003925 -0.995186230011801 -1.001133688158858 -0.997044929768577 -0.9829
-0.398895601821292 -0.305078751603162 -0.208201141478954 -0.109233892971167 -0.009169100505131 0.090990113813280 0.1902

Columns 100 through 101

9.899999999999981 9.999999999999980
-0.882636140767323 -0.830954421124928
0.472685389385588 0.558585576515392

© 2025 The MathWorks, Inc.

You might also like