0% found this document useful (0 votes)
17 views4 pages

Set A

Uploaded by

shreyamakkar1974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Set A

Uploaded by

shreyamakkar1974
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Q .

1) Perform 10 iterations of Bisection Method


to find roots of the following equation
Cos[x] - x * Exp[x] = 0 in the interval[0, 1].
In[70]:= ClearAll[a, b, c, k, f]
BisectionMethod[a0_ , b0_ , m _] := Module{a = N[a0], b = N[b0]}, c = (a + b)  2;
k = 0;
Whilek < m && (b - a)  2 > 10 ^ (- 8), If[Sign[f[b]] ⩵ Sign[f[c]], b = c, a = c];
c = (a + b)  2;
k = k + 1;;
Print["c=", NumberForm[c, 16]];
Print["f[c]=", NumberForm[f[c], 16]];;
f[x_] = Cos[x] - x * Exp[x]
BisectionMethod[0, 1, 20]
x
-ⅇ x + Cos[x]

"c="0.5177569389343262

"f[c]="1.292135901942437 × 10-6

Q .2 Using Euler Method find the


approximate solution for the initial
value problem y x = x + y, 0 ≤ x ≤ 1, y
0 = 1 and use no.of iteration n = 10.
In[40]:= ClearAll[a, b, n, x, y, i, j];
f[x_ , y _] := x + y;
a = 0;
b = 1;
n = 10;
h = (b - a)  n;
y[0] = 1;
Print"Value of h=", N[h];
Fori = 0, i < n + 1, i ++,
x[i] = N[a + i * h];
y[i + 1] = y[i] + h * f[x[i], y[i]];
Print"For x[", i, "]=", x[i], "value of y[", x[i], "]=", y[i]

"Value of h="0.1`
2

"For x["0"]="0.`"value of y["0.`"]="1

"For x["1"]="0.1`"value of y["0.1`"]="1.1`

"For x["2"]="0.2`"value of y["0.2`"]="1.2200000000000002`

"For x["3"]="0.3`"value of y["0.3`"]="1.362`

"For x["4"]="0.4`"value of y["0.4`"]="1.5282`

"For x["5"]="0.5`"value of y["0.5`"]="1.72102`

"For x["6"]="0.6`"value of y["0.6`"]="1.943122`

"For x["7"]="0.7`"value of y["0.7`"]="2.1974342`

"For x["8"]="0.8`"value of y["0.8`"]="2.48717762`

"For x["9"]="0.9`"value of y["0.9`"]="2.8158953820000003`

"For x["10"]="1.`"value of y["1.`"]="3.1874849202`

Q .3 Find the solution of the equation


f x = Exp- x ^ 2 using Simpson rule.
In[74]:= ClearAll[a, b, n, f];
a = 0;
b = 0.6;
h = 0.1;
n = (b - a)  h;
Print"Value of h=", N[h];
k = Tablea + i * h, i, 1, n;
f[x_] := Exp[- x ^ 2];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 4 * fki;
Fori = 2, i < n, i += 2, sumeven += 2 * fki;
simpIntt = h  3 * f[a] + f[b] + N[sumodd] + N[sumeven];
Print"For", n, "equal parts Simpson's Approximation is :", simpIntt;
intt = Integrate[f[x], {x, a, b}];
Print"True value of Integration from ", a, "to", b, "is:", N[intt];
Print"Absolute error in Approximation is ", Abs[simpIntt - intt];

"Value of h="0.1`

"For"6.`"equal parts Simpson's Approximation is :"0.5351556663558436`

"True value of Integration from "0"to"0.6`"is:"0.5351535268080787`


3

"Absolute error in Approximation is "2.139547764845595`*^-6

Q .4 y 'x = F x, y = - 2 * y + 3, y


x, 0 = y0 by Runge - Kutta Method.
Clear[y]
RK4[a0_ , b0_ , h0_ , f _, y0_] :=
Modulea = a0, b = b0, n, h = h0, xi, k1, k2, k3, k4, n = (b - a)  h;
xi = Tablea + j - 1 * h, j, 1, n + 1;
yi = Table[0, {n + 1}];
yi〚1〛 = y0;
OutputDetails = 0, xi〚1〛, y0;
Fori = 1, i ≤ n, i ++,
k1 = h * fxii, yii;
k2 = h * fxii + h  2, yii + k1  2;
k3 = h * fxii + h  2, yii + k2  2;
k4 = h * fxii + h, yii + k3;
yii + 1 = yii + (k1 + 2 * k2 + 2 * k3 + k4)  6;
OutputDetails = AppendOutputDetails, i, Nxii + 1, Nyii + 1;;
Print
NumberFormTableFormOutputDetails, TableHeadings → None, "i", "xi", "yi", 6;;

In[5]:= f[x_ , y _] = - 2 * y + 3
yi = RK4[0, 1, 0.5, f, 1];

3-2y

"i" "xi" "yi"


0 0. 1
1 0.5 1.3125
2 1. 1.42969

Q .5 To solve the following linear system of


equation using Gauss Seidel Error Format.
5 x1 + x2 + 2 x3 = 10
- 3 x1 + 9 x2 + 4 x3 = - 14
x1 + 2 x2 - 7 x3 = - 33
4

In[119]:=
Clear[A, b, x, n, m, k, i, m1, m2]
A = N[{{5, 1, 2}, {- 3, 9, 4}, {1, 2, - 7}}];
b = {10, - 14, - 33};
x0 = {0, 0, 0};
xk = x0;
m1 = Length[A];
m2 = Length[b];
k = 0;
Ifm1 ≠ m2, Print"Solution not possible", OutputDetails = {xk};
xk1 = Table[0, {m1}];
Whilek ≤ 10,
k ++;
Fori = 1, i ≤ m1, i ++,
xk1i =
1  Ai, i bi - SumAi, j * xk1j, j, 1, i - 1 - SumAi, j * xkj, j, i + 1, m1;;
OutputDetails = Append[OutputDetails, xk1];
xk = xk1;;
colHeading = Tablei, i, 1, m1;
rowHeading = Tablej, j, 0, k;
PrintNumberForm
TableFormOutputDetails,
TableHeadings → rowHeading, colHeading, 6;
Print"Roots after", k, "iterations are", NumberForm[xk1, 9]

1 2 3
0 0 0 0
1 2. -0.888889 4.74603
2 0.279365 -3.57178 3.73369
3 1.22088 -2.80801 4.08641
4 0.927039 -3.06272 3.97166
5 1.02388 -2.97944 4.00929
6 0.992174 -3.00674 3.99696
7 1.00256 -2.99779 4.001
8 0.99916 -3.00072 3.99967
9 1.00028 -2.99976 4.00011
10 0.99991 -3.00008 3.99996
11 1.00003 -2.99997 4.00001

"Roots after"11"iterations are"{1.00002955, -2.99997457, 4.00001149}

You might also like