% integración de romber
clear all;
syms x y z q valorsitos;
disp('Metodo
de Romberg')
f=input('Ingrese
la funcion: ');
a=input('Ingrese
el valor de a: ');
b=input('Ingrese
el valor de b: ');
n=input('Ingrese
el valor de N: ');
cont1=1;
%encontrando
los valores de h
while(cont1<=n)
h(cont1)=(b-a)/(2^(cont1-1));
fprintf('h%d=%.9f\n\n',cont1,h(cont1));
cont1=cont1+1;
end
%generando
la primera columna
t(1,1)=(h(1)/2)*(subs(f,a)+subs(f,b));
fprintf('R(1,1)=h1/2*(f(a)+f(b))==>');
disp(t(1,1));
cont1=2;
sumatoria=0;
while(cont1<=n)
cont2=1;
fprintf('R(%1.0f,%1.0f)=',cont1,1);
valorsitos(cont1,cont2)=q+h(cont1-1);
while(cont2<=(2^(cont1-2)))
valorsitos(cont1,cont2+1)=(q+a+(2*cont2-1)*h(cont1));
sumatoria=sumatoria+subs(f,(a+(2*cont2-1)*h(cont1)));
cont2=cont2+1;
end
t(cont1,1)=(1/2)*(t(cont1-1,1)+h(cont1-1)*sumatoria);
fprintf(')==>');
disp(t(cont1,1));
sumatoria=0;
cont1=cont1+1;
end
%generando
el resto de la tabla
cont1=2;
cont2=2;
cont3=2;
while(cont2<=n)
while(cont1<=n)
t(cont1,cont2)=t(cont1,cont2-1)+(t(cont1,cont2-1)-t(cont1-1,cont2-1))/(4^(cont2-1)-1);
fprintf('R(%1.0f,%1.0f)=R(%1.0f,%1.0f)+(R(%1.0f,%1.0f)-R(%1.0f,%1.0f)/%d)==>',cont1,cont2,cont1,cont2-1,cont1,cont2-1,cont1-1,cont2-1,(4^(cont2-1)-1));
disp(t(cont1,cont2));
cont1=cont1+1;
end
cont3=cont3+1;
cont1=cont3;
cont2=cont2+1;
end
%clear all integracion
de simpsom
syms x y;
funcion=input('funcion=');
a=input('a=');
b=input('b=');
n=input('n=');
h=(b-a)/n;
for i=1:n-1
x(i)=a+i*h;
fprintf('x(%d)= ',i)
pretty(x(i));
%disp(x(i));
end
part2=0;
for i=1:(n/2-1)
part2=part2+subs(funcion,x(i*2));
end
part3=0;
for i=1:(n/2)
part3=part3+subs(funcion,x(i*2-1));
end
resultado=(h/3)*(subs(funcion,a)+2*part2+4*part3+subs(funcion,b));
double(resultado)
% extrapolación de richarson programa 1
function N = richardson2B(f, x0, n,
h, pre)
%meter lo
de arriba asi como va copy paste! y la funcion f es primero
%hayq declararla!
% f es la
funcion, x0 el de aproximar, n el subindice de N, h es h y pre es
% precisi�n (opcional)
if nargin < 5
pre = 8;
end
pre = num2str(pre);
N = zeros(n,n);
%
generamos la primera columna
for a =
1:n
h_ = h/(2^(a-1));
expr = sprintf('[1/(2*(%g))]*[f(%g) - f(%g)]', h_, x0 + h_, x0 - h_);
N(a, 1) =
centrada3_fun(f, x0, h_, str2num(pre));
end
% las
demas columnas
for a = 2:n
for b = a:n
N(b, a) = N(b, a-1) +
(N(b, a-1) - N(b-1, a-1))/(4^(a-1) - 1);
end
end
% extrpolacion de Richardson programa 2
function RICHARDSON(h,N,Xo,g)
%h es el
espaciado, N es hasta q numero se va a llegar N3,N4, etc. Xo es el
%punto en
donde se va a evaluar la derivada y g es la funcion.
R=zeros(N,N);
for j=1:N
X1=Xo+h;
X2=Xo-h;
syms x
g1=subs(g,X1);
g2=subs(g,X2);
%Primero
usamos la formula de 3 puntos
%f'(h)=(1/2h)(f(x0+h)-f(x0-h))
%f'(h/2)=(1/2(h/2))(f(x0+(h/2))-f(x0-(h/2)))
R(j,1)=(1/(2*h))*(g1-g2);
fprintf('h=%1.4f\n',h)
h=h/2;
end
for j=2:N
for i=j:N
R(i,j)=R(i,j-1) +
(R(i,j-1)-R(i-1,j-1))/((4^(j-1))-1);
end
end
for j=1:N
for i=j:N
fprintf('N(%d,%d)= %3.10f\n',i,j,R(i,j))
end
end
valor_aproximado=R(N,N)
gp=diff(g);
valor_exacto=subs(gp,Xo)
error=abs(valor_aproximado
- valor_exacto)
%Formula
de Richardson
% N1(h/2)-N1(h)
%N2(h)=N1(h/2)+
-------------
% 4^(j-1))-1
%
% N1(h/4)-N1(h/2)
%N2(h/2)=N1(h/4)+
-------------
% 4^(j-1))-1
%
%....
+prueba
ResponderEliminar*************************************************************************************************
ResponderEliminar********************* EULER MODIFICADO ***********************************************
************************************************************************************************
function EulerMod()
clear all
disp('Método de Euler modificado para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
for i=2:n+1
wi(i)=wi(i-1)+(h/2*subs(f,{t,y},{ti(i-1),wi(i-1)}))+h/2*subs(f,{t,y},{ti(i),wi(i-1)+h*subs(f,{t,y},{ti(i-1),wi(i-1)})});
end
fprintf('ti\t\t\t\t|wi\t\t\t\t\t|y(ti)\n');
for i=1:n+1
fprintf('%.15f\t\t|%.15f\t\t|%.15f\n',ti(i),wi(i),subs(ed,ti(i)));
end
fprintf('\n');
****************************************************************************************************
ResponderEliminar************************************** EULER***************************************************
**************************************************************************************************
function Euler()
clear all
disp('Método de Euler para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
for i=2:n+1
wi(i)=wi(i-1)+(h*subs(f,{t,y},{ti(i-1),wi(i-1)}));
end
fprintf('ti\t\t\t\t|wi\t\t\t\t\t|y(ti)\n');
for i=1:n+1
fprintf('%.9f\t\t|%.9f\t\t|%.9f\n',ti(i),wi(i),subs(ed,ti(i)));
end
fprintf('\n');
****************************************************************************************************
ResponderEliminar********************************** HEUN*********************************************************
***************************************************************************************************
function heun()
clear all
disp('Método de Heunn para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
for i=2:n+1
wi(i)=wi(i-1)+(h*subs(f,{t,y},{ti(i-1),wi(i-1)}))/4+3*h/4*subs(f,{t,y},{ti(i-1)+2/3*h,wi(i-1)+2/3*h*subs(f,{t,y},{ti(i-1),wi(i-1)})});
end
fprintf('ti\t\t\t\t|wi\t\t\t\t\t|y(ti)\n');
for i=1:n+1
fprintf('%.15f\t\t|%.15f\t\t|%.15f\n',ti(i),wi(i),subs(ed,ti(i)));
end
fprintf('\n');
************************************************************************************************
ResponderEliminar************************************** HEUN_ECU******************************************
***********************************************************************************************
%MÉTODO DE HEUN
% - Introduzca la ecuación diferencial : 'Dy=y-(x^2)+1'
% - Introduzca la condición y(a)=b : 'y(0)=0.5'
% - Introduzca la función de trabajo : y-(x^2)+1
% - Introduzca la condición inicial : 0.5
% - Introduzca el valor de a : 0
% - Introduzca el valor de b : 1
% - Introduzca el tamaño de paso h : 0.1
fprintf('\n');
clear all
clc
fprintf(' --------------\n')
fprintf(' MÉTODO DE HEUN (x)\n')
fprintf(' --------------\n')
fprintf('\n');
syms x y
d=input(' - Introduzca la ecuación diferencial : ');
n=input(' - Introduzca la condición y(a)=b : ');
f1=input(' - Introduzca la función de trabajo : ');
ya=input(' - Introduzca la condición inicial : ');
a=input(' - Introduzca el valor de a : ');
b=input(' - Introduzca el valor de b : ');
h=input(' - Introduzca el tamaño de paso h : ');
fprintf('\n\n');
fprintf(' - La solución de la ecuación diferencial es : \n\n\n');
m = dsolve(d,n,'x');
pretty(m);
fprintf('\n\n\n');
%Condiciones para el funcionamiento de los lazos FOR
f=f1;
w(1)=ya;
i=0;
t(1)=a;
v(1)=a;
d=0;
c=0;
g=0;
%Este for obtiene y guarda todos los valores de t
%También se utiliza para evaluar la ecuación diferencial
for p=a:h:b
d=1+d;
t(d)=p;
v(d)=subs(m,p);
end
%Este for se usa para contabilizar las iteraciones
for s=c:1:(d-1)
g=1+g;
k(g)=(g-1);
end
k3=k(end);
%Este for obtiene los valores aproximados de solución
fprintf('-------------------------------------------------------------------------------------------------------');
fprintf('\n');
fprintf(' FÓRMULAS DE CADA ITERACIÓN');
fprintf('\n');
fprintf('-------------------------------------------------------------------------------------------------------');
fprintf('\n\n');
fprintf('- w0 = %1.5f ',ya);
fprintf('\n');
for j=a:h:(b-h)
i=1+i;
w(i+1)=w(i)+((h/4)*(subs(f,{x,y},{t(i),w(i)})))+(((3/4)*h)*(subs(f,{x,y},{(t(i)+((2/3)*h)),(w(i)+(((2/3)*h)*(subs(f,{x,y},{t(i),w(i)}))))})));
fprintf('\n');
fprintf('- w%1.0f = w%1.0f + h/4 f(t%1.0f,w%1.0f) + 3/4 h f(t%1.0f + 2/3 h,w%1.0f + 2/3 h f(t%1.0f,w%1.0f))',i,i-1,i-1,i-1,i-1,i-1,i-1,i-1);
fprintf('\n');
fprintf('- w%1.0f = w%1.0f + %1.5f f(%1.9f,w%1.0f) + %1.5f f(%1.9f + %1.5f,w%1.0f + %1.5f f(%1.9f,w%1.0f))',i,i-1,h/4,t(i),i-1,(3/4)*h,t(i),(2/3)*h,i-1,(2/3)*h,t(i),i-1);
fprintf('\n');
end
fprintf('\n');
fprintf('-------------------------------------------------------------------------------------------------------');
fprintf('\n');
%Presentación de los datos
fprintf('\n\n');
fprintf(' i ti wi y(t)');
fprintf('\n\n');
for k1=0:k3
k2=k1+1;
fprintf('\n');
fprintf(' %1.0f %.15f %.15f %.15f',k(k2),t(k2),w(k2),v(k2));
fprintf('\n');
end
fprintf('\n');
****************************************************************************************************
ResponderEliminar****************************** PUNTO MEDIO ************************************************
**************************************************************************************************
function puntomedio()
clear all
disp('Método de punto medio para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
for i=2:n+1
wi(i)=wi(i-1)+(h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)+h/2*subs(f,{t,y},{ti(i-1),wi(i-1)})}));
end
fprintf('ti\t\t\t\t|wi\t\t\t\t\t|y(ti)\n');
for i=1:n+1
fprintf('%.15f\t\t|%.15f\t\t|%.15f\n',ti(i),wi(i),subs(ed,ti(i)));
end
fprintf('\n');
******************************************************************************************************
ResponderEliminar************************************ RK4**********************************************************
***************************************************************************************************
function rk4()
clear all
disp('Método de Runge-Kutta de orden 4 para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
fprintf('t0=%.15f\nw0=%.15f\nY(%.15f)=%.15f\n',ti(1),wi(1),ti(1),subs(ed,ti(1)));
for i=2:n+1
k1=h*subs(f,{t,y},{ti(i-1),wi(i-1)});
k2=h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)+k1/2});
k3=h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)+k2/2});
k4=h*subs(f,{t,y},{ti(i),wi(i-1)+k3});
wi(i)=wi(i-1)+1/6*(k1+2*k2+2*k3+k4);
fprintf('\nt%d=%.15f\nk1=%.15f\nk2=%.15f\nk3=%.15f\nk4=%.15f',i-1,ti(i),k1,k2,k3,k4);
fprintf('\nw%1.0f= w%1.0f + 1/6 [ K1+ 2K2 + 2K3 + K4 ] = %9.8e - Valor Aprox\n',i-1,i-2,wi(i));
fprintf('Y(%.15f)=%.15f - Valor Exacto\n\n',ti(i),subs(ed,ti(i)));
end
fprintf('\n');
*************************************************************************************************
ResponderEliminar******************************** RK4_sin Exacta**********************************************
**************************************************************************************************
function rk4_SinExacta()
clear all
disp('Método de Runge-Kutta de orden 4 para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
fprintf('t0=%.9f\nw0=%.9f\n',ti(1),wi(1));
for i=2:n+1
k1=h*subs(f,{t,y},{ti(i-1),wi(i-1)});
k2=h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)+k1/2});
k3=h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)+k2/2});
k4=h*subs(f,{t,y},{ti(i),wi(i-1)+k3});
wi(i)=wi(i-1)+1/6*(k1+2*k2+2*k3+k4);
fprintf('\nt%d=%.9f\nk1=%.9f\nk2=%.9f\nk3=%.9f\nk4=%.9f',i-1,ti(i),k1,k2,k3,k4);
fprintf('\nw%1.0f= w%1.0f + 1/6 [ K1+ 2K2 + 2K3 + K4 ] = %9.8e - Valor Aprox\n',i-1,i-2,wi(i));
end
fprintf('\n');
****************************************************************************************************
ResponderEliminar***************************************** RKF4 **************************************************
****************************************************************************************************
function rkf4()
clear all
disp('Método de Runge-Kutta-Fehlberg de orden 4 para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo [a,b]: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
fprintf('t0=%.9f\nw0=%.9f\nY(%.9f)=%.9f\n',ti(1),wi(1),ti(1),subs(ed,ti(1)));
for i=2:n+1
k1=h*subs(f,{t,y},{ti(i-1),wi(i-1)});
k2=h*subs(f,{t,y},{ti(i-1)+h/4,wi(i-1)+k1/4});
k3=h*subs(f,{t,y},{ti(i-1)+3*h/8,wi(i-1)+3/32*k1+9*k2/32});
k4=h*subs(f,{t,y},{ti(i-1)+12/13*h,wi(i-1)+1932/2197*k1-7200/2197*k2+7296/2197*k3});
k5=h*subs(f,{t,y},{ti(i-1)+h,wi(i-1)+439/216*k1-8*k2+3680/513*k3-845/4104*k4});
wi(i)=wi(i-1)+25/216*k1+1408/2565*k3+2197/4104*k4-k5/5;
fprintf('\ni=%d\nt%d=%.9f\nk1=%.9f\nk2=%.9f\nk3=%.9f\nk4=%.9f\nk5=%.9f',i-2,i-1,ti(i),k1,k2,k3,k4,k5);
fprintf('\nw%d=wi(%d)+25/216*k1+1408/2565*k3+2197/4104*k4-k5/5=%.9f - Valor Aprox\n',i-1,i-2,wi(i));
fprintf('Y(%.9f)=%.9f - Valor Exacto\n\n',ti(i),subs(ed,ti(i)));
end
fprintf('\n');
*****************************************************************************************************
ResponderEliminar**************************************** RKF5****************************************************
****************************************************************************************************
function rkf5()
clear all
disp('Método de Runge-Kutta-Fehlberg de orden 5 para ecuaciones diferenciales');
syms y t
f=input('Ingrese y´: ');
%Para obtener la solucion de la ecuacion diferencial:
%funcion='Dy=(log(t)-y)/(t+1)';
%valorInicial='y(1)=10';
%exacta=dsolve(funcion,valorInicial)
%******************************************************************
% y': solo ecuacion sin la igualacion ej: sec(t)-y*sec(t)
% solución de la ecuación diferencial: aplicar dsolve asi como me dan la
% ED y copiarla lo que me resulte
% Valor Inicial: ej: y(1)=4 ----------------> es 4
ed=input('Ingrese la solución de la ecuación diferencial: ');
interv=input('Ingrese el intervalo: ');
vin=input('Ingrese el valor inicial: ');
h=input('Ingrese el tamaño de paso: ');
n=(interv(2)-interv(1))/h;
n=round(n);
ti=zeros(1,n+1);
wi=zeros(1,n+1);
wi(1)=vin;
ti(1)=interv(1);
ti(n+1)=interv(2);
for i=2:n
ti(i)=interv(1)+(i-1)*h;
end
fprintf('t0=%.15f\nw0=%.15f\nY(%.15f)=%.15f\n',ti(1),wi(1),ti(1),subs(ed,ti(1)));
for i=2:n+1
k1=h*subs(f,{t,y},{ti(i-1),wi(i-1)});
k2=h*subs(f,{t,y},{ti(i-1)+h/4,wi(i-1)+k1/4});
k3=h*subs(f,{t,y},{ti(i-1)+3*h/8,wi(i-1)+3/32*k1+9*k2/32});
k4=h*subs(f,{t,y},{ti(i-1)+12/13*h,wi(i-1)+1932/2197*k1-7200/2197*k2+7296/2197*k3});
k5=h*subs(f,{t,y},{ti(i-1)+h,wi(i-1)+439/216*k1-8*k2+3680/513*k3-845/4104*k4});
k6=h*subs(f,{t,y},{ti(i-1)+h/2,wi(i-1)-8/27*k1+2*k2-3544/2565*k3+1859/4104*k4-11/40*k5});
wi(i)=wi(i-1)+16/135*k1+6656/12825*k3+28561/56430*k4-9*k5/50+22/55*k6;
fprintf('\ni=%d\nt%d=%.15f\nk1=%.15f\nk2=%.15f\nk3=%.15f\nk4=%.15f\nk5=%.15f\nk6=%.15f',i-2,i-1,ti(i),k1,k2,k3,k4,k5,k6);
fprintf('\nw%d=wi(%d)+16/135*k1+6656/12825*k3+28561/56430*k4-9*k5/50+22/55*k6=%.15f - Valor Aprox\n',i-1,i-2,wi(i));
fprintf('Y(%.15f)=%.15f - Valor Exacto\n\n',ti(i),subs(ed,ti(i)));
end
fprintf('\n');
prueba
ResponderEliminarBISECCION
ResponderEliminarfprintf('* Bisección *\n')
syms x % cambiar segun sea la variable de la funcion
a = input('Ingrese a: ');
b = input('Ingrese b: ');
f = input('Ingrese la función de trabajo: ');
e = input('Ingrese la presición: ');
fa = subs(f,a); % evalua a en la funcíon
fb = subs(f,b); % evalua b en la función
if fa*fb<0
n=1;
p = (a+b)/2;
error = abs(subs(f,p));
fprintf('\nn=%2.0f a=%.15f b=%.15f p=%.15f error=%.2e\n',n,a,b,p,error)
while e<error
n=n+1;
fa = subs(f,a);
fb = subs(f,b);
fp = subs(f,p);
if fa*fp<0
a = a;
b = p;
p = (a+b)/2;
error = abs(p - b);
else
a = p;
b = b;
p = (a+b)/2;
error = abs(p - a);
end
fprintf('n=%2.0f a=%.15f b=%.15f p=%.15f error=%.2e\n',n,a,b,p,error)
end
fprintf('\nLa raíz aproximada es: %.15f\n\n',p)
else
fprintf('\nEn el intervalo [a,b] no existe una raiz\n\n')
end
VALORES BISECCION
fprintf('* Bisección *\n')
syms h % cambiar segun sea la variable de la funcion
ho = input('Ingrese ho: ');
h1 = input('Ingrese h1: ');
V = input('Ingrese V: ');
R=input('Ingrese R: ');
L=input('Ingrese L: ');
f = input('Ingrese la función de trabajo: ');
e = input('Ingrese la presición: ');
fho = subs(f,ho); % evalua a en la funcíon
fh1 = subs(f,h1); % evalua b en la función
if fho*fh1<0
n=1;
h = (ho+h1)/2;
error = abs(subs(f,h));
fprintf('\nn=%2.0f ho=%.15f h1=%.15f h=%.15f error=%.2e\n',n,ho,h1,h,error)
while e<error
n=n+1;
fho = subs(f,ho);
fh1 = subs(f,h1);
fh = subs(f,h);
if fho*fh1<0
ho = ho;
h1 = h;
h = (ho+h1)/2;
error = abs(h - h1);
else
ho = h;
h1 = h1;
h = (ho+h1)/2;
error = abs(h - ho);
end
fprintf('n=%2.0f ho=%.15f h1=%.15f h=%.15f error=%.2e\n',n,ho,h1,h,error)
end
fprintf('\nLa raíz aproximada es: %.15f\n\n',h)
else
fprintf('\nEn el intervalo [ho,h1] no existe una raiz\n\n')
end
PUNTO FIJO
ResponderEliminarfprintf('* Punto fijo *\n')
syms x % cambiar segun la variable de la ecuación.
p0 = input('Ingrese un valor inicial P0: ');
g = input('Ingrese la función despejada : ');
e = input('Ingrese la precisión: ');
n = 1;
p = subs(g,p0);
error = abs(p - p0);
fprintf('\nn=%2.0f p0=%.15f p=%.15f error=%.2e\n',n,p0,p,error)
while e<error
n=n+1;
p0 = p;
p = subs(g,p0);
error = abs(p - p0);
fprintf('n=%2.0f p0=%.15f p=%.15f error=%.2e\n',n,p0,p,error)
end
fprintf('\nLa raiz aproximada es: %.15f\n\n',p)
VALORES PUNTO FIJO
fprintf('* Punto fijo *\n')
syms h % cambiar segun la variable de la ecuación.
ho = input('Ingrese un valor inicial ho: ');
g = input('Ingrese la función despejada : ');
e = input('Ingrese la precisión: ');
n = 1;
h = subs(g,p0);
error = abs(p - ho);
fprintf('\nn=%2.0f ho=%.15f h=%.15f error=%.2e\n',n,ho,h,error)
while e<error
n=n+1;
ho = h;
h = subs(g,ho);
error = abs(h - ho);
fprintf('n=%2.0f ho=%.15f h=%.15f error=%.2e\n',n,ho,h,error)
end
fprintf('\nLa raiz aproximada es: %.15f\n\n',h)
NEWTON
ResponderEliminarfprintf('* Newton Raphson *\n')
syms x
p0 = input('Ingrese P0(sacado de solve): ');
fx = input('Ingrese la funcion de ejer(el prog. hace la diff): ');
e = input('Ingrese la precisión: ');
dfx = diff(fx);
fp0 = subs(fx,p0);
dfp0 = subs(dfx,p0);
n = 1;
p = p0 - (fp0/dfp0);
error = abs(p - p0);
fprintf('\nn=%2.0f P0=%.15f P=%0.15f Error=%.2e\n',n,p0,p,error)
while error>e
n = n+1;
p0=p;
fp0 = subs(fx,p0);
dfp0 = subs(dfx,p0);
p = p0 - (fp0/dfp0);
error = abs(p - p0);
fprintf('n=%2.0f P0=%.15f P=%.15f Error=%.2e\n',n,p0,p,error)
end
fprintf('\nLa raiz de aproximación es: %0.15f\n\n',p)
VALORES NEWTON
fprintf('* Newton Raphson *\n')
syms h
V=input('Ingrese V: ');
R=input('Ingrese R: ');
L=input('Ingrese L: ');
fx = input('Ingrese la funcion de ejer(el prog. hace la diff): ');
ho = input('Ingrese ho(sacado de solve): ');
e = input('Ingrese la precisión: ');
dfx = diff(fx);
fho = subs(fx,ho);
dfho = subs(dfx,ho);
n = 1;
h = ho - (fho/dfho);
error = abs(h - ho);
fprintf('\nn=%2.0f ho=%.15f h=%0.15f Error=%.2e\n',n,ho,h,error)
while error>e
n = n+1;
ho=h;
fho = subs(fx,ho);
dfho = subs(dfx,ho);
h = ho - (fho/dfho);
error = abs(h - ho);
fprintf('n=%2.0f ho=%.15f h=%.15f Error=%.2e\n',n,ho,h,error)
end
fprintf('\nLa raiz de aproximación es: %0.15f\n\n',h)
SECANTE
ResponderEliminarfprintf('* Secante *\n')
syms x
p0 = input('Introduzca el valor P0: ');
p1 = input('Introduzca el valor P1: ');
fx = input('Introduzca la función del enunciado solo igualada a 0): ');
precision = input('Introduzca la precisión: ');
fp0 = subs(fx,p0);
fp1 = subs(fx,p1);
p2 = p1 - ((fp1*(p1-p0))/(fp1 - fp0));
error = abs(p2 - p1);
n = 1;
fprintf('\nn=%2.0f p0=%2.9f p1=%2.9f p2=%2.9f error=%1.2e\n',n,p0,p1,p2,error)
while error>precision
n=n+1;
p0=p1;
p1=p2;
fp0 = subs(fx,p0);
fp1 = subs(fx,p1);
p2 = p1 - ((fp1*(p1-p0))/(fp1 - fp0));
error = abs(p2 - p1);
fprintf('n=%2.0f p0=%2.9f p1=%2.9f p2=%2.9f error=%1.2e\n',n,p0,p1,p2,error)
end
fprintf('\n-->El valor de aproximación P2 es: %2.9f\n\n',p2)
VALORES SECANTE
fprintf('* Secante *\n')
syms h
ho = input('Introduzca el valor ho: ');
h1 = input('Introduzca el valor h1: ');
V=input('Introduzca el valor de V: ');
R=input('Introduzca el valor de R: ');
L=input('Introduzca el valor de L: ');
fx = input('Introduzca la función del enunciado solo igualada a 0): ');
precision = input('Introduzca la precisión: ');
fho = subs(fx,ho);
fh1 = subs(fx,h1);
h2 = h1 - ((fh1*(h1-ho))/(fh1 - fho));
error = abs(h2 - h1);
n = 1;
fprintf('\nn=%2.0f ho=%2.9f h1=%2.9f h2=%2.9f error=%1.2e\n',n,ho,h1,h2,error)
while error>precision
n=n+1;
ho=h1;
h1=h2;
fho = subs(fx,ho);
fh1 = subs(fx,h1);
h2 = h1 - ((fh1*(h1-ho))/(fh1 - fho));
error = abs(h2 - h1);
fprintf('n=%2.0f ho=%2.9f h1=%2.9f h2=%2.9f error=%1.2e\n',n,ho,h1,h2,error)
end
fprintf('\n-->El valor de aproximación es: %2.9f\n\n',h2)
POSICION FALSA
ResponderEliminarfprintf('Método de Posición Falsa.\n')
syms x
p0 = input('P0: ');
p1 = input('P1: ');
fx = input('Fx(funcion igualada a 0): ');
precision = input('Precision: ');
fp0 = subs(fx,p0);
fp1 = subs(fx,p1);
if fp0*fp1 < 0
p2 = p1 - ((fp1*(p1-p0))/(fp1 - fp0));
error = abs(p2-p1);
n = 1;
fprintf('\nn=%2.0f P0=%3.15f P1=%3.15f P2=%3.15f Error=%1.2e\n',n,p0,p1,p2,error)
fp2 = subs(fx,p2);
while error>precision
n = n+1;
if fp1*fp2 < 0
p0 = p1;
p1 = p2;
else
p1 = p2;
end
fp0 = subs(fx,p0);
fp1 = subs(fx,p1);
p2 = p1 - ((fp1*(p1-p0))/(fp1 - fp0));
error = abs(p2 - p1);
fprintf('n=%2.0f P0=%3.15f P1=%3.15f P2=%3.15f Error=%1.2e\n',n,p0,p1,p2,error)
fp2 = subs(fx,p2);
end
fprintf('\n--> El valor de aproximación P2 es: %3.15f\n\n',p2)
else
fprintf('Entre P0 y P1 no existe raiz.')
end
VALORES POSICION FALSA
fprintf('Método de Posición Falsa.\n')
syms h
ho=input('Ingrese ho: ');
h1=input('Ingrese h1: ');
V=input('Ingrese V: ');
R=input('Ingrese R: ');
L=input('Ingrese L: ');
fx = input('Fx(funcion igualada a 0): ');
precision = input('Precision: ');
fho = subs(fx,ho);
fh1 = subs(fx,h1);
if fho*fh1 < 0
h2 = h1 - ((fh1*(h1-ho))/(fh1 - fho));
error = abs(h2-h1);
n = 1;
fprintf('\nn=%2.0f ho=%3.15f h1=%3.15f h2=%3.15f Error=%1.2e\n',n,ho,h1,h2,error)
fh2 = subs(fx,h2);
while error>precision
n = n+1;
if fh1*fh2 < 0
ho = h1;
h1 = h2;
else
h1 = h2;
end
fho = subs(fx,ho);
fh1 = subs(fx,h1);
h2 = h1 - ((fh1*(h1-ho))/(fh1 - fho));
error = abs(h2 - h1);
fprintf('n=%2.0f ho=%3.15f h1=%3.15f h2=%3.15f Error=%1.2e\n',n,ho,h1,h2,error)
fh2 = subs(fx,h2);
end
fprintf('\n--> El valor de aproximación es: %3.15f\n\n',h2)
else
fprintf('Entre ho y h1 no existe raiz.')
end
STEFFENSEN
ResponderEliminarfprintf('Método de Steffensen.\n')
syms x
p0 = input('P0: ');
fx = input('Fx(despejar x de la funcion y insertar la ec. de x): ');
precision = input('Precisión: ');
p1 = subs(fx,p0);
p2 = subs(fx,p1);
p = p0 - (((p1 - p0)^2)/(p2 - 2*p1 + p0));
error = abs(p - p0);
n = 1;
fprintf('\nn=%2.0f P0=%3.15f P1=%3.15f P2=%3.15f P=%3.15f Error=%1.2e\n',n,p0,p1,p2,p,error)
while error>precision
n=n+1;
p0 = p;
p1 = subs(fx,p0);
p2 = subs(fx,p1);
p = p0 - (((p1 - p0)^2)/(p2 - 2*p1 + p0));
error = abs(p - p0);
fprintf('n=%2.0f P0=%3.15f P1=%3.15f P2=%3.15f P=%3.15f Error=%1.2e\n',n,p0,p1,p2,p,error)
end
fprintf('\n--> El valor de aproximación P es: %3.15f\n\n',p)
VALORES STEFFENSEN
fprintf('Método de Steffensen.\n')
syms V
P=input('Ingrese P(kPa): ');
T=input('Ingrese T(°K): ');
Pc=input('Ingrese Pc(Kpa): ');
Tc=input('Ingrese Tc(°K): ');
R=input('Ingrese R(kJ/kg°K): ');
a=(27*((R*Tc)^(2)))/(64*Pc);
b=(R*Tc)/(8*Pc);
Vo = input('Ingrese Vo: ');
f = input('Ingrese la Funcion de trabajo(despejar x de la funcion y insertar la ec. de x): ');
precision = input('Precisión: ');
V1 = subs(f,Vo);
V2 = subs(f,V1);
V = Vo - (((V1 - Vo)^2)/(V2 - 2*V1 + Vo));
error = abs(V - Vo);
n = 1;
fprintf('\nn=%2.0f Vo=%3.15f V1=%3.15f V2=%3.15f V=%3.15f Error=%1.2e\n',n,Vo,V1,V2,V,error)
while error>precision
n=n+1;
Vo = V;
V1 = subs(f,Vo);
V2 = subs(f,V1);
V = Vo - (((V1 - Vo)^2)/(V2 - 2*V1 + Vo));
error = abs(V - Vo);
fprintf('n=%2.0f Vo=%3.15f V1=%3.15f V2=%3.15f V=%3.15f Error=%1.2e\n',n,Vo,V1,V2,V,error)
end
fprintf('\n--> El valor de aproximación V es: %3.15f\n\n',V)
Buenas, podria subir los metodos de adams pero para el caso de pasos de integracion variables, es decir, que la "h" varie en cada paso
ResponderEliminar