05.09.2007, 18:52 |
tigerbine |
Auf diesen Beitrag antworten » |
Division durch 0 [Matlab]
Wie behandelt matlab eigentlich die Division durch 0? Drauf gestoßen bin ich gerade beim Bisektionsverfahren für die Funktion f(x)=1/x und dem Intervall [a,b] = [-2,2].
Man fragt ja nun hier nicht den konkreten Funktionswert der Intervallmitte m ab, sondern ob dieser "=" 0 ist, und welches "Vorzeichen" f(a)f(m) < 0 ist.
Hier mal der Ausdruck. Es wird gewarnt, aber dennoch weitergerechnet.
LG,
tigerbine
code: |
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:
|
%Bisektion
a=input('a=');
b=input('b=');
n=input('n=');
for i=1:n
m=(a+b)/2;
fm=1/m;
if fm==0
x=m;
break
end
fa=1/a;
if fa*fm < 0
b=m;
else
a=m;
end
disp([i,a,b]);
end
|
|
code: |
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:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
|
[B]Warning: Divide by zero.[/B]
> In C:\matlabR12\work\bi.m at line 10
1 -2 0
2 -1 0
3.00000000000000 -0.50000000000000 0
4.00000000000000 -0.25000000000000 0
5.00000000000000 -0.12500000000000 0
6.00000000000000 -0.06250000000000 0
7.00000000000000 -0.03125000000000 0
8.00000000000000 -0.01562500000000 0
9.00000000000000 -0.00781250000000 0
10.00000000000000 -0.00390625000000 0
11.00000000000000 -0.00195312500000 0
12.00000000000000 -0.00097656250000 0
13.00000000000000 -0.00048828125000 0
14.00000000000000 -0.00024414062500 0
15.00000000000000 -0.00012207031250 0
16.00000000000000 -0.00006103515625 0
17.00000000000000 -0.00003051757813 0
18.00000000000000 -0.00001525878906 0
19.00000000000000 -0.00000762939453 0
20.00000000000000 -0.00000381469727 0
21.00000000000000 -0.00000190734863 0
22.00000000000000 -0.00000095367432 0
23.00000000000000 -0.00000047683716 0
24.00000000000000 -0.00000023841858 0
25.00000000000000 -0.00000011920929 0
26.00000000000000 -0.00000005960464 0
27.00000000000000 -0.00000002980232 0
28.00000000000000 -0.00000001490116 0
29.00000000000000 -0.00000000745058 0
30.00000000000000 -0.00000000372529 0
31.00000000000000 -0.00000000186265 0
32.00000000000000 -0.00000000093132 0
33.00000000000000 -0.00000000046566 0
34.00000000000000 -0.00000000023283 0
35.00000000000000 -0.00000000011642 0
36.00000000000000 -0.00000000005821 0
37.00000000000000 -0.00000000002910 0
38.00000000000000 -0.00000000001455 0
39.00000000000000 -0.00000000000728 0
40.00000000000000 -0.00000000000364 0
41.00000000000000 -0.00000000000182 0
42.00000000000000 -0.00000000000091 0
43.00000000000000 -0.00000000000045 0
44.00000000000000 -0.00000000000023 0
45.00000000000000 -0.00000000000011 0
46.00000000000000 -0.00000000000006 0
47.00000000000000 -0.00000000000003 0
48.00000000000000 -0.00000000000001 0
49.00000000000000 -0.00000000000001 0
50.00000000000000 -0.00000000000000 0
51.00000000000000 -0.00000000000000 0
|
|
|
05.09.2007, 18:56 |
Dual Space |
Auf diesen Beitrag antworten » |
RE: Division durch 0 [Matlab]
Bei Division durch 0 bricht Matlab den Durchlauf ab und gibt eine Fehlermeldung.
Weiterarbeiten sollte das Prog eigentlich nicht.
|
05.09.2007, 18:59 |
tigerbine |
Auf diesen Beitrag antworten » |
RE: Division durch 0 [Matlab]
^^ der Ausruck ist aber kein fake |
05.09.2007, 20:54 |
tigerbine |
Auf diesen Beitrag antworten » |
RE: Division durch 0 [Matlab]
Vielleicht liegt hierin die Antwort.
code: |
1:
2:
3:
4:
5:
6:
|
>> 1/0
Warning: Divide by zero.
ans =
Inf |
|
code: |
1:
2:
3:
4:
5:
6:
7:
|
>> -1/0
Warning: Divide by zero.
ans =
-Inf |
|
Da es bei dem Algo nur auf das Vorzeichen ankommt, kommt es trotz Polstelle nicht zum Abbruch. Inf und -Inf sind Variablen.
code: |
1:
2:
3:
4:
5:
6:
|
>> 2*Inf
ans =
Inf |
|
code: |
1:
2:
3:
4:
5:
6:
7:
|
>> -2*Inf
ans =
-Inf
|
|
|