Escolar Documentos
Profissional Documentos
Cultura Documentos
global fun
fprintf('Metodo de la secante:\n');
fun=input('Ingrese la funcion:\n','s');
x0=input('Ingrese el primer punto inicial:\n');
x1=input('Ingrese el segundo punto inicial:\n');
tol=input('Ingrese la tol:\n');
it=0;
fprintf('it x0 x1 x2 x1-x2');
while(it<50) it="it+1;" x="x0;" f0="eval(fun);" x="x1;" f1="eval(fun);" x2="(x0*f1-x1*f0)/(f1-f0);">
fprintf('el procedimiento se completo satisfactoriamente:\n');
break
end
x0=x1;
x1=x2;
end
fprintf('la raiz buscada es=%15.9f\n',x2);
ezplot(fun),
grid on
Ejemplo:
Metodo de la secante:
Ingrese la funcion:
x^2+10*cos(x)
1.5
2
Ingrese la tol:
10^(-5)
NEWTON
function newton
global fun dfun
fprintf('metodo de newton:\n');
fun=input('ingrese la funcion:\n','s');
x0=input('ingrese el punto inicial:\n');
tol=input('ingrese la tol:\n');
dfun=diff(fun);
it=0;
fprintf(' it x0 x1 x0-x1');
while(it<50) it="it+1;" x="x0;" x1="x0-(eval(fun)/eval(dfun));">fprintf('el procedimiento se completo
satisfactoriamente:\n');
break
end
x0=x1;
end
fprintf('la raiz buscada es=%15.9f\n',x1);
ezplot(fun),grid on
Ejemplo:
metodo de newton:
ingrese la funcion:
exp(-x)-log(x)
ingrese el punto inicial:
1
ingrese la tol:
0.01
MINIMOS CUADRADOS
Function correl
xi=input('ingrese los valores de xi:\n');
yi=input('ingreso los valores de yi:\n');
xm=input('ingreso un valor de x para analisar la pendiente:\n');
n=length(xi);
p=xi.';
q=yi.';
r=p.*q;
s=p.*p;
a1=sum(xi);
a2=sum(yi);
a3=sum(r);
a4=sum(s);
m=a2/n;
j=sum((yi-m).^2);
A=[a4 a1;a1 n];
B=[a3 a2];
X=inv(A)*(B');
l=X(1);r=X(2);
fprintf('\nLos coeficientes de la ecuacion son:\n')
fprintf(' a b\n')
fprintf('%8.8f%10.8f\n',l,r)
fun=input('\nLa funcion lineal es y=','s');
y=l*xi+r;
t=sum((yi-y).^2);
r2=1-(t/j);
z=(r2)^0.5;
m=diff(fun);
x=xm;
n=eval(m);
fprintf('\n El coeficiente de determinacion r^2=');
fprintf('%5.5f\n',r2);
if(n>0)
fprintf('\n El coeficiente de correlacion r=');
fprintf('%5.5f\n',z);
elsefprintf('\n El coeficiente de correlacion r=');
fprintf('%5.5f\n',-z);
end
hold on
ezplot(fun),grid on,
plot(xi,yi,'m.'),
hold off
function correlog2
xi=input('ingrese los valores de xi:\n');
yi=input('ingreso los valores de yi:\n');
n=length(xi);
p=log(-xi);
q=yi;
r=p.*q;
s=p.*p;
a1=sum(p);
a2=sum(q);
a3=sum(r);
a4=sum(s);
m=sum(yi)/n;
j=sum((yi-m).^2);
A=[a4 a1;a1 n];
B=[a3 a2];
X=inv(A)*(B');
l=X(1);r=X(2);
fprintf('\nLos coeficientes de la funcion son:\n')
fprintf(' a b\n')
fprintf('%8.12f%10.12f\n',l,r)
y=input('\nLa funcion logaritmica natural es y=','s');
h=l*log(-xi)+r;
z=sum((yi-h).^2);
r2=1-(z/j);
k=(r2)^0.5;
fprintf('\n El coeficiente de determinacion r^2=');
fprintf('%5.5f\n',r2);
fprintf('\n El coeficiente de correlacion r=');
fprintf('%5.5f\n',k);
hold on
ezplot(y),grid on,
plot(xi,yi,'m.'),
hold off
BISECCION
fprintf('\t\tmetodo de biseccion\n');
fun=input('ingrese la funcion:','s');
a=input('ingrese el punto inicial:');
b=input('ingrese el punto final:');
tol=input('ingrese la tolerancia:');
it=0;
x=a;
f_a=eval(fun);
x=b;
f_b=eval(fun);
if(f_a*f_b>0)
fprintf('\t\tEn este intervalo no hay raices\n');
fprintf('\t\tBuscar otro intervalo\n');
return
else
n=ceil(log((b-a)/tol)/log(2));
fprintf('\n=%10.6f\n',n);
fprintf(' it a b c f(a) f(b) f(c) abs(b-a)/2\n');
while 1
it=it+1;
c=(a+b)/2;
x=c;
f_c=eval(fun);
fprintf('%5.0f %10.9f %10.9f %10.9f %10.9f %10.9f %10.9f %10.9f\n',it,a,b,c,f_a,f_b,f_c,abs(b-a)/2);
if(abs(b-a)/2<=tol)
fprintf('n se satisface la tolerancia\n');
break end if(it>n)
fprintf('\numero de iteraciones excedido');
break
end
if(f_c*f_b<=0) a=c;
f_a=f_c;
else b=c;f_b=f_c;
end
end
fprintf('La raiz pedida es= %10.6f\n',c)
fprintf('La tolerancia es= %10.6f\n',abs(b-a)/2);
x=-100:0.1:100;
ezplot(fun),
grid on end
Ejemplo:
metodo de biseccion
ingrese la funcion:
3*x-2+exp(x)-x^2
ingrese el punto inicial:
0
ingrese el punto final:
1
ingrese la tolerancia:
10^(-5)
= 17.000000
n se satisface la tolerancia
La raiz pedida es= 0.257530
La tolerancia es= 0.000008
EULER
La idea del método de Euler es muy sencilla y está basada en el significado geométrico de la
derivada de una función en un punto dado.
Supongamos que tuviéramos la curva solución de la ecuación diferencial y trazamos la recta
tangente a la curva en el punto dado por la condición inicial.
Para obtener únicamente hay que pensar que ahora el papel de lo toma el punto
y por lo tanto, si sustituímos los datos adecuadamente, obtendremos que:
hasta
en pasos de longitud h.
Programa en matlab del método de Euler
function f
fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO METODO DE
EULER\n')
f=input('\nIngrese la ecuacion diferencial de la forma: dy/dx=f(x,y)\n','s');
x0=input('\nIngrese el primer punto x0:\n');
x1=input('\nIngrese el segundo punto x1:\n');
y0=input('\nIngrese la condicion inicial y(x0):\n');
n=input('\nIngrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
y1=y0;
fprintf('\n''it x0 x1 y1');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
x1=xs(i+1);
y=y0;
y1=y0+h*eval(f);
fprintf('\n%2.0f%10.6f%10.6f%10.6f\n',it,x0,x1,y1);
y0=y1;
end
fprintf('\n El punto aproximado y(x1) es = %10.6f\n',y1);
Solución
Respuesta
>> euler
RESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO METODO DE EULER
'it x0 x1 y1
0 2.000000 2.100000 0.706155
Método de Runge-Kutta
Programa en MatLab de Runge-Kutta de orden dos.
function f
fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE
ORDEN 4\n')
f=input('\n Ingrese la ecuacion diferencial dy/dx=\n','s');
x0=input('\n Ingrese el primer punto x0:\n');
x1=input('\n Ingrese el segundo punto x1:\n');
y0=input('\n Ingrese la condicion inicial y(x0):\n');
n=input('\n Ingrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
fprintf('\n''it x0 y(x1)');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
y=y0;
k1=h*eval(f);
x=xs(i+1);
y=y0+k1;
k2=h*eval(f);
y0=y0+(k1+k2)/2;
fprintf('\n%2.0f%10.6f%10.6f\n',it,x0,y0);
end
fprintf('\n El punto aproximado y(x1) es = %8.6f\n',y0);
Solucion
2. Respuesta
>> runge2
'it x0 y(x1)
0 0.000000 6.701082
1 1.000000 16.319782
2 2.000000 37.199249
3 3.000000 83.337767
respuesta
>> runge4
'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