summaryrefslogtreecommitdiff
path: root/845/CH2
diff options
context:
space:
mode:
Diffstat (limited to '845/CH2')
-rwxr-xr-x845/CH2/EX2.1/Ex2_1.sce56
-rwxr-xr-x845/CH2/EX2.10/Ex2_10.sce19
-rwxr-xr-x845/CH2/EX2.11/Ex2_11.sce2
-rwxr-xr-x845/CH2/EX2.12/Ex2_12.sce19
-rwxr-xr-x845/CH2/EX2.13/Ex2_13.sce12
-rwxr-xr-x845/CH2/EX2.2/Ex2_2.sce59
-rwxr-xr-x845/CH2/EX2.3/Ex2_3.sce56
-rwxr-xr-x845/CH2/EX2.4/Ex2_4.sce57
-rwxr-xr-x845/CH2/EX2.5/Ex2_5.sce24
-rwxr-xr-x845/CH2/EX2.6/Ex2_6.sce23
-rwxr-xr-x845/CH2/EX2.7/Ex2_7.sce24
-rwxr-xr-x845/CH2/EX2.8/Ex2_8.sce24
-rwxr-xr-x845/CH2/EX2.9/Ex2_9.sce2
13 files changed, 377 insertions, 0 deletions
diff --git a/845/CH2/EX2.1/Ex2_1.sce b/845/CH2/EX2.1/Ex2_1.sce
new file mode 100755
index 000000000..500b7c858
--- /dev/null
+++ b/845/CH2/EX2.1/Ex2_1.sce
@@ -0,0 +1,56 @@
+//Example 2.1
+clc
+clear
+
+function [root] = Bisection(fun,x,tol,maxit)
+// Bisection: Computes roots of the function in the given range using Bisection Method
+//// Input: Bisection(fun,x,tol,maxit)
+// fun = function handle
+// x = range in between sign change is evident
+// tol = Maximum error between iterations that can be tolerated
+// maxit = Maximum number of iterations
+//// Output: [root]
+// Root: Root of the given function in defined range
+
+if fun(x(1)) > 0 then
+ xu = x(1); xl = x(2);
+else
+ xu = x(2); xl = x(1);
+end
+
+Ea = 1;
+iter = 1;
+
+while(1)
+ xr(iter) = (xl(iter) + xu(iter)) / 2;
+ if fun(xr(iter)) > 0 then
+ xu(iter+1) = xr(iter);
+ xl(iter+1) = xl(iter);
+ elseif fun(xr(iter)) < 0 then
+ xl(iter+1) = xr(iter);
+ xu(iter+1) = xu(iter);
+ else
+ break
+ end
+
+ if iter>1 then
+ Ea(iter) = 100 * abs((xr(iter) - xr(iter-1)) / xr(iter));
+ end
+
+ if Ea(iter) < tol | iter == maxit then
+ break
+ end
+ iter = iter + 1;
+end
+root = xr(iter);
+endfunction
+
+function f = fun1(x)
+ f = x.^3 -9*x + 1;
+endfunction
+
+x = [2 4];
+tol = 1e-4;
+maxit = 5;
+root = Bisection(fun1,x,tol,maxit);
+disp(root,"root = ")
diff --git a/845/CH2/EX2.10/Ex2_10.sce b/845/CH2/EX2.10/Ex2_10.sce
new file mode 100755
index 000000000..c1a3ffacf
--- /dev/null
+++ b/845/CH2/EX2.10/Ex2_10.sce
@@ -0,0 +1,19 @@
+//Example 2.10
+clc
+clear
+
+N = 12;
+xold = 3.5;
+iter = 1;
+maxit = 3;
+
+while (1)
+ xnew = (xold + N/xold) / 2;
+ if iter == maxit then
+ break
+ end
+ xold = xnew;
+ iter = iter + 1;
+end
+root = round(xnew*10^4) / 10^4;
+disp(root,"root = ")
diff --git a/845/CH2/EX2.11/Ex2_11.sce b/845/CH2/EX2.11/Ex2_11.sce
new file mode 100755
index 000000000..3169eee69
--- /dev/null
+++ b/845/CH2/EX2.11/Ex2_11.sce
@@ -0,0 +1,2 @@
+// Example 2.11
+// This is an analytical problem and need not be coded.
diff --git a/845/CH2/EX2.12/Ex2_12.sce b/845/CH2/EX2.12/Ex2_12.sce
new file mode 100755
index 000000000..7ff416a86
--- /dev/null
+++ b/845/CH2/EX2.12/Ex2_12.sce
@@ -0,0 +1,19 @@
+//Example 2.12
+clc
+clear
+
+function [f] = fun12(x)
+ f = x.^3 - x - 1;
+endfunction
+
+x = [0 1 2];
+h = [x(2)-x(1) x(3)-x(2)];
+lamdai = h(2)/h(1);
+deli = 1 + lamdai;
+f = fun12(x);
+
+g = f(1)*lamdai^2 - f(2)*deli^2 + f(3)*(lamdai + deli);
+lamda = -2*f(3)*deli / (g + sqrt(g^2 - 4*f(3)*deli*(f(1)*lamdai - f(2)*deli + f(3))));
+xnew = x(3) + lamda*h(2);
+xnew = round(xnew*10^5) / 10^5;
+disp(xnew,"root = ")
diff --git a/845/CH2/EX2.13/Ex2_13.sce b/845/CH2/EX2.13/Ex2_13.sce
new file mode 100755
index 000000000..eb4c6330f
--- /dev/null
+++ b/845/CH2/EX2.13/Ex2_13.sce
@@ -0,0 +1,12 @@
+//Example 2.13
+clc
+clear
+
+a = [-6 11 -6 1];
+maxit = 3;
+for iter = 1:maxit
+ a = [a(4)^2 -(a(3)^2 -2*a(2)*a(4)) (a(2)^2 - 2*a(1)*a(3)) -a(1)^2];
+ root = abs([a(4)/a(3) a(3)/a(2) a(2)/a(1)])^(1/(2^iter));
+end
+root = round(root*10^5) / 10^5;
+disp(root,"Estimated roots for the polynomial are: ")
diff --git a/845/CH2/EX2.2/Ex2_2.sce b/845/CH2/EX2.2/Ex2_2.sce
new file mode 100755
index 000000000..b22ce56bd
--- /dev/null
+++ b/845/CH2/EX2.2/Ex2_2.sce
@@ -0,0 +1,59 @@
+//Example 2.2
+clc
+clear
+
+function [root] = FalsePosition(fun,x,tol,maxit)
+// FalsePosition: Computes roots of the function in the given range using False Position Method
+//// Input: FalsePosition(fun,x,tol,maxit)
+// fun = function handle
+// x = range in between sign change is evident
+// tol = Maximum error between iterations that can be tolerated
+// maxit = Maximum number of iterations
+//// Output: [root]
+// Root: Root of the given function in defined range
+
+if fun(x(1)) > 0 then
+ xu = x(1); xl = x(2);
+else
+ xu = x(2); xl = x(1);
+end
+
+Ea = 1;
+iter = 1;
+
+while(1)
+ xr(iter) = xl(iter) - ((xu(iter)-xl(iter)) / (fun(xu(iter))-fun(xl(iter))) * fun(xl(iter)));
+ if fun(xr(iter)) > 0 then
+ xu(iter+1) = xr(iter);
+ xl(iter+1) = xl(iter);
+ elseif fun(xr(iter)) < 0 then
+ xl(iter+1) = xr(iter);
+ xu(iter+1) = xu(iter);
+ else
+ break
+ end
+
+ if iter>1 then
+ Ea(iter) = 100 * abs((xr(iter) - xr(iter-1)) / xr(iter));
+ end
+
+ if Ea(iter) < tol | iter == maxit then
+ break
+ end
+ iter = iter + 1;
+end
+root = xr(iter);
+endfunction
+
+function f = fun1(x)
+ f = x.^3 -9*x + 1;
+endfunction
+
+x = [2 4; 2 3];
+tol = 1e-4;
+maxit = 3;
+for i = 1:2
+ root = FalsePosition(fun1,x(i,:),tol,maxit);
+ root = round(root*10^5)/10^5;
+ disp(strcat(["root(",string(i),") = ",string(root)]))
+end
diff --git a/845/CH2/EX2.3/Ex2_3.sce b/845/CH2/EX2.3/Ex2_3.sce
new file mode 100755
index 000000000..3dd1168db
--- /dev/null
+++ b/845/CH2/EX2.3/Ex2_3.sce
@@ -0,0 +1,56 @@
+//Example 2.3
+clc
+clear
+
+function [root] = FalsePosition(fun,x,tol,maxit)
+// FalsePosition: Computes roots of the function in the given range using False Position Method
+//// Input: FalsePosition(fun,x,tol,maxit)
+// fun = function handle
+// x = range in between sign change is evident
+// tol = Maximum error between iterations that can be tolerated
+// maxit = Maximum number of iterations
+//// Output: [root]
+// Root: Root of the given function in defined range
+
+if fun(x(1)) > 0 then
+ xu = x(1); xl = x(2);
+else
+ xu = x(2); xl = x(1);
+end
+
+Ea = 1;
+iter = 1;
+
+while(1)
+ xr(iter) = xl(iter) - ((xu(iter)-xl(iter)) / (fun(xu(iter))-fun(xl(iter))) * fun(xl(iter)));
+ if fun(xr(iter)) > 0 then
+ xu(iter+1) = xr(iter);
+ xl(iter+1) = xl(iter);
+ elseif fun(xr(iter)) < 0 then
+ xl(iter+1) = xr(iter);
+ xu(iter+1) = xu(iter);
+ else
+ break
+ end
+
+ if iter>1 then
+ Ea(iter) = 100 * abs((xr(iter) - xr(iter-1)) / xr(iter));
+ end
+
+ if Ea(iter) < tol | iter == maxit then
+ break
+ end
+ iter = iter + 1;
+end
+root = xr(iter);
+endfunction
+
+function f = fun3(x)
+ f = log(x) - cos(x);
+endfunction
+
+x = [1 2];
+tol = 1e-4;
+maxit = 5;
+root = FalsePosition(fun3,x,tol,maxit);
+disp(round(root*10^4)/10^4,"root = ")
diff --git a/845/CH2/EX2.4/Ex2_4.sce b/845/CH2/EX2.4/Ex2_4.sce
new file mode 100755
index 000000000..da626dcae
--- /dev/null
+++ b/845/CH2/EX2.4/Ex2_4.sce
@@ -0,0 +1,57 @@
+//Example 2.4
+clc
+clear
+
+function [root] = FalsePosition(fun,x,tol,maxit)
+// FalsePosition: Computes roots of the function in the given range using False Position Method
+//// Input: FalsePosition(fun,x,tol,maxit)
+// fun = function handle
+// x = range in between sign change is evident
+// tol = Maximum error between iterations that can be tolerated
+// maxit = Maximum number of iterations
+//// Output: [root]
+// Root: Root of the given function in defined range
+
+if fun(x(1)) > 0 then
+ xu = x(1); xl = x(2);
+else
+ xu = x(2); xl = x(1);
+end
+
+Ea = 1;
+iter = 1;
+
+while(1)
+ xr(iter) = xl(iter) - ((xu(iter)-xl(iter)) / (fun(xu(iter))-fun(xl(iter))) * fun(xl(iter)));
+ if fun(xr(iter)) > 0 then
+ xu(iter+1) = xr(iter);
+ xl(iter+1) = xl(iter);
+ elseif fun(xr(iter)) < 0 then
+ xl(iter+1) = xr(iter);
+ xu(iter+1) = xu(iter);
+ else
+ break
+ end
+
+ if iter>1 then
+ Ea(iter) = 100 * abs((xr(iter) - xr(iter-1)) / xr(iter));
+ end
+
+ if Ea(iter) < tol | iter == maxit then
+ break
+ end
+ iter = iter + 1;
+end
+root = xr(iter);
+endfunction
+
+function f = fun4(x)
+ f = x.*log10(x) - 1.2;
+endfunction
+
+clc
+x = [2 3];
+tol = 1e-4;
+maxit = 2;
+root = FalsePosition(fun4,x,tol,maxit);
+disp(round(root*10^4)/10^4,"root = ")
diff --git a/845/CH2/EX2.5/Ex2_5.sce b/845/CH2/EX2.5/Ex2_5.sce
new file mode 100755
index 000000000..4f19233f2
--- /dev/null
+++ b/845/CH2/EX2.5/Ex2_5.sce
@@ -0,0 +1,24 @@
+//Example 2.5
+clc
+clear
+
+function f = fun5(x)
+ f = exp(-x)/10;
+endfunction
+
+clc
+tol = 1e-4;
+maxit = 4;
+xold = 0;
+iter = 1;
+while(1)
+ xnew = fun5(xold);
+ EA = abs(xnew - xold);
+ if EA < tol | iter > maxit then
+ break
+ end
+ xold = xnew;
+ iter = iter + 1;
+end
+root = round(xnew*10^4) / 10^4; //rounded to 4 decimal places
+disp(root,"root = ")
diff --git a/845/CH2/EX2.6/Ex2_6.sce b/845/CH2/EX2.6/Ex2_6.sce
new file mode 100755
index 000000000..e6b8f27e3
--- /dev/null
+++ b/845/CH2/EX2.6/Ex2_6.sce
@@ -0,0 +1,23 @@
+//Example 2.6
+clc
+clear
+
+function f = fun6(x)
+ f = 1./ sqrt(x+1);
+endfunction
+
+tol = 1e-4;
+maxit = 6;
+xold = 1;
+iter = 1;
+while(1)
+ xnew = fun6(xold);
+ EA = abs(xnew - xold);
+ if EA < tol | iter > maxit then
+ break
+ end
+ xold = xnew;
+ iter = iter + 1;
+end
+root = round(xnew*10^4) / 10^4; //rounded to 4 decimal places
+disp(root,"root = ")
diff --git a/845/CH2/EX2.7/Ex2_7.sce b/845/CH2/EX2.7/Ex2_7.sce
new file mode 100755
index 000000000..43631cc12
--- /dev/null
+++ b/845/CH2/EX2.7/Ex2_7.sce
@@ -0,0 +1,24 @@
+//Example 2.7
+clc
+clear
+
+function [f,df] = fun7(x)
+ f = x.*exp(x) - 2;
+ df = x.*exp(x) + exp(x);
+endfunction
+
+xold = 1;
+maxit = 2;
+iter = 1;
+
+while (1)
+ [fx,dfx] = fun7(xold);
+ xnew = xold - fx/dfx;
+ if iter == maxit then
+ break
+ end
+ xold = xnew;
+ iter = iter + 1;
+end
+root = round(xnew*10^3) / 10^3;
+disp(root,"root = ")
diff --git a/845/CH2/EX2.8/Ex2_8.sce b/845/CH2/EX2.8/Ex2_8.sce
new file mode 100755
index 000000000..5ef7cea63
--- /dev/null
+++ b/845/CH2/EX2.8/Ex2_8.sce
@@ -0,0 +1,24 @@
+//Example 2.8
+clc
+clear
+
+function [f,df] = fun8(x)
+ f = x.^3 - x - 1;
+ df = 3*x.^2 - 1;
+endfunction
+
+xold = 1;
+maxit = 5;
+iter = 1;
+
+while (1)
+ [fx,dfx] = fun8(xold);
+ xnew = xold - fx/dfx;
+ if iter == maxit then
+ break
+ end
+ xold = xnew;
+ iter = iter + 1;
+end
+root = round(xnew*10^4) / 10^4;
+disp(root,"root = ")
diff --git a/845/CH2/EX2.9/Ex2_9.sce b/845/CH2/EX2.9/Ex2_9.sce
new file mode 100755
index 000000000..4e319d990
--- /dev/null
+++ b/845/CH2/EX2.9/Ex2_9.sce
@@ -0,0 +1,2 @@
+// Example 2.9
+// This is an analytical problem and need not be coded.