Sie sind auf Seite 1von 10

METODO DE NEWTON CON ITERACION GRAFICA

%newt_g
%objetivo: resuelve una ecuacion no linial
%sintaxis: >> newt_g('nombre_f', x0,xmin,xmax,n_puntos)
%nombre_f: nombre de la funcion que define la ecuacion no linial
%x0: iteracion inicial
%xmin,xmax: cordenadas 'x'minimo' y 'x'maximo de la grafica
%n_puntos: numero de puntos utilizado9s para trazar la curva
function x=newt_g(nombre_f, x0,xmin,xmax,n_puntos)
clf, hold off
% metodo de newton con iteracion grafica
del_x=0.001;
wid_x=xmax-xmin; dx=(xmax-xmin)./n_puntos;
xp=xmin:dx:xmax; yp=feval(nombre_f,xp);
plot(xp,yp); xlabel('x'); ylabel('f(x)');
title('iteracion de newton'); hold on
ymin=min(yp);ymax=max(yp); wid_y=ymax-ymin
yp=0.*xp; plot(xp,yp)
x=x0; xb=x+999; n=0;
while abs(x-xb)>0.000001
if n>300 break;end
y=feval(nombre_f,x); plot([x,x],[y,0]); plot(x,0,'o')
fprintf('n=%3.0f,x=%12.5e,y=%12.5e\n',n,x,y)
xsc=(x-xmin)/wid_x;
if n>4, text(x,wid_y/20,[num2str(n)]),end
y_driv=(feval(nombre_f,x+del_x)-y)/del_x;
xb=x;
x=xb-y./y_driv; n=n+1;
plot([xb,x],[y,0])
end
plot([x,x],[0.05*wid_y 0.2*wid_y]), grid
text(x,0.2*wid_y,'solucion final')
plot([x (x-wid_x*0.004 )],[0.01*wid_y 0.09*wid_y])
plot([x (x+wid_x*0.004 )],[0.01*wid_y 0.09*wid_y])

EVALUAR
function y=newt_uno(x)
y=(0.01*x+1).*sin(x)-(x-0.01).*(x.^2+1).^(-1)-0.0096;

RESPUETA
>> newt_g('newt_uno',2,0,5,100)

wid_y =1.8241
n= 0,x=2.00000e+000,y=5.19883e-001
n= 1,x=3.74774e+000,y=-8.49087e-001
n= 2,x=2.68741e+000,y=1.15289e-001
n= 3,x=2.82675e+000,y=-4.47860e-003
n= 4,x=2.82171e+000,y=-5.73265e-006
n= 5,x=2.82170e+000,y=-1.23656e-009
ans =2.8217


Ejemplo 111111
Primeramente evaluamos por el metodo grafico de la ecuacion 1
>>x=0:0.05:1000; y=1.25.*(x-625)./0.05+0.8.*5.67.*10.^-8(x.^4-298.^4)+20.*(x-298);
plot(x,y),grid

Newton_g
%newt_g
%objetivo: resuelve una ecuacion no linial
%sintaxis: >> newt_g('nombre_f', x0,xmin,xmax,n_puntos)
%nombre_f: nombre de la funcion que define la ecuacion no linial
%x0: iteracion inicial
%xmin,xmax: cordenadas 'x'minimo' y 'x'maximo de la grafica
%n_puntos: numero de puntos utilizado9s para trazar la curva
function x=newt_g(nombre_f, x0,xmin,xmax,n_puntos)
clf, hold off
% metodo de newton con iteracion grafica
del_x=0.001;
wid_x=xmax-xmin; dx=(xmax-xmin)./n_puntos;
xp=xmin:dx:xmax; yp=feval(nombre_f,xp);
plot(xp,yp); xlabel('x'); ylabel('f(x)');
title('iteracion de newton'); hold on
ymin=min(yp);ymax=max(yp); wid_y=ymax-ymin
yp=0.*xp; plot(xp,yp)
x=x0; xb=x+999; n=0;
while abs(x-xb)>0.000001
if n>300 break;end
y=feval(nombre_f,x); plot([x,x],[y,0]); plot(x,0,'o')
fprintf('n=%3.0f,x=%12.5e,y=%12.5e\n',n,x,y)
xsc=(x-xmin)/wid_x;
if n>4, text(x,wid_y/20,[num2str(n)]),end
y_driv=(feval(nombre_f,x+del_x)-y)/del_x;
xb=x;
x=xb-y./y_driv; n=n+1;
plot([xb,x],[y,0])
end
plot([x,x],[0.05*wid_y 0.2*wid_y]), grid
text(x,0.2*wid_y,'solucion final')
plot([x (x-wid_x*0.004 )],[0.01*wid_y 0.09*wid_y])
plot([x (x+wid_x*0.004 )],[0.01*wid_y 0.09*wid_y])


Polinomios- interpolacion
Polinomio: expresamos la forma deserie de potencia de un polinomio con :
Y=c1x^n+c2
Ejemplo1

>> p=[2 1 4 5];roots(p)

ans =

0.2500 + 1.5612i
0.2500 - 1.5612i
-1.0000
Ejemplo1

>> p=[1 -3 -4];roots(p)

ans =

4
-1
>> help polyval
polyval Evaluate polynomial.
Y = polyval(P,X) returns the value of a polynomial P evaluated at X. P
is a vector of length N+1 whose elements are the coefficients of the
polynomial in descending powers.

Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)

If X is a matrix or vector, the polynomial is evaluated at all
points in X. See POLYVALM for evaluation in a matrix sense.

[Y,DELTA] = polyval(P,X,S) uses the optional output structure S created
by POLYFIT to generate prediction error estimates DELTA. DELTA is an
estimate of the standard deviation of the error in predicting a future
observation at X by P(X).

If the coefficients in P are least squares estimates computed by
POLYFIT, and the errors in the data input to POLYFIT are independent,
normal, with constant variance, then Y +/- DELTA will contain at least
50% of future observations at X.

Y = polyval(P,X,[],MU) or [Y,DELTA] = polyval(P,X,S,MU) uses XHAT =
(X-MU(1))/MU(2) in place of X. The centering and scaling parameters MU
are optional output computed by POLYFIT.

Example:
Evaluate the polynomial p(x) = 3x^2+2x+1 at x = 5,7, and 9:

p = [3 2 1];
polyval(p,[5 7 9])%

Class support for inputs P,X,S,MU:
float: double, single

See also polyfit, polyvalm.

Overloaded methods:
gf/polyval

Reference page in Help browser
doc polyval

ejemplo6
>> p=[3 -7 2 1 1]; xi=2.5; yi=polyval(p,xi)

yi =

23.8125
ejemplo6

>> p=[3 -7 2 1 1]; xi=[1 2 5]; yi=polyval(p,xi)

yi =

0 3 1056
poly_itg(p)
%
%integracion de un polinomio
%objetivo: encontrar los coeficientes del polinomio de integracion
%sintaxis: py=poly_itp(p)
%p: cueficiente del polinomio por integracion
%py: cueficiente de polinomio ya integrado
function py=poly_itg(p)
n=length(p)
py=[p.*[n:-1:1].^(-1),0]
>> p=[3 -7 2 1 1];poly_itg(p)

n =

5


py =

0.6000 -1.7500 0.6667 0.5000 1.0000 0


ans =

0.6000 -1.7500 0.6667 0.5000 1.0000 0
% objetivo:sumar dos polinomios
% sintaxis: c=poly_add(a,b)
% a,b:arreglo de coeficientes de los polinomios
% c:arreglo de coeficiente despues de la suma
fuction c=poly_add(a,b)
n1=length(a);n2=length(b);
if n1==n2 c=a+b;end
if n1>n2 c=a+[zeros(1,n1-n2),b];end
if n1<n2 c=[zeros(1,n2-n1),a]+b;end
interpolacion linial
interp1

% interpolando para valores de:
%tem = 300,400,500,600
%beta= 1000(3.33,2.50,2.00,1.67)
%alfa= 10000(0.2126,0.3605,0.5324,0.7190)
%para ti= (321,440,571)cuanto vale temp, beta y alfa
temp =[300 400 500 600]';
beta =1000.*[3.33 2.50 2.00 1.67]';
alfa =10000.*[0.2128 0.3605 0.5324 0.7190]';
ti=[321 440 571]';
propiedad=interp1(temp,[beta,alfa],ti,'liniar');
[ti,propiedad]

>> interpo1

ans =

1.0e+003 *

0.3210 3.1557 2.4382
0.4400 2.3000 4.2926
0.5710 1.7657 6.6489
Ejemplo dos interp2
% interpolando para valores de:
%x =0,0.25,0.50,0.75,1.00
%y= 0.9162,0.8109,0.6931,0.5396,0.4055
%para yi= (0.9,0.7,0.6,0.5)cuanto vale x,y
x =[0 0.25 0.50 0.75 1.00]';
y=[0.9162 0.8109 0.6931 0.5396 0.4055]';
yi=[0.9 0.7 0.6 0.5]';
resp=interp1(y,x,yi,'liniar');
[yi,resp]


>> interpo2

ans =

0.9000 0.0385
0.7000 0.4854
0.6000 0.6516
0.5000 0.8238
Regla trapezoidal

Trapez_v
% objetivo: integrar una funcion mediante la regla trapesoidal
extendida
% sintaxis: I=trapez_v(f,h)
% f: arreglo de los datos en puntos equiespaciados. la longitud del
% vector "f" es arbitraria pero debe ser mayor que 1
% h: tamano del intervalo.
function I=trapez_v(f,h)
I=h*(sum(f)_(f(1)+f(length(f))))/2;

Trapez_n
% objetivo:integrar una funcion por la regla trapezoidal extendida
%sintaxis:trapez_n('nombre_f',a,b,n)
% nombre_f:nombre del archivo de la funcion del intgrando f(x)
% a: limite inferior de x.
% b: limite superior de x.
% n: numero de intervalos.
function I=trapez_n (nombre_f,a,b,n)
x=a+(0:n)*h;
f=feval(nombre_f,x)
I=trapez_v(f,h)


Trapez_g

% objetivo:integrar una funcion por la regla trapezoidal extendida
% mostrar un grafico.
% la sintaxis es lo mismo que trapez_n.
function I=trapez_g(nombre_f,a,b,n)
n=n; hold off
h=(b-a)/n;
x=a+(0:n)*h; f=feval(nombre_f,x);
I=(h/2)*(f(1)+f(n+1));
if n>1 I=I+h*sum(f(2:n)); end
h2=(b-a)/100;
xc=a+(0:100)*h2; fc=feval(nombre_f,xc);
plot(xc,fc,'r'); hold on
title('regla trapezoidal'); xlabel('x');ylabel('y');
plot(x,f);
plot(x,zeros(size(x)))
for i=1:n; plot([x(i),x(i)],[0,f(i)]);end


ejemplos de trapezoidal
clear
clc
n_puntos=16;
i=1:n_puntos;
h=(30-15)/(n_puntos-1);
u=15+(i-1)*h;
f=2000*u./(8.1*u.^2+1200);
x=trapez_v(f,h)





aaaaaaaaaaaa
% objetivo: integrar una funcion mediante la regla trapesoidal
extendida
% sintaxis: I=trapez_v(f,h)
% f: arreglo de los datos en puntos equiespaciados. la longitud del
% vector "f" es arbitraria pero debe ser mayor que 1
% h: tamano del intervalo.
function I=trapez_v(f,h)
I=h*(sum(f)_(f(1)+f(length(f))))/2;


aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
clear
Iexacta=4.006994;
a=0; b=2;
fprintf('\n Regla Trapezoidal Extendida \n');
fprintf('\n n I Error \n');
n=1;
for k=1:6
n=2.*n;
h=(b-a)./n; i=1:n+1;
x=a+(i-1)*h; f=sqrt(1+exp(x));

I=trapez_v(f,h);
I-Iexsacta;
fprintf('\n %3.0f %10.5f %10.5f \n',...
n, I, Iexsacta-I);
end

Das könnte Ihnen auch gefallen