Está en la página 1de 9

MTODO DE NEWTON RAPSHON

Ejemplo 1:
f=@(x)x.^3-2*x.^2+1; % definimos la funcin
df=@(x)3*x.^2-4*x; % definimos su derivada
%
Ea=0.001;x0=-1; % Datos
[x,n]=Newton1(f,df,x0,Ea) % Aplicamos la funcin
x = -0.6180
n=4
x0=0.5; % Datos
[x,n]=Newton1(f,df,x0,Ea) % Aplicamos la funcin
x=1
n=2
x0=1.5; % Datos
[x,n]=Newton1(f,df,x0,Ea) % Aplicamos la funcin
x = 1.6180
n=4

Ejemplo 2:
x0=input('Ingrese el valor inicial: ');
tol=input('Ingrese el porcentaje de error: ');
f=input('Ingrese la funcin: ');
i=1;
fx(i)=x0;

syms x;
f1=subs(f,x,fx(i));
z=diff(f);
d=subs(z,x,fx(i));

ea(1)=100;

while abs(ea(i))>=tol;
fx(i+1)=fx(i)-f1/d; f1=subs(f,x,fx(i+1)); d=subs(z,x,fx(i+1));
ea(i+1)=abs((fx(i+1)-fx(i))/fx(i+1)*100);
i=i+1;
end
fprintf('i fx(i) Error aprox (i) \n');
for j=1:i;
fprintf('%2d \t %11.7f \t %7.3f \n',j-1,fx(j),ea(j));
end

MTODO DE NEWTON RAPSHON MULTIVARIABLE


Ejercicio 1
% y = -x^2 + x + 0.5
% y + 5xy = x^2
%function

f= @(x) ...
[ -x(1)^2+x(1)+0.5-x(2) ;...
x(2)+5*x(1)*x(2)-x(1)^2 ];

%jacobian

J= @(x) ...
[ -2*x(1)+1 -1 ;...
5*x(2)-2*x(1) 1+5*x(1) ];

%initial guess:
x=[1.2; 1.2];

tol = 10e-9;
maximum_iter = 100;

% Newton?Raphson method for multidimensional methods

iter = 0;
error=1;
while (error>tol & iter<maximum_iter)
xold = x;

x = x - J(x)\f(x);
error = norm(x-xold)/norm(x);
iter = iter+1;
end
x_newton_raphson = x

Ejercicio 2
%newton raphson multivariable

clear all; close all; clc;


%funciones x^2+x*y+y^2-1=0
% x^2-y=0
f=inline('[x(1)^2+x(1)*x(2)+x(2)^2-1; x(2)-x(1)^2]');
df=inline('[2*x(1)+x(2) x(1)+2*x(2); -2*x(1) 1]');

%punto de partida para x e y


x0=[1;1];

%iteraciones mximas
maxi=10;

%tolerancia
tol=1.e-12;

corr=tol+1;
k=0;

while k<=maxi & norm(corr,inf)>tol

fx0=feval(f,x0);
dfx0=feval(df,x0);
corr=dfx0\fx0;
x=x0-corr;
x0=x;
k=k+1;
end

disp('la solucin es')


disp(x)

if norm(corr,inf)>tol
error('Se alcanz el nmero mximo de iteraciones')
end

disp(['se calculo en ' num2str(k) ' iteraciones'])

MTODO DEL PUNTO FIJO


Ejemplo 1:
xf(1)=input('Ingrese el valor inicial: ');
tol=input('Ingrese el porcentaje de error: ');
syms x;
f=input('Ingrese la funcin f(x), despejada g(f(x)): ');
i=1;
ea(1)=100;
while abs(ea(i))>=tol,
xf(i+1) = subs(f,x,xf(i));
ea(i+1) = abs((xf(i+1)-xf(i))/xf(i+1))*100;
i=i+1;
end
fprintf('i xf(i) Error aprox (i) \n');
for j=1:i;
fprintf('%2d \t %11.7f \t %7.3f \n',j-1,xf(j),ea(j));
end

Ejemplo 2:
Sea la funcin: x3 + 4x2 10 = 0 tiene una raz en [1, 2]
Puede despejarse en:
a. x = g1(x) = x x3 4x2 +10
b. x = g2(x) = (10(b) x3)
c. x = g3(x) = (10/(4 + x))
d. x = g4(x) = x (x1.5
3
+ 4x2 10)/(3x2 + 8x)
1.2869537
(a) 67 (c) (d)
1.4025408
1 1.5 03 1.5 1.5
2 -0.875 1.3454583 1.34839972 1.373333333
3 6.732421875 74 4 1.365262014
4 1.3751702 1.36737637 1.365230013
-469.72001200 52 1 1.365230013
5 1.02754555E8 1.3600941 1.36495701
6 92 5
-1.084933870E2 1.3678469 1.36526474
4 67 8
7 1.3638870 1.36522559
1.277055591E7 03 4
2 1.3659167 1.36523057
33 (c)
8
1.3648782
5 (d)
-2.082712908E2 1.36522994
16 17 1 1.5
9 NaN 1.3654100 1.36523002
1.5
61 1.348399724
10 2 1.373333333
11 1.3651378 1.367376371
20 MATRICES 1.36523001 1.365262014
12 2 1.364957015
13 1.3652772 1.36523001 1.365230013
14 08 3
1.365264748
Ejemplo 1 1.3652058 1.365230013
15 1.36523001 1.365225594
20 Operaciones Elementales 50 Definimos dos
3 matrices:
1.3652423 1.365230575
25
30
>>A=[2 1;3 2] 83 1.365229941
1.3652295
78
1.365230022
1.3652300 1.365230012
28 1.365230013
1.3652300
12 1.365230013
A=2132
>>B=[3 4;-1 5]
B = 3 4 -1 5
Para sumar las dos matrices:
>>A+B ans = 5 5 2 7
Para multiplicar una matriz por un escalar:
>>3*A
ans = 6 3
9 6
Producto de matrices:
>>C=A*B
C= 5 13
7 22
Siempre que los tamanos de las matrices sean los adecuados. Para
saber cul es el tamao de una matriz con la que estamos trabajando
>>size(A)
ans = 2 2
Que quiere decir, evidentemente, 2 filas y 2 columnas.
Para calcular la matriz transpuesta:
>>A
ans = 2 3
1 2

Ejemplo 2:
Se puede crear una matriz de 32, y asignar a la variable A de dos
formas distintas
>> A=[1 2 3
4 5 6];
>> A=[1 2 3; 4 5 6]
A=
1 2 3
4 5 6
>> A(2,2) %accede al elemento situado en la fila 2 columna 2
ans = 5
>> size(A) %dimensiones de la matriz A (2 filas, 3 columnas)
ans = 2 3
>> B=A' % B es la matriz traspuesta de A
B=
1 4
2 5
3 6
>> size(B)
ans = 3 2

Se puede crear una matriz a partir de vectores o a partir de otras


matrices

>> x1=[1,2,3]; %vectores fila


>> x2=[4,5,6];
>> A=[x1;x2]
A=
1 2 3
4 5 6
>> x1=[1;2;3]; %vectores columna
>> x2=[4;5;6];
>> A=[x1,x2]
A=
1 4
2 5
3 6

>> X=[1,2,3;4,5,6]
X=
1 2 3
4 5 6
>> Y=[7,8,9;10,11,12;13,14,15]
Y=
7 8 9
10 11 12
13 14 15
>> A=[X;Y]
A=
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
La funcion repmat crea una matriz B compuesta de la repeticin de
nm copias de A.

>> A=[1,2;3,4];
>> B=repmat(A,3,2)
B=
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
Una matriz se puede convertir en un vector columna

>> A=[1,2,3;4,5,6];
>> X=A(:)
X=
1
4
2
5
3
6
Un vector se puede convertir en una matriz diagonal mediante diag.

>> x=[1,2,3];
>> A=diag(x)
A=
1 0 0
0 2 0
0 0 3

Ejemplo 3:
Rango, Inversa y Determinante

Definimos la matriz,
>>X=[2 3 4; 1 -1 0]
X=
2 3 4
1 -1 0
Para calcular su rango,
>>rank(X)
ans =2

Supongamos que tenemos definida la siguiente matriz,


H=
8 1 6
3 5 7
492
Para calcular su inversa,
>>inv(H)
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028

Y si queremos ver el resultado en forma racional,


>>format rational
>>inv(H)
ans =
53/360 -13/90 23/360
-11/180 1/45 19/180
-7/360 17/90 -37/360

(Para ver todas las opciones del comando format hacer help format)
Para calcular el determinante de la matriz anterior H,
>>det(H)
ans = -360

También podría gustarte