-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab.m
48 lines (46 loc) · 1.06 KB
/
Lab.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%% Método bisección
a = input('Digite el valor de A: ');
b = input('Digite el valor de B: ');
error = 1;
epsilon =10^-9;
x = -1;
iteraciones=0;
while (error > epsilon)
antx=x;
fa = cos(a)*exp(a);
fb = cos(b)*exp(b);
x = (a+b)/2;
fx = cos(x)*exp(x);
if (fb*fx < 0)
a = x;
else
b = x;
end
error = abs((x-antx)/x);
iteraciones = iteraciones+1;
end
fprintf('Con el metodo de biseccion se obtuvo la raiz: %f\n',x);
fprintf('Se realizaron un total de %d iteraciones\n',iteraciones);
%% Método Newton Raphson
syms h;
f = cos(h)*exp(h);
df = diff(f,h);
a = input('Digite la semilla: ');
error = 1;
epsilon =10^-9;
x = a;
iteraciones=0;
while (error > epsilon)
antx = x;
x=x-subs(f,h,x)/subs(df,h,x);
error = abs((x-antx)/x);
iteraciones = iteraciones+1;
end
fprintf('Con el metodo de biseccion se obtuvo la raiz: %f\n',x);
fprintf('Se realizaron un total de %d iteraciones\n',iteraciones);
x=4:.1:20;
y=cos(x).*exp(x);
hold on;
plot(x,y,'r');
plot(x,0,'b');
hold off;