RK4 - EjemploVentechow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Runge-Kutta

S = Almacenamiento S (H)
A = Área = A (H)
Q = Salida del embalse en función de la altura
I = Entrada I(t)

dS
= I (t) - Q (H (t))
dt
dS = A (H) dH
dS dH
= A (H) = I (t) - Q (H (t))
dt dt
dH I (t) - Q (H (t))
=
dt A (H)

Datos
In[1]:= Ao = 43 560;
dt = 600; tmax = 250 * 60; nt = tmax / dt
Out[2]= 25

Caudal elevación
In[3]:= dh = 0.5; nh = 20;
H = Table [i dh, {i, 0, nh}];
Q = {0, 3, 8, 17, 30, 43, 60, 78, 97, 117,
137, 156, 173, 190, 205, 218, 231, 242, 253, 264, 275};
2 RK4_EjemploVentechow.nb

In[6]:= ListLinePlot[Transpose[{H, Q}], LabelStyle → {16, GrayLevel[0]},


Frame → True, GridLines → Automatic, FrameLabel → {"H", "Q"}]

250
200
150
Q

Out[6]=
100
50
0
0 2 4 6 8 10
H

In[7]:= QH = Interpolation[Transpose[{H, Q}]];

In[8]:= Plot[QH[h], {h, 0., 10.}, LabelStyle → {16, GrayLevel[0]},


Frame → True, GridLines → Automatic, FrameLabel → {"H", "Q"}]

250
200
150
Q

Out[8]= 100
50
0
0 2 4 6 8 10
H

Hidrograma de Entrada
In[9]:=

t = Table [i dt, {i, 0, nt}];


pointss = {{0, 0}, {60 × 60., 360.}, {60 × 150., 0}, {60 nt dt, 0}};
Ents = Interpolation[pointss, InterpolationOrder → 1];
Hin = Ents[t];
RK4_EjemploVentechow.nb 3

In[13]:= Plot[Ents[t], {t, 0., tmax}, Frame → True,


GridLines → Automatic, FrameLabel → {"t", "I(t)"}]

350

300

250

200
I(t)

Out[13]=
150

100

50

0
0 2000 4000 6000 8000 10 000 12 000 14 000
t

Runge Kutta 3 Orden (Ven te Chow)


In[14]:=

(Ents[t] - QH[h])
In[15]:= f[t_, h_] := dt
Ao

In[16]:= QH[H]
Out[16]= {0., 3., 8., 17., 30., 43., 60., 78., 97., 117., 137.,
156., 173., 190., 205., 218., 231., 242., 253., 264., 275.}

In[17]:= ts = t;

In[18]:= HS = Table[0, {i, 0, nt}];

In[19]:= QS = HS; dh1 = dh2 = dh3 = 0;

In[20]:= Fori = 1, i <= nt, i ++,


dh1 = f[ts[[i]], HS[[i]]];
dt dh1
dh2 = fts[[i]] + , HS[[i]] + ;
3 3
2 dt 2 dh2
dh3 = fts[[i]] + , HS[[i]] + ;
3 3
dh1 3
dh = + dh3;
4 4
ht = HS[[i]] + dh;
HS[[i + 1]] = ht;
QS[[i + 1]] = QH[ht];
(*Print[ tmin[[i+1]],"\t",Hin[[i+1]],"\t",
dh1,"\t",dh2,"\t",dh3,"\t",HS[[i+1]],"\t",QS[[i+1]]]*)

In[21]:= QS;
4 RK4_EjemploVentechow.nb

In[22]:= ListLinePlot[{Transpose[{ts, Hin}], Transpose[{ts, QS}]},


PlotLegends → {"Entrada", "Salida"}]

350

300

250

200 Entrada
Out[22]=

150
Salida

100

50

2000 4000 6000 8000 10 000 12 000 14 000

In[23]:= Max[QS]
Out[23]= 269.475

Función
(Ents[t] - QH[h])
In[24]:= ff[t_, h_] :=
Ao

Método de Euler
In[25]:= He = Table[0, {i, 0, nt}];

In[26]:= Do[{He[[i + 1]] = He[[i ]] +


dt ff[ts[[i ]], He[[i ]]]}, {i, 1, nt }]
InterpolatingFunction::dmval :
Input value {10.3781} lies outside the range of data in the interpolating function. Extrapolation will be used. 

InterpolatingFunction::dmval :
Input value {10.3324} lies outside the range of data in the interpolating function. Extrapolation will be used. 

In[27]:= QSe = QH[He];


InterpolatingFunction::dmval :
Input value {10.3781} lies outside the range of data in the interpolating function. Extrapolation will be used. 

InterpolatingFunction::dmval :
Input value {10.3324} lies outside the range of data in the interpolating function. Extrapolation will be used. 

In[28]:= TableForm[Transpose[{ts, QSe}]];


RK4_EjemploVentechow.nb 5

In[29]:= ListLinePlot[{Transpose[{ts, Hin}], Transpose[{ts, QSe}]},


PlotLegends → {"Entrada", "Salida"}]

350

300

250

200 Entrada
Out[29]=

150
Salida

100

50

2000 4000 6000 8000 10 000 12 000 14 000

Método de Runge Kutta 4 Orden


In[30]:= Hr = Table[0, {i, 0, nt}];

In[31]:= DoK1 = ff[ts[[i ]], Hr[[i ]]];


dt dt
K2 = ffts[[i ]] + , Hr[[i ]] + K1;
2 2
dt dt
K3 = ffts[[i ]] + , Hr[[i ]] + K2;
2 2
K4 = ff[ts[[i ]] + dt, Hr[[i ]] + dt K3];
Hr[[i + 1]] = Hr[[i ]] +
dt
(K1 + 2 K2 + 2 K3 + K4), {i, 1, nt }
6
In[32]:= QSr = QH[Hr];

In[33]:= TableForm[Transpose[{ts, QSr}]];


6 RK4_EjemploVentechow.nb

In[34]:= ListLinePlot[{Transpose[{ts, Hin}], Transpose[{ts, QSr}]},


PlotLegends → {"Entrada", "Salida"}]

350

300

250

200 Entrada
Out[34]=

150
Salida

100

50

2000 4000 6000 8000 10 000 12 000 14 000

NDSolve
In[35]:= sol = NDSolveValue[{HT '[tt] ⩵ ff[tt, HT[tt]], HT[0] == 0}, HT, {tt, 0, tmax}]

Domain: {{0., 1.50 × 104 }}


Out[35]= InterpolatingFunction 
Output: scalar

In[36]:= QSND = QH[sol[t]];

In[37]:= Plot[{Ents[t], QH[sol[t]]}, {t, 0., 15 000.}]

350

300

250

200
Out[37]=

150

100

50

2000 4000 6000 8000 10 000 12 000 14 000


RK4_EjemploVentechow.nb 7

Comparación
In[38]:= ListLinePlot[{Transpose[{ts, Hin}], Transpose[{ts, QS}],
Transpose[{ts, QSe}], Transpose[{ts, QSr}], Transpose[{ts, QSND}]},
PlotLegends → {"Entrada", "RK3", "Euler", "RK4", "NDSolve"},
Frame → True, GridLines → Automatic]

350

300

250 Entrada
RK3
200
Out[38]= Euler
150
RK4
100 NDSolve
50

0
0 2000 4000 6000 8000 10 000 12 000 14 000

You might also like