Академический Документы
Профессиональный Документы
Культура Документы
h=(b-a)/n;
sumai=0;
sumap=0;
for i=1:2:n-1
sumai=sumai+feval(f,h*i+a);
end
for i=2:2:n-2
sumap=sumap+feval(f,h*i+a);
end
int=(h/3)*(feval(f,a)+4*sumai+2*sumap+feval(f,b));
%****************************************************************
%** Calculo de la integral por el
**
**
UdeG **
%**
Maestria en Electronica **
**
**
26/Nov/99 **
%****************************************************************
clear;
clc;
fprintf('Calculo de la integral por el metodo de Simpson de 1/3 y 3/8\n\n');
fprintf('Para cualquier cantidad de segmentos par o impar\n\n');
f=input('introduce la funcion:','s');
a=input('lime inferior:');
b=input('limite superior:');
c=input('numero de segmentos a dividir :');
h=(b-a)/c;
%*****************************************************************
%** En la siguiente seccion se aplica la regla de simpson 3/8
**
**
qqqqq=0;
if (-1)^c==-1
b=b-(3*h);
c=c-3;
e=b+h;
ff=b+2*h;
g=b+3*h;
ee=g-b;
x=b;
q=eval(f);
x=e;
qq=eval(f);
x=ff;
qqq=eval(f);
x=g;
qqqq=eval(f);
qqqqq=(ee)*((q+3*qq+3*qqq+qqqq)/8);
end
%*****************************************************************
%** En la siguiente seccion se realiza la sumatoria
**
**
%*****************************************************************
z=0;
x=a;
for i=1:c;
if (-1)^i==1
k=eval(f);
z=z+k;
end
x=h*i;
end
%*****************************************************************
%** En la siguiente seccion se realiza la sumatoria
**
**
%*****************************************************************
zz=0;
x=a;
for i=2:c;
if (-1)^i==-1
k=eval(f);
zz=zz+k;
end
x=h*i;
end
%*****************************************************************
%** En la siguiente seccion se realiza la evaluacion de
**
**
%*****************************************************************
x=a;
if x==a
end
d=eval(f);
x=b;
if x==b
eee=eval(f);
end
%******************************************************************
%** una vez que se tienen los datos de areas bajo la curva
**
**
**
z=z*4;
v=zz*2;
z=z+v+d+eee;
z=z/(3*c);
z=z*(b-a);
z=z+qqqqq
fprintf('Resultado ');
Mtodo de Runge-Kutta
Solucion
2. Respuesta
>> runge2
RESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE ORDEN 4
Ingrese la ecuacion diferencial dy/dx=
4*exp(0.8*x)-0.5*y
Ingrese el primer punto x0:
0
Ingrese el segundo punto x1:
4
Ingrese la condicion inicial y(x0):
2
Ingrese el numero de pasos n:
4
'it x0 y(x1)
0 0.000000 6.701082
1 1.000000 16.319782
2 2.000000 37.199249
3 3.000000 83.337767
El punto aproximado y(x1) es = 83.337767
respuesta
>> runge4
RESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE ORDEN 4
Ingrese la ecuacion diferencial
-2*x^3+12*x^2-20*x+8.5
Ingrese el primer punto x0:
0
Ingrese el segundo punto x1:
0.5
Ingrese la condicion inicial y(x0):
1
Ingrese el nmero de pasos n:
5
'it x0 y(x1)
0 0.000000 1.753950
1 0.100000 2.331200
2 0.200000 2.753950
3 0.300000 3.043200
4 0.400000 3.218750
Z = zeros(m+1,length(Za));
T(1) = a;
Z(1,:) = Za;
for j=1:m,
tj = T(j);
Zj = Z(j,:);
K1 = h*feval(Fn,tj,Zj);
K2 = h*feval(Fn,tj+h/2,Zj+K1/2);
K3 = h*feval(Fn,tj+h/2,Zj+K2/2);
K4 = h*feval(Fn,tj+h,Zj+K3);
Z(j+1,:) = Zj + (K1 + 2*K2 + 2*K3 + K4)/6;
T(j+1) = a + h*j;
end
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
function [y t] = rk4(f,a,b,ya,n)
h = (b - a) / n;
halfh = h / 2;
y(1,:) = ya;
t(1) = a;
h6 = h/6;
for i = 1 : n
t(i+1) = t(i) + h;
th2 = t(i) + halfh;
s1 = f(t(i), y(i,:));
s2 = f(th2, y(i,:) + halfh * s1);
s3 = f(th2, y(i,:) + halfh * s2);
s4 = f(t(i+1), y(i,:) + h * s3);
y(i+1,:) = y(i,:) + (s1 + s2+s2 + s3+s3 + s4) * h6;
end;