Sei sulla pagina 1di 3

1 Genere un cdigo en Matlab para el mtodo de Newton-Raphson y de la

secante.
2 Utilice los cdigos anteriores para encontrar la raz real ms grande de
f ( x )=0.95 x 35.9 x 2+10.9 x6
a En forma grfica.
b Mtodo de Newton- Raphson (tres iteraciones) con x l=3.5.
c Mtodo de la secante (3 iteraciones) con xi-1=2.5 y xi=3.5.

1) Mtodo Newton-Raphson (Cdigo).


clc;
clear all;
close all;
syms x
d=input('enter f(x)= \n') %introduzca la funcin
f=inline(d);
z=diff(f(x)); %calcula la derivada
f1=inline(z);
x0=input('enter x0= \n') %introduzca el valor inicial para x0
x=x0
for u=0:inf
y=x
x=y-(f(x)/f1(x));
if x==y
break
end
end
u
Mtodo de la secante (cdigo).
clc;
clear all;
close all;
f=input('ingrese la funcion: \n','s');;
f=inline(f);
x0=input('ingrese el primer punto inicial: \n');;
x1=input('ingrese el segundo punto inicial: \n');
tol=input('ingrese el error\n');
xra=0;
xr=0;
i=0;
error_aprox=100;
error=0;
f1=f(x1);
f2=x0-x1;
f3=f(x0);
f4=f(x1);

xr=x1-((f1*f2)/(f3-f4));
i=i+1;
fprintf('it.
x0
x1
xr error aprox\n');
fprintf('%2d \t %11.7f \t %11.7f %11.7f %11.7f \n',1,x0,x1,xr,error);
while error_aprox>tol,
xra=xr;
x1=xr;
f1=f(x1);
f2=x0-x1;
f3=f(x0);
f4=f(x1);
xr=x1-((f1*f2)/(f3-f4));
error=abs((xr-xra)/ xr)*100;
error_aprox=error;
fprintf('%2d \t %11.7f \t %11.7f %11.7f %11.7f \n',1,x0,x1,xr,error_aprox);
i=i+1;
if i>400
disp('no se alcanzo la convergencia')
break
end
end
Resultados
2)
a) Forma grfica.

b)
Mtodo Newton-Raphson con tres iteraciones y xi=3.5
enter f(x)= 0.95*x^3-5.9*x^2+10.9*x-6

d = (19*x^3)/20 - (59*x^2)/10 + (109*x)/10 6


enter x0= 3.5
x0 = 3.5000
x = 3.5000
Warning: FOR loop index is too large. Truncating to
9223372036854775807.
> In newton at 11
y = 3.5000
y = 3.3657
y = 3.3451
y = 3.3446
c)
Mtodo de la secante con tres iteraciones con x i-1=2.5 y x1=3.5.
ingrese la funcin:
0.95*x^3-5.9*x^2+10.9*x-6
ingrese el primer punto inicial:
3.5
ingrese el segundo punto inicial:
2.5
ingrese el error
1
it.
x0
x1
xr
error aprox
1
3.5000000 2.5000000 3.0630631 0.0000000
1
3.5000000 3.0630631 3.2919062 6.9516917
2
3.5000000 3.2919062 3.3363987 1.3335484
3
3.5000000 3.3363987 3.3433969 0.2093136
>>

Potrebbero piacerti anche