0% found this document useful (0 votes)
86 views

Function: 'ERROR, Inserte Otros Valores Iniciales'

This document contains functions and code for numerical methods used to solve equations including: - Bisection method for root finding - Newton's method for root finding - Secant method for root finding - Romberg's method for numerical integration - Gaussian elimination for solving systems of equations - Euler's method for solving differential equations It provides the code to implement these numerical methods and includes comments to explain what each section is calculating.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Function: 'ERROR, Inserte Otros Valores Iniciales'

This document contains functions and code for numerical methods used to solve equations including: - Bisection method for root finding - Newton's method for root finding - Secant method for root finding - Romberg's method for numerical integration - Gaussian elimination for solving systems of equations - Euler's method for solving differential equations It provides the code to implement these numerical methods and includes comments to explain what each section is calculating.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

function [xr,fxr,ea,numero_iteraciones]=Biseccion(F,xl,xu,es,imax)

%xl=input('Ingrese valor menor: ');


%xu=input('Ingrese valor mayor: ');
%es=input('Ingrese error fijado porcentual: ');
%imax=input('Nmero de iteraciones: ');
%F=input('Ingrese funcin: ');
iteraciones=[xl xu];
x=xl;
fxl=eval(F);
x=xu;
fxu=eval(F);
if fxl*fxu>0
disp('ERROR, inserte otros valores iniciales')
else
iter=0;
while fxl*fxu~=0||iter<imax
%if iter>imax
%error('Excede nmero de iteraciones')
%break
%end
xr=(xl+xu)/2;
x=xr;
fxr=eval(F);
iter=iter+1;
%fprintf('Iteracin %i\n',iter)
if fxr==0
xl=xr;
xu=xr;
elseif fxl*fxr>0
xl=xr;
if abs((xl-xu)/xl)*100<es
break
end
else
xu=xr;
if abs((xu-xl)/xu)*100<es
break
end
end
iteraciones=[iteraciones;xl xu];
x=xl;
fxl=eval(F);
end
end
xr=(xl+xu)/2;
x=xr;
fxr=eval(F);
ea=abs((xr-xl)/xr)*100;
iteraciones=[iteraciones;xl xu];
numero_iteraciones=iter;
%fprintf('El valor de la raiz es: %8.9f\n',xr)
%fprintf('Valor de la funcin evaludada en xr: %7.8e\n',fxr)
%%fprintf('El error relativo porcentual es: %7.8e\n',ea)
%fprintf('El nmero de iteraciones es: %i\n',numero_iteraciones)
end
function [xr,Ea,num_iter]=Newtonra(EPS,EPS1,x0,imax)
F=input('Ingrese funcin: ');
i=1;
dF=diff(sym(F))
while i<=imax
x=x0;
Fx0=eval(F);
dFx=eval(sym(dF));
if dFx==0
error('Error, no converge')
break
end
xi=x0-Fx0/dFx;
x=xi;
Fxi=eval(F);
i=i+1;
if abs(xi-x0)<EPS
fprintf('\n')
disp('RESPUESTAS')
fprintf('La raiz es: %8.9f\n',xi)
disp('Criterio de convergencia')
break
end
if abs(Fxi)<EPS1
fprintf('\n')
disp('RESPUESTAS')
fprintf('La raiz es: %8.9f\n',xi)
disp('Criterio de exactitud')
break
end
x0=xi;
end
xr=xi;
Ea=abs(xi-x0);
num_iter=i+1;
Ea=abs(xi-x0);
end
%SECANTE
F=input('Ingrese funcin: ');
xi=input('Ingrese xi: ');
xime1=input('Ingrese x(i-1): ');
EPS=input('Ingrese criiterio de convergencia: ');
EPS1=input('Ingrese criterio de exactitud: ');
imax=input('Ingrese nmero de iteraciones: ');
i=0;
while i<=imax
i=i+1;
x=xi;
Fxi=eval(F);
x=xime1;
Fxime1=eval(F);
xima1=xi-(Fxi*(xime1-xi)/(Fxime1-Fxi));
x=xima1;
Fxima1=eval(F);
if abs(xi-xime1)<EPS
fprintf('\n')
disp('RESPUESTAS: ')
fprintf('La raiz es: %8.9f\n',xima1)
Ea=abs(xi-xime1);
fprintf('Ea: %8.9f\n',Ea)
n=length(i);
fprintf('Nmero de iteraciones: %8.9f\n',i)
disp('Criterio de convergencia')
break
end
if abs(Fxima1)<EPS1
fprintf('\n')
disp('RESPUESTAS')
fprintf('La raiz es: %8.9f\n',xima1)
Ea=abs(xi-xime1);
fprintf('Ea: %8.9f\n',Ea)
fprintf('Nmero de iteraciones: %8.9f\n',i)
disp('Criterio de exactitud')
break
end
xime1=xi;
xi=xima1;
end
if i>imax
error('ERROR, excede nmero de iteraciones')
end
function [In,t,et,I,k]=Romberg(a,b,imax,es,f,m)
%a=Lmite inferior,
%b=Lmite supeior
%imax=Nmero mximo de iteraciones
%es=Error fijado porcentual
%f=Funcin
%n=Nmero de segmentos
%m=Grado de la derivada para error de truncamiento
I=zeros(8);
n=1;
I(1,1)=trapezoide(f,a,b,n,m);
z=0;
while z<=imax
z=z+1;
n=2^z;
I(z+1,1)=trapezoide(f,a,b,n,m);
for k=2:z+1
j=2+z-k;
I(j,k)=(4^(k-1)*I(j+1,k-1)-I(j,k-1))/(4^(k-1)-1);
end
ea=abs((I(1,z+1)-I(2,z))/I(1,z+1))*100;
if ea<=es
break
end
end
In=I(j,k);
t=double(int(sym('x^0.1*(1.2-x)*(1-exp(20*(x-1)))'),a,b));
et=((t-In)/t)*100;
end

function p=trapezoide(f,a,b,n,m)
h=(b-a)/n;
n=n+1;
Y=zeros(n,1);
X=zeros(n,1);
suma=0;
for i=1:n
X(i)=a+h*(i-1);
x=X(i);
Y(i)=eval(f);
end
for i=2:n-1
suma=suma+Y(i);
end
p=0.5*h*(Y(1)+2*suma+Y(n));
Et=-1/12*dprom(f,a,b,m)*(b-a)^3;
Ea=-1/(12*n^2)*dprom(f,a,b,m)*(b-a)^3;
end
function dp=dprom(f,a,b,m)
dp=int(diff(sym(f),m),a,b)/(b-a);
end

function [z1,z2,z3,v1,v2,v3]=gaussp(a,b,tol)
n=length(b);
er=0;
s=zeros(1,n);
for i=1:n
s(i)=abs(a(i,1));
for j=2:n
if abs(a(i,j))>s(i)
s(i)=a(i,j);
end
end
end
[a,b,s,er,f]=eliminate(a,b,n,s,tol,er);
if er~=-1
x=substitute(a,b,n);
end
z1=x(1);
z2=x(2);
z3=x(3);
%Comprobando
v1=83/100*z1+55/100*z3;
v2=61/100*z2+24/100*z3;
v3=17/100*z1+39/100*z2+21/100*z3;
end

function [a,b,s,er,f]=eliminate(a,b,n,s,tol,er)
%er=0;
disp('f=')
for k=1:n-1
[a,b,s]=pivot(a,b,n,s,k);
if abs(a(k,k)/s(k))<tol
er=-1;
break
end
for i=k+1:n
f(i)=a(i,k)/a(k,k);
%disp(f(i));
for j=1:n
a(i,j)=a(i,j)-f(i)*a(k,j);
end
b(i)=b(i)-f(i)*b(k);
end
end
if abs(a(k,k)/s(k))<tol
er=-1;
end
end
function [a,b,s]=pivot(a,b,n,s,k)
p=k;
big=abs(a(k,k)/s(k));
for r=k+1:n
T=abs(a(r,k)/s(r));
if T>big
big=T;
p=r;
end
end
if p~=k
for m=k:n
T=a(p,m);
a(p,m)=a(k,m);
a(k,m)=T;
end
T=b(p);
b(p)=b(k);
b(k)=T;
T=s(p);
s(p)=s(k);
s(k)=T;
end
end

function x=substitute(a,b,n)
x(n)=b(n)/a(n,n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+a(i,j)*x(j);
end
x(i)=(b(i)-sum)/a(i,i);
end
end
function [I,t,et]=gaussl(f,a,b,n)
syms z
x=((b+a)+(b-a)*z)/2;
dx=(b-a)/2;
fu=eval(f);
fun=fu*dx;
switch n
case 2
c0=1;
c1=1;
x0=-1/sqrt(3);
x1=1/sqrt(3);
z=x0;
fun0=eval(fun);
z=x1;
fun1=eval(fun);
I=c0*fun0+c1*fun1;
case 3
c0=0.5555556;
c1=0.8888889;
c2=0.5555556;
x0=-0.774596669;
x1=0;
x2=0.774596669;
z=x0;
fun0=eval(fun);
z=x1;
fun1=eval(fun);
z=x2;
fun2=eval(fun);
I=c0*fun0+c1*fun1+c2*fun2;
case 4
c0=0.3478548;
c1=0.6521452;
c2=0.6521452;
c3=0.3478548;
x0=-0.861136312;
x1=-0.339981044;
x2=0.339981044;
x3=0.861136312;
z=x0;
fun0=eval(fun);
z=x1;
fun1=eval(fun);
z=x2;
fun2=eval(fun);
z=x3;
fun3=eval(fun);
I=c0*fun0+c1*fun1+c2*fun2+c3*fun3;
case 5
c0=0.2369269;
c1=0.4786287;
c2=0.5688889;
c3=0.4786287;
c4=0.2369269;
x0=-0.906179846;
x1=-0.538469310;
x2=0;
x3=0.538469310;
x4=0.906179846;
z=x0;
fun0=eval(fun);
z=x1;
fun1=eval(fun);
z=x2;
fun2=eval(fun);
z=x3;
fun3=eval(fun);
z=x4;
fun4=eval(fun);
I=c0*fun0+c1*fun1+c2*fun2+c3*fun3+c4*fun4;
case 6
c0=0.1713245;
c1=0.3607616;
c2=0.4679139;
c3=0.4679139;
c4=0.3607616;
c5=0.1713245;
x0=-0.932469514;
x1=-0.661209386;
x2=-0.238619186;
x3=0.238619186;
x4=0.661209386;
x5=0.932469514;
z=x0;
fun0=eval(fun);
z=x1;
fun1=eval(fun);
z=x2;
fun2=eval(fun);
z=x3;
fun3=eval(fun);
z=x4;
fun4=eval(fun);
z=x5;
fun5=eval(fun);
I=c0*fun0+c1*fun1+c2*fun2+c3*fun3+c4*fun4+c5*fun5;
otherwise
disp('ERROR, el nmero de puntos debe estar comprendido entre 2 y 6')
end
t=double(int(sym(f),a,b));
et=(t-I)/t*100;
end
%EULER
%clear all;clc
n=input('Ingrese nmero de ecuaciones: ');
y=input('Variables iniciales dependientes: ');
xi=input('Variable inicial independiente: ');
xf=input('Variable final independiente: ');
dx=input('Tamao de paso: ');
xout=input('Intervalo de salida: ');
x=xi;
m=1;
xp(m)=x;
F=teoricrk(x,y);
for i=1:n
yp(i,m)=y(i);
end
a(m)=9.8-0.27*yp(i,m);
for i=1:n
t(i,m)=eval(F(i));
e(i,m)=(t(i,m)-yp(i,m))/t(i,m)*100;
end
A(m)=9.8-0.27*t(i,m);
while x<xf
xend=x+xout;
if xend>xf
xend=xf;
end
h=dx;
[h,x,y]=integrator(x,y,h,xend);
m=m+1;
xp(m)=x;
for i=1:n
yp(i,m)=y(i);
end
a(m)=9.8-0.27*yp(i,m);
for i=1:n
t(i,m)=eval(F(i));
e(i,m)=(t(i,m)-yp(i,m))/t(i,m)*100;
end
A(m)=9.8-0.27*t(i,m);
end
plot(xp,t)
hold on
plot(xp,yp,'--r')
%plot(xp,a,'--r')
%plot(xp,A,'g')
hold off
grid on
%disp('x ye yt e')
%fprintf('%i %i %i
%i\n',xp(1),yp(1,1),t(1,1),0)
%for i=2:(xf-xi)/dx+1
% fprintf('%1.1f %9.10f %9.10f
%9.10f\n',xp(i),yp(1,i),t(1,i),e(1,i))
%end
disp('x ye1 ye2 yV1 yV2
et1 et2')
fprintf('%i %i %i %i %i
%i %i\n',xp(1),yp(1,1),yp(2,1),t(1,1),t(2,1),e(1,1),e(2,1))
for i=2:(xf-xi)/dx+1
fprintf('%1.1f %5.6f %5.6f %5.6f %5.6f
%5.6f %5.6f\n',xp(i),yp(1,i),yp(2,i),t(1,i),t(2,i),e(1,i),e(2,i))
end

function F=teoricrk(x,y)
syms f(x) g(x)
S=dsolve(diff(f)==f*x^2-1.2*f,diff(g)==(1+x)*g^(0.5),f(0)==1,g(0)==1);
F(1)=S.f(1);
F(2)=S.g(1);
end

function [h,x,y]=integrator(x,y,h,xend)
while x<xend
if xend-x<h
h=xend-x;
end
[x,ynew]=euler(x,y,h);
y=ynew;
end
end

function [x,ynew]=euler(x,y,h)
dydx=derivs(x,y);
ynew=y+dydx*h;
x=x+h;
end

function dydx=derivs(x,y)
dydx(1)=1/(0.55*sqrt(2*9.81*x));
%dydx(2)=(1+x)*(y(2))^0.5;
end
%RUNGE-KUTTA
clear all;clc
n=input('Nmero de ecuaciones: ');
y=input('Valores iniciales dependientes: ');
xi=input('Valor inicial de la variable independiente: ');
xf=input('Valor final de la variable independiente: ');
dx=input('Tamao de paso: ');
xout=input('Tamao de paso: ');
x=xi;
m=1;
xp(m)=x;
F=teoricrk(x,y);
for i=1:n
yp(i,m)=y(i);
end
for i=1:n
t(i,m)=eval(F(i));
e(i,m)=(t(i,m)-yp(i,m))/t(i,m)*100;
end
while x<xf
xend=x+xout;
if xend>xf
xend=xf;
end
h=dx;
[x,y]=integratork(x,y,n,h,xend);
m=m+1;
xp(m)=x;
for i=1:n
yp(i,m)=y(i);
end
for i=1:n
t(i,m)=eval(F(i));
e(i,m)=(t(i,m)-yp(i,m))/t(i,m)*100;
end
end
plot(xp,yp,'--r')
hold on
plot(xp,t)
grid on
hold off
%disp(xp)
%disp(yp)
disp('x yRK zRK yV
zV e1 e2')
fprintf('%i %i %i %i
%i %i%%
%i%%\n',xp(1),yp(1,1),yp(2,1),t(1,1),t(2,1),e(1,1),e(2,1))
for i=2:(xf-xi)/dx+1
fprintf('%1.1f %5.6f %5.6f %5.6f
%5.6f %7.8f%%
%7.8f%%\n',xp(i),yp(1,i),yp(2,i),t(1,i),t(2,i),e(1,i),e(2,i))
end

function [x,y]=integratork(x,y,n,h,xend)
while x<xend
if (xend-x)<h
h=xend-x;
end
[x,y]=RK4(x,y,n,h);
end
end

function [x,y]=RK4(x,y,n,h)
K1=derivrk(x,y);
for i=1:n
ym(i)=y(i)+K1(i)*(h/2);
end
K2=derivrk(x+h/2,ym);
for i=1:n
ym(i)=y(i)+K2(i)*(h/2);
end
K3=derivrk(x+h/2,ym);
for i=1:n
ye(i)=y(i)+K3(i)*h;
end
K4=derivrk(x+h,ye);
for i=1:n
slope(i)=(K1(i)+2*(K2(i)+K3(i))+K4(i))/6;
y(i)=y(i)+slope(i)*h;
end
x=x+h;
end

function [dy]=derivrk(x,y)
dy(1)=y(1)*x^2-1.2*y(1); %Funciones del problema
dy(2)=(1+x)*(y(2))^(0.5);
%dy(3)=sin(x*y(3))+y(4);
%dy(4)=cos(y(4));
end

You might also like