Está en la página 1de 14

Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 28 Dr. Pilar Nuñez B.

ñez B. Métodos Numéricos para Ecuaciones Diferenciales 1

VII. MÉTODOS NUMÉRICOS PARA ECUACIONES


DIFERENCIALES ORDINARIAS
7.1 PROBLEMA. Resolver:
 y(x)  f (x, y(x)), x  [a, b]
(P) : 
 y(a)  

7.2 TEOREMA (Existencia y unicidad de la solución). Sean



D  (x, y)  ¡ 2
/ a  x  b, y  ¡ , f : D  ¡ 
 ¡ continua sobre D.
f
Si  L en D  el problema (P) tiene una única solución para x[a,b].
y

7.3 MÉTODOS DE UN PASO


ba
Consideremos los puntos de la red: x i  a  ih , h , i  0, n
n
Computacionalmente determinaremos w i  y(x i )
Observar que w i 1  y(x i 1 )  y(x i  h)

7.3.1 MÉTODO DE EULER


Del problema (P):
y(x  h)  y(x)
 y(x)  f (x, y(x))
h
 y(x  h)  y(x)  h f (x, y(x))
 y(x i  h)  y(x i )  hf (x i , y(x i ))

Considerando que w i  y(x i ) y w i 1  y(x i 1 )  y(x i  h) obtenemos el siguiente


esquema
 w i 1  w i  h f (x i , w i )

w0  
para automatizarlo.

7.3.2 MÉTODO DEL PUNTO MEDIO O MÉTODO DE EULER


MODIFICADO
La regla de la mitad para calcular integrales es
b ab

a
g(x) dx  (b  a) g 
 2 

Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 2 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 27

Y f

a ab b X
2

Aplicaremos esta fórmula al problema para deducir el esquema respectivo.


x h x h
y(x)  f (x, y)   x
y(u)du   x
f (u, y(u))du

 y(u) | x h
x 
 2x  h
(x  h  x) f 
 2
, y(
2x  h 
2
)

 h h 
 y(x  h)  y(x)  h f  x  , y(x  ) 
 2 2 
Aplicando el método de Euler a y(x+h/2) se obtiene
 h h 
 y(x  h)  y(x)  h f  x  , y(x)  f (x, y(x)) 
 2 2 
 h h 
 y(x i  h)  y(x i )  h f  x i  , y(x i )  f (x i , y(x i )) 
 2 2 
Ahora hacemos w i  y(x i ) y obtenemos
w0  

  h h  i=0,...,n-1
 w i 1  w i  h f  x i  2 , w i  2 f (x i , w i ) 
  

w0  

k1  h f (x i , w i )
o   h 1  i=0,...,n-1
k 2  h f  x i  2 , w i  2 k1 
  
 w i 1  w i  k 2

7.3.3 MÉTODO DE HEUN O MÉTODO DE RUNGE-KUTTA DE


SEGUNDO ORDEN
Para deducir este esquema aplicaremos el método de integración
trapezoidal
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 26 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 3

b ba
System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
a
g(x)dx 
2
[g(a)  g(b)]
InitializeDrawingArea(); Del problema (P) tenemos
} x h x h

private void InitializeDrawingArea() {


y(x)  f (x, y)   x
y(u)du   x
f (u, y(u))du

Graphics oGraphics; h
oGraphics = Graphics.FromImage(area);
 y(x  h)  y(x) 
2
f (x, y(x))  f (x  h, y(x  h))
myPen.Color = Color.AliceBlue; Aplicando el método de Euler a y(x+h) se tiene
for ( int x = 0; x < this.Width; x++) { h
oGraphics.DrawLine(myPen, x, 0, x, this.Height);  y(x  h)  y(x)   f (x, y(x))  f (x  h, y(x)  hf (x, y(x)))
2
}
h
//oGraphics.Dispose();  y(x i  h)  y(x i )  f (x i , y(x i ))  f (x i  h, y(x i )  hf (x i , y(x i ))) 
//this.Invalidate(); 2
} Haciendo w i  y(x i ) tenemos
w0  
private void Form1_Paint(object sender, 
 h
 w i 1  w i  2  f (x i , w i )  f (x i 1 , w i  h f (x i , w i ))
System.Windows.Forms.PaintEventArgs e) {
Graphics g=e.Graphics; 
g.DrawImage( area,
450,90, w0  
area.Width, 
area.Height); k1  h f (x i , w i )
//g.Dispose(); o k 2  h f  x i  h, w i  k1  i=0,...,n-1
} 
 w  w  1 [k  k ]
 i 1 i
2
1 2

7.3.4 MÉTODO DE RUNGE-KUTTA CLÁSICO DE CUARTO ORDEN


Para deducir este esquema aplicaremos el método de integración numérica de
Simpson 1/3
b (b  a) / 2  ab 
 a
g(x)dx 
3
 g(a)  4 g 
  2   g(b) 
 
b ba  ab 
  a
g(x)dx 
6 
g(a)  4 g    g(b) 
 2  
x h x h
y(x)  f (x, y)   x
y(u)du   x
f (u, y(u))du
x h
 y(x  h)  y(x)   x
f (u, y(u))du

h
 y(x  h)  y(x)  [f (x, y(x))  4f (x  h2 , y(x  h2 ))  f (x  h, y(x  h))]
6
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 4 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 25

Descomponiendo el segundo término del corchete textBoxN.Clear();


h textBoxM.Clear();
 y(x  h)  y(x)  [f (x, y(x))  2f (x  h , y(x  h ))  textBoxTol.Clear();
6 2 2 (1) listBox1.Items.Clear();
h h
2f (x  , y(x  ))  f (x  h, y(x  h))]
2 2 }
Al aplicar diferencia hacia adelante a y’(x), se tiene private void buttonDibujar_Click(object sender, System.EventArgs e) {
h listBox1.Items.Clear();
y(x  )  y(x) Graphics g=Graphics.FromImage(area);
y(x)  2 int ox=area.Width/2;
h/2 int oy=area.Height/2;
Pero y(x)  f (x, y(x)) Pen pen=new Pen(Color.Red);
Entonces, reemplazando en la ecuación anterior y despejando, se tiene g.DrawLine(pen, new Point(0,oy),new Point(2*ox,oy));
h g.DrawLine(pen, new Point(ox,0),new Point(ox,2*oy));
y(x  h2 )  y(x)  f (x, y(x)) (2) g.DrawString("Eje X",new Font("Arial",8),Brushes.Blue,2*(ox-15),oy);
2
Al aplicar diferencia hacia atrás a y’(x+h/2) se tiene g.DrawString("Eje Y",new Font("Arial arrow",8),Brushes.Blue,ox-25,0);
h
y(x  )  y(x) float fesc=40;
h 2
y(x  ) 
2 h/2 ArrayList points=new ArrayList();
h h h PointF[] puntos;
pero y(x  )  f (x  , y(x  ))
2 2 2
Entonces, reemplazando en la ecuación anterior y despejando se tiene string archivo=Convert.ToString(textBoxAr.Text);
h h h StreamReader re=File.OpenText(archivo);
y(x  h2 )  y(x)  f (x  , y(x  )) int n=Convert.ToInt32(textBoxNN.Text);
2 2 2
double[,] ww=new double[n,2];
h h h
 y(x  h2 )  y(x)  f (x  , y(x)  f (x, y(x))) (3) for(int k=0;k<2;k++){
2 2 2 for(int i=0;i<n;i++){
Al aplicar diferencia centra a y’(x+h/2) se tiene ww[i,k]=Convert.ToDouble(re.ReadLine());
h y(x  h)  y(x) listBox1.Items.Add("w["+i+","+k+"]= "+ww[i,k]);
y(x  ) 
2 h }
h h h }
pero y(x  )  f (x  , y(x  ))
2 2 2
for(int i=0;i<n;i++){
Entonces, reemplazando en la ecuación anterior y despejando se tiene
points.Add(new PointF((float)ox+fesc*(float)ww[i,0],(float)oy-
h h fesc*(float)ww[i,1]));
y(x  h)  y(x)  hf (x  , y(x  ))
2 2 }
Es esta ecuación reemplazamos y(x+h/2) según (3) y obtenemos puntos=(PointF[])points.ToArray(points[0].GetType());
h h h h g.DrawLines(pen,puntos);
y(x  h)  y(x)  h f (x  , y(x)  f (x  , y(x)  f (x, y(x)))) (4)
2 2 2 2 this.Invalidate();
Reemplazando apropiadamente (2), (3) y (4) en (1) se tiene }

private void Form1_Load(object sender, System.EventArgs e) {


area = new Bitmap(
this.Width/3,
this.Height/3,
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 24 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 5

l[i,i-1]=a[i,i-1]; h
l[i,i]=a[i,i]-l[i,i-1]*u[i-1,i]; y(x  h)  y(x)  [f (x, y(x))  2f (x  h , y(x)  h f (x, y(x))) 
6 2 2
u[i,i+1]=a[i,i+1]/l[i,i];
} 2f (x  h , y(x)  h f (x  h , y(x)  h f (x, y(x))) 
2 2 2 2
l[n-1,n-2]=a[n-1,n-2];
f (x  h, y(x)  h f (x  h , y(x)  h f (x  h , y(x)  h f (x, y(x))))]
l[n-1,n-1]=a[n-1,n-1]-l[n-1,n-2]*u[n-2,n-1]; 2 2 2 2
//Resuelve el sistema Av=b Luego, sustituyendo x=xi wiy(xi) en la ecuación anterior y haciendo la
//donde A=LU asignación respectiva se obtiene el esquema de Runge-Kutta
//Primero se resuelve Lz=b  w0  
z[0]=a[0,n]/l[0,0];  k
for(int i=1;i<=n-1;i++)  1  h f (x i , w i )
z[i]=(a[i,n]-l[i,i-1]*z[i-1])/l[i,i];  k  h f  x i  12 h, w i  12 k1 
//Resuelve Uv=z  2
v[n-1]=z[n-1];  k3  h f  x i  12 h, w i  12 k 2  i=0,...,n-1

w[n]=w[n]+v[n-1];  k4  h f (x i  h, w i  k 3 )
for(int i=n-2;i>=0;i--){ 
1
v[i]=z[i]-u[i,i+1]*v[i+1]; w  w i   k1  2k 2  2k 3  k 4 
w[i+1]=w[i+1]+v[i];  i 1 6
}
//calculo de la norma euclideana 7.3.5 ALGORITMO DEL MÉTODO DE RUNGE-KUTTA
double s=0; Entrada: a, b, valor, n
for(int i=0;i<=n-1;i++) h=(b-a)/n
s=s+Math.Pow(Math.Abs(v[i]),2); w=valor
double norma=Math.Sqrt(s); x=a
if(norma<tol){ Para i=1,...,n hacer
for(int i=0;i<=n+1;i++) { k1=h*f(x,w)
xx[i]=aa+i*(float)h; k2=h*f(x+h/2,w+k1/2)
listBox1.Items.Add("x["+i+"]="+xx[i]+" k3=h*f(x+h/2,w+k2/2)
"+"w["+i+"]="+w[i]+"\n"); k4=h*f(x+h,w+k3)
sr.WriteLine("{0}",xx[i]); w=w+(k1+2*k2+2*k3+k4)/6
} x=x+h
for(int i=0;i<=n+1;i++) { Salida: i, x, w
sr.WriteLine("{0}",w[i]); fin
}
break; 7.3.6 MÉTODO DE RUNGE KUTTA DE TERCER ORDEN
} w0  
k=k+1; 
} k1  f (x i , w i )

k 2  f (x i  2 h, w i  2 hk1 )
1 1
sr.Close();
} k  f (x  h, w  hk  2hk )
private void button1_Click(object sender, System.EventArgs e){  3 i i 1 2

textBoxA.Clear();  w i 1  w i  h (k1  4k 2  k 3 )
 6
textBoxB.Clear();
textBoxYa.Clear();
textBoxYb.Clear();
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 6 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 23

7.3.7 MÉTODO DE RUNGE KUTTA DE QUINTO ORDEN StreamWriter sr=File.CreateText(arch);


w0   double[,] a=new double[n,n+1];
 double[,] l=new double[n,n];
k1  f (x i , w i ) double[,] u=new double[n,n];
k  f  x  1 h, w  1 hk  double[] v=new double[n];
 2 i 4 i 4 1
double[] z=new double[n];
 3  i 4 i 8 1 8 2
k  f x  1 h, w  1 hk  1 hk
double[] w=new double[n+2];
 float[] xx=new float[n+2];
k 4  f (x i  2 h, w i  2 hk 2  hk 3 )
1 1

 //inicialización de las matrices


k 5  f (x i  4 h, w i  16 hk1  16 hk 4 )
3 3 9
for(int i=0;i<=n-1;i++) {
k  f (x  h, w  3 hk  2 hk  12 hk  12 hk  8 hk ) for(int j=0;j<=n;j++)
 6 i i 7 1 7 2 7 3 7 4 7 5
a[i,j]=0;
 w i 1  w i  h (7k1  32k 3  12k 4  32k 5  7k 6 ) v[i]=0;
 90
z[i]=0;
7.4 MÉTODOS MULTIPASOS }
Sea la ecuación diferencial: y(x)  f (x, y(x)) double h=(b-aa)/(n+1);
w[0]=ya;
Integrando en [x p  j , x p  q ] , se obtiene w[n+1]=yb;
xpk for(int i=1;i<=n;i++)
y(x p  k )  y(x p  j ) 
 xp  j
f (x, y(x))dx (1) w[i]=ya+i*h*(yb-ya)/(b-aa);
int k=1;
Consideremos el polinomio interpolante de Lagrange que aproxima a f,
q
while(k<=m) {

 f (x
//almacenamiento de la matriz tridiagonal
Pq (x)  p  i , y(x p  i )) L i (x) double x=aa+h;
i0 double t=(w[2]-ya)/(2*h);
q
x  x p l a[0,0]=2+h*h*fy1(x,w[1],t);
donde Li (x)  x l 0 p i  x pl
a[0,1]=-1+(h/2)*fy2(x,w[1],t);
a[0,n]=-(2*w[1]-w[2]-ya+h*h*f(x,w[1],t));
l i
for(int i=2;i<=n-1;i++) {
Reemplazando en la ecuación (1): x=aa+i*h;
q

 f (x
xpk xpk t=(w[i+1]-w[i-1])/(2*h);
y(x p  k )  y(x p  j ) 
 xp  j
Pq (x) dx 
i0
p  i , y(x p  i ))
 xp j
Li (x) dx a[i-1,i-1]=2+h*h*fy1(x,w[i],t);
a[i-1,i]=-1+(h/2)*fy2(x,w[i],t);
q a[i-1,i-2]=-1-(h/2)*fy2(x,w[i],t);
h  i 0
qi f (x p i , y(x p i ))
}
a[i-1,n]=-(2*w[i]-w[i+1]-w[i-1]+h*h*f(x,w[i],t));

q x=b-h;
x  x pm
 x
xp  k xpk
1 1
donde qi 
h  xp j
Li (x)dx 
h xp j
m0 p i  x pm
dx t=(yb-w[n-1])/(2*h);
a[n-1,n-1]=2+h*h*fy1(x,w[n],t);
mi a[n-1,n-2]=-1-(h/2)*fy2(x,w[n],t);
q
x  mh  x p a[n-1,n]=-(2*w[n]-w[n-1]-yb+h*h*f(x,w[n],t));
 
xpk
1
Sea x p  m  x p  ( m)h  qi  dx //método de crout
h xp j (m  i)h l[0,0]=a[0,0];
m0
mi u[0,1]=a[0,1]/l[0,0];
for(int i=1;i<=n-2;i++) {
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 22 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 7

printf("\n a(%2d,%2d)=%10.8f",i,j,a[i][j]); x  xp
q
}
printf("\n");
Sea s 
h
 yp  k  yp  j  h 
i 0
qi f (x p i , y(x p i ))

} q

  m  i ds
k
sm
donde qi 
double f(double x,double y1, double y2){ j
m 0
return (32+2*x*x*x-y1*y2)/8; m i
}
double fy1(double x,double y1, double y2){ 1) MÉTODOS DE ADAMS-BASHFORTH (EXPLÍCITOS)
return -y2/8; k=1, j=0, q=0,1,2,3,…..
} q

  m  i ds
1
double fy2(double x,double y1, double y2){ sm
qi 
return -y1/8; 0
m 0
} m i

qi \ i 0 1 2 3 4
PROGRAMA EN C#, QUE RESUELVE EL PROBLEMA LINEAL DE UNA 0i 1
ECUACION DIFERENCIAL ORDINARIA CON CONDICIONES DE 1i 3/2 -1/2
FRONTERA.
2i 23/12 -16/12 5/12
using System.IO;
namespace DifFinitasCSharp{ 3i 55/24 -59/24 37/24 -9/24
public class Form1 : System.Windows.Forms.Form { 4i 1901/720 -2774/720 2616/720 -1274/720 251/720
q
private Bitmap area;
private Pen myPen;
//Definición de funciones (o métodos)
y p 1  y p  h 
i 0
qi f (x p i , y(x p i ))

double f(double x,double y1, double y2){


return (32+2*x*x*x-y1*y2)/8; Para q=0 resulta el método de Euler: y p 1  y p  h f (x p , y p )
}
double fy1(double x,double y1, double y2){ Para q=1 resulta: y p 1  y p  h2  f (x p , y p )  f (x p 1 , y p 1 ) 
return -y2/8;
} 2) MÉTODOS DE ADAMS-MOULTON (IMPLÍCITOS)
double fy2(double x,double y1, double y2){ k=0, j=1, q=0, 1, 2, ….
return -y1/8; q

  m  i ds
0
sm
} qi 
float g1(float x){ 1
m0
return (float)Math.Exp(-x*x)*(float)Math.Sin(x); mi

} qi \ i 0 1 2 3 4
private void ButtonCalcular_Click(object sender, System.EventArgs e){ 0i 1
float aa=Convert.ToSingle(textBoxA.Text); 1i 1/2 1/2
float b=Convert.ToSingle(textBoxB.Text); 2i 5/12 8/12 -1/12
double ya=Convert.ToDouble(textBoxYa.Text);
3i 9/24 19/24 -5/24 1/24
double yb=Convert.ToDouble(textBoxYb.Text);
int n=Convert.ToInt32(textBoxN.Text); 4i 251/720 646/720 -264/720 106/720 -19/720
q


int m=Convert.ToInt32(textBoxM.Text);
double tol=Convert.ToDouble(textBoxTol.Text); y p  y p 1  h qi f (x p  i , y(x p i ))
string arch=Convert.ToString(textBoxIa.Text); i 0
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 8 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 21

q l[i][i-1]=a[i][i-1];
 y p 1  y p  h 
i 0
qi f (x p i , y(x p i )) l[i][i]=a[i][i]-l[i][i-1]*u[i-1][i];
u[i][i+1]=a[i][i+1]/l[i][i];
}
3) MÉTODOS DE NYSTROEM (EXPLÍCITO) l[n-1][n-2]=a[n-1][n-2];
k=1, j=1, q=0, 1, 2, …. l[n-1][n-1]=a[n-1][n-1]-l[n-1][n-2]*u[n-2][n-1];
q //Resuelve el sistema Av=b

  m  i ds
1
sm //donde A=LU
qi 
1
m0
//Primero se resuelve Lz=b
mi z[0]=a[0][n]/l[0][0];
qi \ i 0 1 2 3 4 for(i=1;i<=n-1;i++)
0i 2 z[i]=(a[i][n]-l[i][i-1]*z[i-1])/l[i][i];
//Resuelve Uv=z
1i 2 0
v[n-1]=z[n-1];
2i 7/3 -2/3 1/3
w[n]=w[n]+v[n-1];
3i 8/3 -5/3 4/3 -1/3 for(i=n-2;i>=0;i--) {
4i 269/90 -266/90 294/90 -146/90 29/90 v[i]=z[i]-u[i][i+1]*v[i+1];
q
w[i+1]=w[i+1]+v[i];
y p 1  y p 1  h 
i0
qi f (x p i , y(x p i )) }
//calculo de la norma euclideana
s=0;
4) MÉTODOS DE MILNE-THOMPOSON (IMPLÍCITO) for(i=0;i<=n-1;i++)
k=0, j=2, q=0, 1, 2, …. s=s+pow(fabs(v[i]),2);
q norma=sqrt(s);
sm
  m  i ds
0
qi  printf("Solucion:\n");
2
m0 if(norma<tol){
mi for(i=0;i<=n+1;i++){
qi \ i 0 1 2 3 4 xx[i]=aa+i*h;
0i 0 printf("%3d %5.1f% 17.10f\n",i,xx[i],w[i]);
1i 0 2 }
2i 1/3 4/3 1/3 break;
3i 1/3 4/3 1/3 0 }
k=k+1;
4i 29/90 124/90 24/90 4/90 -1/90
q
}


//Imprime en pantalla los valores de la matriz: a
yp  yp 2  h qi f (x p i , y(x p i )) printf("Matriz ampliada a:\n");
i0 for(i=0;i<=n-1;i++){
q
for(j=0;j<=n;j++)
 yp 1  yp1  h  i 0
qi f (x p i , y(x p i )) printf("%12.8f",a[i][j]);
printf("\n");
}
5) MÉTODO DE PREDICTOR-CORRECTOR //valores de la matriz ampliada
h printf("\n");
y%p 1 { y p 
12

23f (x p , yp )  f (x p 1 , y p 1 )  5f (x p  2 , y p  2 )  for(i=0;i<=n-1;i++) {
q  2 (A  B)
for(j=0;j<=n;j++)
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 20 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 9

l[i]=new double[n]; h
u=new double*[n]; y%
p 1 { yp 
24

9f (x p 1 , y% 
p 1 )  19f (x p , y p )  5f (x p 1 , y p 1 )  f (x p  2 , y p  2 )
q  3 (A  M)
for(i=0;i<n;i++)
u[i]=new double[n];
v=new double[n]; Esto es una combinación de los métodos de Adams-Bashforth para q=2 y Adam-
z=new double[n]; Moulton para q=3. En cada paso se necesita solamente dos evaluaciones de la
w=new double[n+2]; función f.
xx=new double[n+2];
//inicializacion de las matrices 6) OTRO MÉTODO DE PREDICTOR-CORRECTOR
h
for(i=0;i<=n-1;i++){
for(j=0;j<=n;j++)
y%p 1  
{ y p 1  7f (x p , yp )  2f (x p 1 , y p 1 )  f (x p  2 , y p  2 )
3

q  2 (N)
a[i][j]=0;
h
v[i]=0; y%
p 1 { yp 
3
 
f (x p 1 , y%p 1 )  4f (x p , y p )  f (x p 1 , y p 1 )
z[i]=0; q  3 (M  T)
}
h=(b-aa)/(n+1); 7) DEFINICIÓN DE MÉTODO MULTIPASO LINEAL EN GENERAL
w[0]=alfa; Son de la forma:
w[n+1]=beta; w j r  a r 1w j r  L  a 0 w j  b r f (x j r , w j r )  L  b0 f (x j , w j )
for(i=1;i<=n;i++)
w[i]=alfa+i*h*(beta-alfa)/(b-aa);
7.5 MÉTODOS DE DIFERENCIAS FINITAS PARA ECUACIONES
k=1;
DIFERENCIALES ORDINARIAS CON CONDICIONES DE
while(k<=m) {
FRONTERA
//almacenamiento de la matriz tridiagonal
x=aa+h;
7.5.1 PROBLEMA LINEAL
t=(w[2]-alfa)/(2*h);
Sea el problema lineal
a[0][0]=2+h*h*fy1(x,w[1],t);
a[0][1]=-1+(h/2)*fy2(x,w[1],t);  y(x)  p(x)y(x)  q(x)y(x)  r(x), a  x  b

a[0][n]=-(2*w[1]-w[2]-alfa+h*h*f(x,w[1],t));  y(a)  
for(i=2;i<=n-1;i++){  y(b)  

x=aa+i*h;
t=(w[i+1]-w[i-1])/(2*h); Dividamos [a, b] en n+1 subintervalos de igual longitud
a[i-1][i-1]=2+h*h*fy1(x,w[i],t); xi=a+ih, i=0,..., n+1
a[i-1][i]=-1+(h/2)*fy2(x,w[i],t); ba
h
a[i-1][i-2]=-1-(h/2)*fy2(x,w[i],t); n 1
a[i-1][n]=-(2*w[i]-w[i+1]-w[i-1]+h*h*f(x,w[i],t)); y(xi)  wi
} 1 1
x=b-h; y(x i )  2  y(x i 1 )  2y(x i )  y(x i 1 )  2  w i 1  2w i  w i 1 
h h
t=(beta-w[n-1])/(2*h);
1 1
a[n-1][n-1]=2+h*h*fy1(x,w[n],t); y(x i ) 
2h
 y(x i 1 )  y(x i 1 )  2h  w i 1  w i 1 
a[n-1][n-2]=-1-(h/2)*fy2(x,w[n],t);
a[n-1][n]=-(2*w[n]-w[n-1]-beta+h*h*f(x,w[n],t)); Al reemplazar estas afirmaciones en la ecuación diferencial se tiene
//método de crout y(x i )  p(x i )y(x i )  q(x i )y(x i )  r(x i )
l[0][0]=a[0][0]; w i 1  2w i  w i 1 p(x i )[w i 1  w i 1 ]
u[0][1]=a[0][1]/l[0][0];  2
  q(x i )w i  r(x i )
h 2h
for(i=1;i<=n-2;i++){
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 10 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 19

Reagrupando se tiene fx(t+h,f,w0+k3[0],w1+k3[1]);


 1  p(x i )  w i 1  (2  h q(x i ))w i  1  p(x i )  w i 1   h r(x i )
h
2
2 h
2
2 for(j=0;j<=m;j++) k4[j]=h*f[j];
for(j=0;j<=m;j++)
w0   w[j]=w[j]+(k1[j]+2*k2[j]+2*k3[j]+k4[j])/6;
w n 1   i  1,L , n t=a+i*h;
w0=w[0];
w1=w[1];
Al desarrollar este esquema para i=1,...,n se obtiene el siguiente sistema de
cout<<i<<" "<<t<<" ";
ecuaciones lineales
for(j=0;j<=m;j++)
Ax  b cout<<w[j]<<" ";
donde cout<<endl;
 2  h 2 q(x1 ) 1  h2 p(x1 ) 0 L 0  }
 2
 }
 1  h2 p(x 2 ) 2  h q(x 2 ) 1  h2 p(x 2 ) O M 
A 0 O 0  PROGRAMA EN VISUAL C++ NET
 
 M O 1  h2 p(x n 1 )  #include "stdafx.h"
  #include<iostream> //cout, cin
 0 L 0 1  h2 p(x n ) 2  h 2 q(x n )  #include<math.h> //pow, fabs, sqrt
 h 2 r(x1 )  (1  h2 p(x1 ))  #include<stdio.h> //printf
 w1  using namespace std;
  w 
 h 2 r(x 2 )  double f(double x,double y1, double y2);
 2 

b M  x M double fy1(double x,double y1, double y2);
   double fy2(double x,double y1, double y2);
  h 2 r(x n 1 )   w n 1 
  void main(){
w  double aa,b,alfa,beta,tol,h,x,t;
2
 h r(x n )  (1  2 p(x n ))
h
 n 
double s;
double norma;
TEOREMA. Sean p, q y r funciones continuas sobre [a, b]. Si q(x)0 sobre [a, b] double *xx,*v,*w,*z;
 el sistema lineal tridiagonal anterior tiene solución única siempre que h<2/L, double **a,**l,**u;
donde L  max | p(x) | int i,k,n,m,j;
a x b
cout<<"Ingrese los extremos del intervalo [a b]: ";
7.5.2 ALGORITMO cin>>aa>>b;
ENTRADA: aa, bb, ,  cout<<"Ingrese los valores de la funcion en: a y b: ";
SALIDA xi, wi cin>>alfa>>beta;
h=(bb-aa)/(n+1) cout<<"Ingrese la tolerancia: ";
x=aa+h cin>>tol;
a1=2+h2q(x) cout<<"Ingrese el numero maximo de iteraciones: ";
b1=-1+(h/2)p(x) cin>>m;
cout<<"Ingrese el valor de n (numero de ecuaciones): ";
d1=-h2r(x)+(1+(h/2)p(x))
cin>>n;
Para i=2,..., n-1 hacer
a=new double*[n];
x=aa+ih
ai=2+h2q(x) for(i=0;i<n;i++)
bi=-1+(h/2)p(x) a[i]=new double[n+1];
l=new double*[n];
ci=-1-(h/2)p(x)
for(i=0;i<n;i++)
di=-h2r(x)
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 18 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 11

k4=h*fu(t+h,w+k3); fin
w=w+(k1+2*k2+2*k3+k4)/6; x=bb-h
t=a+(i+1)*h; an=2+h2q(x)
cout<<i+1<<" "<<t<<" "<<w<<" "<<endl; cn=-1-(h/2)p(x)
salida<<t<<" "<<w<<" "<<endl; dn=-h2r(x)+(1-(h/2)p(x))
} Resuelve el sistema tridiagonal usando el método de Crout
} l1=a1
u1=b1/a1
double fu(double t, double y){ para i=2,...,n-1 hacer
return y-t*t+1; li=ai – ciui-1
} ui=bi/li
fin
PROGRAMA PARA RESOLVER UN SISTEMA DE ECUACIONES ln=an - cnun-1
DIFERENCIALES ORD. CON EL MÉTODO RUNGE-KUTTA. z1=d1/l1
#include<iostream.h> para i=2,...,n hacer
const int m=1; zi=(di - cizi-1)/li
void fx(double t,double f[m+1],double w0,double w1){ fin
f[0]=-4*w0+3*w1+6; w0=
f[1]=-2.4*w0+1.6*w1+3.6; wn+1=
} wn=zn
void main(){ Para i=n-1,...,1 hacer
double a,b,h,t,f[m+1],alfa[m+1],k1[m+1],k2[m+1]; wi=zi – uiwi+1
double k3[m+1],k4[m+1],w0,w1,w[m+1]; fin
int n;
cout<<"Ingrese el intervalo [a,b]: "; 7.5.3 PROBLEMA NO LINEAL
cin>>a>>b; Sea el problema no lineal
cout<<"Ingrese los valores iniciales: ";
 y(x)  f (x, y(x), y(x)) a  x  b
for(int j=0;j<=m;j++) 
cin>>alfa[j];  y(a)  
cout<<"Ingrese el numero de subintervalos: ";  y(b)  

cin>>n;
Sean
h=(b-a)/n;
xi=a+ih, i=0,..., n+1
t=a;
for(j=0;j<=m;j++) w[j]=alfa[j]; ba
h
w0=w[0]; n 1
w1=w[1]; y(xi)  wi
cout<<"i"<<" "<<"t"<<" "<<"w0"<<" "<<"w1"<<endl; Luego, de la ecuación diferencial se tiene
cout<<0<<" "<<t<<" "<<w0<<" "<<w1<<" "<<endl; y(x i )  f (x i , y(x i ), y(x i ))
for(int i=1;i<=n;i++){ w i 1  2w i  w i 1 w  w i 1 

fx(t,f,w0,w1);   f  x i , w i , i 1 
for(j=0;j<=m;j++) k1[j]=h*f[j]; h2  2h 
fx(t+h/2,f,w0+k1[0]/2,w1+k1[1]/2);
for(j=0;j<=m;j++) k2[j]=h*f[j];
fx(t+h/2,f,w0+k2[0]/2,w1+k2[1]/2);
for(j=0;j<=m;j++) k3[j]=h*f[j];
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 12 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 17

 w  w i 1  cout<<0<<" "<<t[0]<<" "<<w[0]<<" "<<endl;


w i 1  2w i  w i 1  h 2 f  x i , w i , i 1  salida<<t[0]<<" "<<w[0]<<" "<<endl;
 2h  for(i=0;i<n;i++) {
 w0   i=1,...,n k1=h*fu(t[i],w[i]);
w1   k2=h*fu(t[i]+h/2,w[i]+k1/2);
k3=h*fu(t[i]+h/2,w[i]+k2/2);
Al desarrollar este esquema para i=1,...,n, se obtiene un sistema de ecuaciones NO
k4=h*fu(t[i]+h,w[i]+k3);
lineales de orden nn. w[i+1]=w[i]+(k1+2*k2+2*k3+k4)/6;
Este sistema se puede resolver aplicando el método de Newton. t[i+1]=a+(i+1)*h;
cout<<i+1<<" "<<t[i+1]<<" "<<w[i+1]<<" "<<endl;
7.6 ANÁLISIS NUMÉRICO DE LOS MÉTODOS salida<<t[i+1]<<" "<<w[i+1]<<" "<<endl;
}
DEFINICIÓN. El error de truncamiento local de un método de un paso es }
1 double fu(double t, double y){
h (x, y)  (y(x  h)  y(x))  (x, y; h)
h return y-t*t+1;
DEFINICIÓN. Un método de un paso es consistente con la ecuación diferencial si }
lim (x, y; h)  f (x, y)
h 
MÉTODO DE RUNGE-KUTTA EN C++6.0 SIN PUNTEROS PARA
DEFINICIÓN. El método de un paso es de orden p si h (x, y)  O(h p ) INGRESAR EL NUMEROS DE NODOS EN EL INTERVALO
DEFINICIÓN. El error de truncamiento global está definido por //a=0, b=2, d=0.5
g j (h)  y(x j )  w j //Ingresar el numero de puntos en el intervalo
#include<iostream.h>
DEFINICIÓN. Un método de un paso es convergente si
#include<fstream.h>
lim y(x j )  w j  0 double fu(double t,double y);
h 0
void main(){
El método tiene el orden de convergencia p si g j (h)  O(h p ) double h,a,b,d,t,w;
DEFINICIÓN. El error de truncamiento local para un método multipaso lineal es double k1,k2,k3,k4;
r int i,n;
 b f (x  ih, y(x  ih))
1
h (x, y)  [y(x  rh)  a r 1 y(x  (r  1)h)  L  a 0 y(x)]  i ofstream salida;
h i 0 salida.open("datos2");
DEFINICIÓN. El método multipaso es consistente si h (x, y)  (h) cout<<"ingrese el intervalo: ";
donde lim (h)  0 cin>>a>>b;
h 0 cout<<"Ingrese el valor de la condicion inicial: ";
DEFINICIÓN. Un método multipaso es de orden p si  (h)  O(h p ) cin>>d;
DEFINICIÓN. El método de r pasos cout<<"Ingrese el numero de puntos del intervalo menos 1: ";
cin>>n;
y j r  a r 1 y j r 1  L  a 0 y j  F(h, f )(x j , y j r ,K , y j )
h=(b-a)/n;
satisface la condición de estabilidad si el polinomio t=a;
 (u)  u r  a r 1u r 1  L  a 0 w=d;
tiene las propiedades cout<<0<<" "<<t<<" "<<w<<" "<<endl;
salida<<t<<" "<<w<<" "<<endl;
i)  ( )  0    1
for(i=0;i<n;i++) {
ii)  ()  0 ,   1   es cero simple de ,  ( )  0 k1=h*fu(t,w);
k2=h*fu(t+h/2,w+k1/2);
k3=h*fu(t+h/2,w+k2/2);
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 16 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 13

cout<<"Ingrese el valor de la condicion inicial: "; PROGRAMAS


cin>>d;
h=(b-a)/n; MÉTODO DE RUNGE-KUTTA EN MATLAB
t[0]=a; function y=rungekutta2(fu,a,b,n,d)
w[0]=d; %METODO DE RUNGE KUTTA
cout<<0<<" "<<t[0]<<" "<<w[0]<<" "<<endl; %Se escribe: rungekutta2('fu',0,2,10,0.5)
salida<<t[0]<<" "<<w[0]<<" "<<endl; %fu:función con dos variables independientes(t,y)
for(i=0;i<n;i++) { %fu debe ser creado en un archivo
k1=h*fu(t[i],w[i]); %a y b extremos del intervalo
k2=h*fu(t[i]+h/2,w[i]+k1/2); %n es el numero de subintervalos
k3=h*fu(t[i]+h/2,w[i]+k2/2); %d el valor inicial
k4=h*fu(t[i]+h,w[i]+k3); fprintf(' i ti wi\n')
w[i+1]=w[i]+(k1+2*k2+2*k3+k4)/6; h=(b-a)/n;
t[i+1]=a+(i+1)*h; t(1)=a;
cout<<i+1<<" "<<t[i+1]<<" "<<w[i+1]<<" "<<endl; w(1)=d;
salida<<t[i+1]<<" "<<w[i+1]<<" "<<endl; fi=fopen('datos', 'w');
} for i=1:n+1
} k1=h*(feval(fu,t(i),w(i)));
double fu(double t, double y){ k2=h*(feval(fu,t(i) + h/2,w(i) + k1/2));
return y-t*t+1; k3=h*(feval(fu,t(i) + h/2,w(i) + k2/2));
} k4=h*(feval(fu,t(i) + h,w(i) + k3));
w(i+1)=w(i)+(k1+2*k2+2*k3+k4)/6;
MÉTODO DE RUNGE-KUTTA EN C++6.0 CON PUNTEROS PARA t(i+1)=a+i*h;
INGRESAR EL NUMERO DE NODOS DEL INTERVALO MEDIANTE EL fprintf(' %5i %3.1f %3.10f\n',i,t(i),w(i));
TECLADO (ALMACENADO EN MATRICES FILA) fprintf(fi,' %5i %3.1f %3.10f\n',i,t(i),w(i));
#include<iostream.h> end
#include<fstream.h> plot(t,w);
double fu(double t,double y);
void main(){ %%%%%%%%%%%%%%%
double h,a,b,d,*t,*w; function u=fu(t,y)
double k1,k2,k3,k4; u=y-t.^2+1;
int i,n; %a=0, b=2, n=10, d=0.5
ofstream salida;
salida.open("datos"); MÉTODO DE EULER EN MATLAB
cout<<"ingrese el intervalo: "; function E=euler(f,a,b,ya,M)
cin>>a>>b; %a,b son los extremos del intervalo
cout<<"Ingrese el valor de la condicion inicial: "; %ya es la condicion inicial
cin>>d; %M es el número de subintervalos; M+1 número de nodos
cout<<"Ingrese el numero de puntos del intervalo: "; %E=[T' Y']son los puntos de la funcion solucion
cin>>n; %T es el vector de abscisas de los puntos de la solucion
w=new double[n]; %Y es el vector de ordenadas de los puntos de solucion
t=new double[n]; %E=euler('difl',-pi,pi,0,10)
h=(b-a)/n; h=(b-a)/M;
t[0]=a; T=zeros(1,M+1);
w[0]=d; Y=zeros(1,M+1);
Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 14 Dr. Pilar Nuñez B. Métodos Numéricos para Ecuaciones Diferenciales 15

T=a:h:b; %resuelve el sistema con eliminacion gaussiana hacia adelante


Y(1)=ya; z(1)=d(1)/L(1);
for j=1:M; for i=2:n
Y(j+1)=Y(j)+h*feval(f,T(j),Y(j)); z(i)=(d(i)-c(i)*z(i-1))/L(i);
end end
E=[T' Y'];
plot(E(:,1),E(:,2)) w(1)=alfa;
w(n+2)=beta;
%%%%%%%%%%%%%%%%%%%%%%%%% %resuelve el sistema con eliminacion gaussiana hacia atras
function f=difl(t,y) w(n+1)=z(n);
f=(t-y)/2; for i=n:-1:2
w(i)=z(i-1)-U(i-1)*w(i+1);
MÉTODO DE DIFERENCIAS FINITAS EN MATLAB end
aa=input('Ingrese el valor de a:'); fprintf('i xi wi\n')
bb=input('Ingrese el valor de b:'); for i=1:n+2
alfa=input('Ingrese el valor de alfa:'); fprintf('%3d %4.3f %5.6f\n',i,x(i),w(i));
beta=input('Ingrese el valor de beta:'); end
n=input('Ingrese el valor de n:'); plot(x,w)
h=(bb-aa)/(n+1); %%%%%%%%%%%%%%%%
x(1)=aa; Ejemplo. Resolver
x(2)=aa+h;   2 2 sen(ln x)
x(n+2)=bb;  y   x y  x 2 y  x2
a(1)=2+h^2*q(x(2)); 
b(1)=-1+(h/2)*p(x(2));  y(1)  2
d(1)=-h^2*r(x(2))+(1+(h/2)*p(x(2)))*alfa;  y(2)  2

for i=2:n-1 n  9
x(i+1)=aa+i*h;
Su solución analítica es:
a(i)=2+h^2*q(x(i+1));
b(i)=-1+(h/2)*p(x(i+1)); 1 3sen(ln x)  cos(ln x)
y  1.1392070132x  0.03920701320 2 
c(i)=-1-(h/2)*p(x(i+1)); x 10
d(i)=-h^2*r(x(i+1));
end MÉTODO DE RUNGE-KUTTA EN C++6.0
x(n+1)=bb-h; //Resuelve el problema y’=y-t2+1, 0≤t≤2, y(0)=0.5
a(n)=2+h^2*q(x(n+1)); #include<iostream.h>
c(n)=-1-(h/2)*p(x(n+1)); #include<fstream.h>
d(n)=-h^2*r(x(n+1))+(1-(h/2)*p(x(n+1)))*beta; const n=10;
%resuelve el sistema mediante el metodo de Crout double fu(double t,double y);
%factorizacion directa de crout void main(){
L(1)=a(1); double h,a,b,d,t[n],w[n];
U(1)=b(1)/a(1); double k1,k2,k3,k4;
for i=2:n-1 int i;
L(i)=a(i)-c(i)*U(i-1); ofstream salida;
U(i)=b(i)/L(i); salida.open("kutta");
end cout<<"ingrese el intervalo: ";
L(n)=a(n)-c(n)*U(n-1); cin>>a>>b;

También podría gustarte