Skip to content

Commit 6e78302

Browse files
Add files via upload
1 parent b367139 commit 6e78302

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

Intro2Matlab/arrays_demo.mlx

5.17 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function [a, b] = bracket_root_bisection1(f, a, b)
2+
% single step bisection search for bracekting a root
3+
% takes a bracket containing a root as input
4+
% gives a reduced(half) bracket containing root as output
5+
6+
7+
fa = f(a);
8+
fb = f(b);
9+
10+
% Checking for sign, if same sign, no root exists
11+
if fa*fb > 0
12+
error('Root not bracketed in the interval');
13+
end
14+
15+
% else continuing
16+
c = (a+b)/2;
17+
fc = f(c);
18+
19+
if fc == 0
20+
% Found the root exactly
21+
a = c;
22+
b = c;
23+
return;
24+
elseif fa*fc < 0
25+
% Root is in the interval [a,c]
26+
b = c;
27+
fb = fc;
28+
else
29+
% Root is in the interval [c,b]
30+
a = c;
31+
fa = fc;
32+
end
33+
34+
35+
36+
37+
end

Intro2Matlab/final.mlx

3.45 KB
Binary file not shown.

Intro2Matlab/final_m.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
% inputs
2+
a = -3 ; % Left bracket
3+
b = 1 ; % Right bracket
4+
tol = 1e-3 ; % Tolerance
5+
f = @(x) x^3 ; % function
6+
7+
iter = 0;
8+
while abs(a-b)/2 > tol
9+
iter = iter + 1;
10+
[a, b] = bracket_root_bisection1(f, a, b);
11+
12+
13+
if abs(a-b)/2 <= tol
14+
root = (a+b)/2;
15+
end
16+
end

Intro2Matlab/plots_demo.mlx

274 KB
Binary file not shown.

Intro2Matlab/sym_toolbox_demo.mlx

178 KB
Binary file not shown.

Intro2Matlab/test1.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
% inputs
2+
a = -3 ; % Left bracket
3+
b = 1 ; % Right bracket
4+
f = @(x) x^3 ; % function
5+
6+
7+
8+
fa = f(a);
9+
fb = f(b);
10+
11+
% Checking for sign, if same sign, no root exists
12+
if fa*fb > 0
13+
error('Root not bracketed in the interval');
14+
end
15+
16+
% else continuing
17+
c = (a+b)/2;
18+
fc = f(c);
19+
20+
if fc == 0
21+
% Found the root exactly
22+
a = c;
23+
b = c;
24+
return;
25+
elseif fa*fc < 0
26+
% Root is in the interval [a,c]
27+
b = c;
28+
fb = fc;
29+
else
30+
% Root is in the interval [c,b]
31+
a = c;
32+
fa = fc;
33+
end
34+
35+
36+

Intro2Matlab/test2.mlx

34.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)