Sei sulla pagina 1di 3

%Método punto fijo% %Método de Bisección %Método Newton R)% %(Metodo Secante)%

f='-sin(x)/(3*x)'; syms x; syms x;


p0=-1; df=inline(diff(f)); for i=1:n
n=8; %% f=inline(f); p=p1-(p1-
tol=1e-4; f=inline(f); p0)*f(p1)/(f(p1)-f(p0));
f=inline(f); for i=1:n for i=1:n err=abs(p-p1);
c=(a+b)/2; p=p0-f(p0)/df(p0); relerr=err/abs(p);
for i=1:n resultB(i,:)=[i a b c err=abs(p-p0); R(i,:)=[i p err relerr];
p=f(p0); abs(f(c))]; relerr=err/abs(p); if tol>err || tol>relerr
err=abs(p-p0); if tol>abs(f(c)) R(i,:)=[i p err || tol>abs(f(p))
relerr=err/abs(p); break relerr]; break
R(i,:)=[i p err relerr]; elseif f(c)*f(a)<0 if tol>err || else
if err<tol || relerr<tol b=c; tol>relerr || p0=p1;
|| abs(f(p))<tol else a=c; tol>abs(f(p)) p1=p;
break; end break; end
else end else end
p0=p; resultB p0=p; R
end end
end end
R R

%Jacobi% Método Gauss-Siedel %ejercicio 4(método de la


tol=1e-3; n=length(B); falsa posición)%
max1=12; %% f=inline(f);
n=length(B); X=Po;
for i=1:m for i=1:n
for k=1:max1 for j=1:n c1=a1-((b1-
for j=1:n X(j)=(B(j)-A(j,[1:j-1 a1)*f(a1))/(f(b1)-f(a1));
X1(j)=(B(j)-A(j,[1:j- j+1:n])*X([1:j-1 j+1:end]))/A(j,j); R1(i,:)=[i c1
1,j+1:n])*Po1([1:j- end abs(f(c1))];
1,j+1:n]))/A(j,j); err=norm(X-Po); if f(c1)*f(a1)<0
end relerr=err/norm(X); b1=c1;
err=abs(norm(X1'-Po1)); R(i,:)=[i X' err relerr]; else
relerr=err/norm(X1); if tol>err || tol>relerr a1=c1;
Po1=X1'; break end
R1(k,:)=[k X1 err relerr]; else if tol>abs(f(c1))
if err<tol || relerr<tol Po=X; break
break end end
end end end
R

% (Interpolación Lagrange)% % (Interpolación Newton)% (Método Euler)% (Método Heun)%


x=[8.1 8.3 8.6 8.7]; x=[-0.75 -0.5 -0.25 0]; t0=1; syms x;
y=[16.94410 17.56492 18.50515 y=[-0.07181250 -0.02475000 syms t; f=
18.82091]; 0.334993750 1.10100000]; f=[-2/(t^2) 2/t]; g=exp(x);
n=length(x); n=length(x); g=t*log(t); m=length(y0);
P=zeros(1,n); m=length(y0); result=[0 x0 y0(1)];
p=[1 -x(1)]; result=[0 t0 y0(1)]; A=[zeros(m-1,1) eye(m-1);f];
for i=1:n a=y(1); A=[zeros(m-1,1) eye(m-1);f]; B=[zeros(1,m-1),g]';
a=x([1:i-1 1+1:n]); P=a; B=[zeros(1,m-1),g]'; x=x0;
p=[1 -a(1)]; for i=2:n t=t0; for i=1:n
for j=2:n-1 a=(y(i)- for i=1:n dy1=eval(A)*y0+eval(B);
p=conv(p,[1 -a(j)]); polyval(P,x(i)))/polyval(p,x dy=eval(A)*y0+eval(B); x=x+h;
end (i)); y=y0+h*dy; y1=y0+h*dy1;
pj=y(i)*p/polyval(p,x(i)); resultN(1,i)=a; y0=y; dy2=eval(A)*y1+eval(B);
resultL(i,:)=[i,pj]; P=[a*p(1) a*p(2:end)+P]; t=t+h; y=y0+(h/2)*(dy1+dy2);
P=P+pj; p=conv(p,[1 -x(i)]); result=[result;i t y(1)]; result=[result;i x y(1)];
end end end y0=y;
resultL(n+1,:)=[n+1,P] resultN=[resultN;P] result end
result

(Simpson 1/3)% Simpson 3/8) 10(Método Rectángulo)% (Método del trapecio)%


A=0; f='1/(1+x^5)'; a1=0;
h=(b-a)/(2*n); %% n=25; b1=2;
f=inline(f); Q=0; a=1; h1=(b1-a1)/n;
j=1; h=(b-a)/(3*n); b=3; A1=0;
for i=a:2*h:b-2*h j=1; %% f1=inline(f1);
for i=a:3*h:b-3*h A=0; j1=1;
A=A+(h/3)*(f(i)+4*f(i h=(b-a)/n; for i=a1:h1:b1-h1
+h)+f(i+2*h)); f=inline(f);
result(j,:)=[j Q=Q+(3*h/8)*(f(i)+3*f(i+h) j=1; A1=A1+(h1/2)*(f1(i)+f1(
A]; +3*f(i+2*h)+f(i+3*h)); for i=a:h:b-h i+h1));
j=j+1; resultQ(j,:)=[j Q]; Xm=(2*i+h)/2; result(j1,:)=[j1
end j=j+1; A=A+h*f(Xm); A];
result result(j,:)=[j A]; j1=j1+1;
result j=j+1; end
end result
result
Runge Kutta O^4)% %milne Simpson% %hamming%
x0=0; syms x; syms x;
y0=[-0.8 0]'; f=[8/(1+x)^3 -1/(1+x)^2 -1/(1+x)]; f=[8/(1+x)^3 -1/(1+x)^2 -1/(1+x)];
n=100; g=x/(1+x)^5; g=x/(1+x)^5;
h=0.01; %% %%
x2=x1+h; x2=x1+h;
m=length(y0); x3=x2+h; x3=x2+h;
syms x; x4=x3+h; x4=x3+h;
f=[-16*x 0*(x/x)]; m=length(y1); m=length(y1);
g=0*(x/x); resultMS=[x1,y1(1);x2,y2(1);x3,y3(1);x4,y4(1)]; resultHg=[x1,y1(1);x2,y2(1);x3,y3(1);x4,y4(1)];
x=x0; A=[zeros(m-1,1) eye(m-1);f]; A=[zeros(m-1,1) eye(m-1);f];
A=[zeros(m-1,1) eye(m-1,1);f]; B=[zeros(1,m-1),g]'; B=[zeros(1,m-1),g]';
B=[zeros(1,m-1),g]'; for i=1:n for i=1:n
result=[0 x0 y0(1)]; x=x2; x=x2;
for i=1:n dy2=eval(A)*y2+eval(B); dy2=eval(A)*y2+eval(B);
dy1=eval(A)*y0+eval(B); x=x3; x=x3;
x=x+h/2; dy3=eval(A)*y3+eval(B); dy3=eval(A)*y3+eval(B);
x=x4; x=x4;
y1=y0+(h/2)*dy1; dy4=eval(A)*y4+eval(B); dy4=eval(A)*y4+eval(B);
dy2=eval(A)*y1+eval(B);
y2=y0+(h/2)*dy2; p=y1+(4/3)*h*(2*dy2-dy3+2*dy4); p=y1+(4/3)*h*(2*dy2-dy3+2*dy4);
dy3=eval(A)*y2+eval(B); x=x+h; x=x4+h;
x=x+h/2; dy5=eval(A)*p+eval(B); dy5=eval(A)*p+eval(B);
y3=y0+(h/2)*dy3;
dy4=eval(A)*y3+eval(B); y=y3+(h/3)*(dy3+4*dy4+dy5); y=(9*y4-y2)/8+(3/8)*h*(-dy3+2*dy4+dy5);
resultMS=[resultMS;x y(1)]; resultHg=[resultHg;x y(1)];
y=y0+h*(dy1+2*dy2+2*dy3+dy4)/6; y1=y2; y1=y2;
result=[result;i x y(1)]; y2=y3; y2=y3;
y0=y; y3=y4; y3=y4;
end y4=y; y4=y;
result x2=x3; x2=x3;
x3=x4; x3=x4;
x4=x; x4=x;

end end
resultMS resultHg

%milne Simpson mod% %Hamming Mod%


syms x;
syms x; f=[8/(1+x)^3 -1/(1+x)^2 -1/(1+x)];
f=[8/(1+x)^3 -1/(1+x)^2 -1/(1+x)]; g=x/(1+x)^5;
g=x/(1+x)^5; %%
%% x2=x1+h;
x2=x1+h; x3=x2+h;
x3=x2+h; x4=x3+h;
x4=x3+h; m=length(y1);
m=length(y1); resultHgm=[x1,y1(1);x2,y2(1);x3,y3(1);x4,y4(1)];
resultMSm=[x1,y1(1);x2,y2(1);x3,y3(1);x4,y4(1)]; A=[zeros(m-1,1) eye(m-1);f];
A=[zeros(m-1,1) eye(m-1);f]; B=[zeros(1,m-1),g]';
B=[zeros(1,m-1),g]'; for i=1:n
for i=1:n x=x2;
x=x2; dy2=eval(A)*y2+eval(B);
dy2=eval(A)*y2+eval(B); x=x3;
x=x3; dy3=eval(A)*y3+eval(B);
dy3=eval(A)*y3+eval(B); x=x4;
x=x4; dy4=eval(A)*y4+eval(B);
dy4=eval(A)*y4+eval(B);
p=y1+(4/3)*h*(2*dy2-dy3+2*dy4);
p=y1+(4/3)*h*(2*dy2-dy3+2*dy4); if i>1
if i>1 mod=p+(112/121)*(y4-p0);
mod=p+(28/29)*(y4-p0); else mod=p;
else mod=p; end
end x=x4+h;
x=x+h; dy5=eval(A)*mod+eval(B);
dy5=eval(A)*mod+eval(B);
y=(9*y4-y2)/8+(3/8)*h*(-dy3+2*dy4+dy5);
y=y3+(h/3)*(dy3+4*dy4+dy5); resultHgm=[resultHgm;x y(1)];
resultMSm=[resultMSm;x y(1)]; y1=y2;
y1=y2; y2=y3;
y2=y3; y3=y4;
y3=y4; y4=y;
y4=y; x2=x3;
x2=x3; x3=x4;
x3=x4; x4=x;
x4=x; p0=p;
p0=p; end
end resultHgm
resultMSm