1) Codes Numerical Methods

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PRACTICAL 1  SOLVE THE FOLLOWING

EQUATION USING BISECTION METHOD


Q 1 x^3 + x^2 - 3 x - 3

In[1]:= bisection[f_, a0_, b0_, n_] := Module{}, a = N[a0]; b = N[b0];

Iff[a] * f[b] > 0, Print"Bisection Method cannot be applied" ×

Return[];
p = (a + b) / 2;
i = 1;
Whilei ≤ n,
If[f[a] * f[p] < 0, b = p, a = p];
Printi, " ", a, " ", b;
i ++;
p = (a + b) / 2;

Print"Root= ", p

x^3 + x^2 - 3 x - 3;
f[x_] :=
Plot[f[x], {x, - 3, 3}]

-3 -2 -1 1 2 3
Out[3]=

-5

-10

In[4]:= bisection[f, 1, 2, 12]


2

1 1.5 2.

2 1.5 1.75

3 1.625 1.75

4 1.6875 1.75

5 1.71875 1.75

6 1.71875 1.73438

7 1.72656 1.73438

8 1.73047 1.73438

9 1.73047 1.73242

10 1.73145 1.73242

11 1.73193 1.73242

12 1.73193 1.73218

Root= 1.73206

Q 2 x ^ 3 - x - 1
3

In[5]:= bisection[f_, a0_, b0_, n_] := Module{}, a = N[a0]; b = N[b0];

Iff[a] * f[b] > 0, Print"Bisection Method cannot be applied" ×

Return[];
p = (a + b) / 2;
i = 1;
Whilei ≤ n,
If[f[a] * f[p] < 0, b = p, a = p];
Printi, " ", a, " ", b;
i ++;
p = (a + b) / 2;

Print"Root= ", p


x^3 - x - 1;
f[x_] :=
Plot[f[x], {x, - 3, 3}]

20

10

Out[7]=
-3 -2 -1 1 2 3

-10

-20

In[8]:= bisection[f, 1, 2, 10]


1 1. 1.5

2 1.25 1.5

3 1.25 1.375

4 1.3125 1.375

5 1.3125 1.34375

6 1.3125 1.32813

7 1.32031 1.32813

8 1.32422 1.32813

9 1.32422 1.32617

10 1.32422 1.3252

Root= 1.32471
4

You might also like