From 7f60ea012dd2524dae921a2a35adbf7ef21f2bb6 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Tue, 10 Oct 2017 12:27:19 +0530 Subject: initial commit / add all books --- 260/CH4/EX4.1/4_1.sce | 70 +++++++++++++++++++++++++++++++++++++++++++ 260/CH4/EX4.10/4_10.sce | 29 ++++++++++++++++++ 260/CH4/EX4.11/4_11.sce | 31 +++++++++++++++++++ 260/CH4/EX4.12/4_12.sce | 28 ++++++++++++++++++ 260/CH4/EX4.13/4_13.sce | 25 ++++++++++++++++ 260/CH4/EX4.14/4_14.sce | 23 ++++++++++++++ 260/CH4/EX4.15/4_15.sce | 19 ++++++++++++ 260/CH4/EX4.16/4_16.sce | 21 +++++++++++++ 260/CH4/EX4.17/4_17.sce | 32 ++++++++++++++++++++ 260/CH4/EX4.18/4_18.sce | 32 ++++++++++++++++++++ 260/CH4/EX4.19/4_19.sce | 28 ++++++++++++++++++ 260/CH4/EX4.2/4_2.sce | 23 ++++++++++++++ 260/CH4/EX4.20/4_20.sce | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 260/CH4/EX4.21/4_21.sce | 28 ++++++++++++++++++ 260/CH4/EX4.3/4_3.sce | 25 ++++++++++++++++ 260/CH4/EX4.4/4_4.sce | 32 ++++++++++++++++++++ 260/CH4/EX4.5/4_5.sce | 40 +++++++++++++++++++++++++ 260/CH4/EX4.6/4_6.sce | 52 ++++++++++++++++++++++++++++++++ 260/CH4/EX4.7/4_7.sce | 37 +++++++++++++++++++++++ 260/CH4/EX4.8/4_8.sce | 23 ++++++++++++++ 260/CH4/EX4.9/4_9.sce | 32 ++++++++++++++++++++ 21 files changed, 709 insertions(+) create mode 100644 260/CH4/EX4.1/4_1.sce create mode 100644 260/CH4/EX4.10/4_10.sce create mode 100644 260/CH4/EX4.11/4_11.sce create mode 100644 260/CH4/EX4.12/4_12.sce create mode 100644 260/CH4/EX4.13/4_13.sce create mode 100644 260/CH4/EX4.14/4_14.sce create mode 100644 260/CH4/EX4.15/4_15.sce create mode 100644 260/CH4/EX4.16/4_16.sce create mode 100644 260/CH4/EX4.17/4_17.sce create mode 100644 260/CH4/EX4.18/4_18.sce create mode 100644 260/CH4/EX4.19/4_19.sce create mode 100644 260/CH4/EX4.2/4_2.sce create mode 100644 260/CH4/EX4.20/4_20.sce create mode 100644 260/CH4/EX4.21/4_21.sce create mode 100644 260/CH4/EX4.3/4_3.sce create mode 100644 260/CH4/EX4.4/4_4.sce create mode 100644 260/CH4/EX4.5/4_5.sce create mode 100644 260/CH4/EX4.6/4_6.sce create mode 100644 260/CH4/EX4.7/4_7.sce create mode 100644 260/CH4/EX4.8/4_8.sce create mode 100644 260/CH4/EX4.9/4_9.sce (limited to '260/CH4') diff --git a/260/CH4/EX4.1/4_1.sce b/260/CH4/EX4.1/4_1.sce new file mode 100644 index 000000000..21dd2091e --- /dev/null +++ b/260/CH4/EX4.1/4_1.sce @@ -0,0 +1,70 @@ +//Eg-4.1 +//pg-139 + +clear +clc + +A=[-3 0 1]; + xl=1; + xu=3; + fx=poly(A,'x','c') + +//This is used determine true root with less errors using bisection method +//input arguments--xl(lower bound of x),xu(upper bound of x),es(stopping criterion error),f(given function) +//output arguments--root(final value of x) obtained from my code,pre-percent relative error,itns-number of itertions used in the code + +xrold=(xl+xu)/2; + +//Verying whether in given interval,function are applicable for bisecion method + +if horner(fx,xl)*horner(fx,xu)>0 then + disp("there are more than 2 roots or no root ,so bisection method is not applicable") +elseif horner(fx,xl)*horner(fx,xu)==0 then + disp("there is no need to apply any method since one of bound is a root") +elseif horner(fx,xl)*horner(fx,xrold)==0|horner(fx,xrold)*horner(fx,xu)==0 then + disp("(xl+xu)/2 is the required root") + +else + //BISECTION METHOD + disp("The given function has only one root in the given interval ,so by applying bisection method root can be determined") + test=horner(fx,xl)*horner(fx,xrold); + if test<0 then + xu=xrold; + elseif test>0 then + xl=xrold; + else + root=xrold; + end + xrnew=(xl+xu)/2; + ea=abs(xrnew-xrold)*100/abs(xrnew); + pre(1,1)=ea; + iter=1; + itns(1,1)=1; + i=1; + while i<=10 + printf('\n\nIteration No. %i \n',i); + printf('xlow=%f \n',xl); + printf('xhigh=%f \n',xu); + xrold=xrnew; + test=horner(fx,xl)*horner(fx,xrold); + if test<0 then + xu=xrold; + elseif test>0 then + xl=xrold; + else + root=xrold; + +end + xrnew=(xl+xu)/2; + fnew=horner(fx,xrnew); + ea=abs(xrnew-xrold)*100/abs(xrnew); + iter=iter+1; + itns(iter,1)=iter; + pre(iter,1)=ea; + i=i+1; + printf('xnew=%f \n',xrnew); + printf('fnew=%f \n',fnew); +end +root=xrnew;//True root +end + diff --git a/260/CH4/EX4.10/4_10.sce b/260/CH4/EX4.10/4_10.sce new file mode 100644 index 000000000..255b61639 --- /dev/null +++ b/260/CH4/EX4.10/4_10.sce @@ -0,0 +1,29 @@ +//Eg-4.10 +//pg-161 + +clear +clc + + +// Secant Method + +A=[-6 5 -3 2]; +x1=0.5; +x2=0.7; +eps=10^(-10); +fx=poly(A,'x','c'); +iter=1; +Abserr=100; +while Abserr>eps + printf('iteration number %i\n',iter); + xnew1=x2-horner(fx,x2)*(x2-x1)/(horner(fx,x2)-horner(fx,x1)); + printf('xnew1 = %f \n',xnew1); + Abserr = abs(horner(fx,xnew1) - horner(fx,x1))/abs(horner(fx,xnew1)); + x1=x2; + x2=xnew1; + iter=iter+1; +end + +disp("result was found in iterations") +disp(iter-1) + diff --git a/260/CH4/EX4.11/4_11.sce b/260/CH4/EX4.11/4_11.sce new file mode 100644 index 000000000..740616998 --- /dev/null +++ b/260/CH4/EX4.11/4_11.sce @@ -0,0 +1,31 @@ +//Eg-4.11 +//pg-163 + +clear +clc + +// Secant Method + +clear ; +close ; +clc ; + +deff('[z]=f(x)','z=1.55*x^(-0.5)-7.2*x+8.1*x^2-4*x^3-1.3'); +iter=1; +eps=10^(-10); +x1=0.5; +x2=1; +imax=20; + +Abserr=100; +while Abserr>eps&iter %f\n so n = %d',t,t+1) + + \ No newline at end of file diff --git a/260/CH4/EX4.4/4_4.sce b/260/CH4/EX4.4/4_4.sce new file mode 100644 index 000000000..cf59e091f --- /dev/null +++ b/260/CH4/EX4.4/4_4.sce @@ -0,0 +1,32 @@ +//Eg-4.4 +//pg-147 + +clear +clc + + +//False Position Method + +clear ; +close ; +clc ; +//Coefficients of polynomial in increasing order of power of x +A = [-2 1 -2 1]; +x1 = 1 ; +x2 = 3 ; +fx = poly(A,'x','c'); +for i = 1:20 + printf('\n\nIteration No. %i \n',i); + fx1 = horner(fx,x1); + fx2 = horner(fx,x2); + x0 = x1 - fx1*(x2-x1)/(fx2-fx1) + printf('xnew = %f \n',x0); + fx0 = horner(fx,x0); + if fx1*fx0 < 0 then + x2 = x0 ; + else + x1 = x0 ; + end +end + +printf('\n\nPlease note that the author has considered only 5 decimal places, but here we have taken 6 decimal places, so a minor difference in the answer may occur') \ No newline at end of file diff --git a/260/CH4/EX4.5/4_5.sce b/260/CH4/EX4.5/4_5.sce new file mode 100644 index 000000000..6703a8b09 --- /dev/null +++ b/260/CH4/EX4.5/4_5.sce @@ -0,0 +1,40 @@ +//Eg-4.5 +//pg-149 + +clear +clc + + +//False Position Method + +clear ; +close ; +clc ; +//Coefficients of polynomial in increasing order of power of x +A = [-3.1622777 -1.2649111 -0.1264911 0 0 100]; +x1 = 0 ; +x2 = 2 ; +fx = poly(A,'x','c'); +printf('\n\nThe given equation can be modified and written in the following form after substituting the values of given constants\n') +disp(fx) +i = 0; +eps = 1; + +while(eps > 10^(-6)) + i = i+1; + //printf('\n\nIteration No. %i \n',i); + fx1 = horner(fx,x1); + fx2 = horner(fx,x2); + xnew = (x1*fx2 - x2*fx1)/(fx2-fx1); + fxnew = horner(fx,xnew); + //printf('xnew = %f \nfxnew = %f',xnew,fxnew); + + if fx1*fxnew < 0 then + x2 = xnew ; + else + x1 = xnew ; + end + eps = abs(fxnew); +end + +printf('\n\nThe result obtained after %d iterations is x = %f\n',i,xnew) \ No newline at end of file diff --git a/260/CH4/EX4.6/4_6.sce b/260/CH4/EX4.6/4_6.sce new file mode 100644 index 000000000..1695ccb55 --- /dev/null +++ b/260/CH4/EX4.6/4_6.sce @@ -0,0 +1,52 @@ +//Eg-4.6 +//pg-151 + +clear +clc + +// Method of Sucessive substitution + +clear ; +close ; +clc ; +//Coefficients of polynomial in increasing order of power of x +A = [0 0 1]; + +//when G(x)=x^2 +x01 = 0.5; +x02 = 1.5; +t(1) = x01; +fx = poly(A,'x','c'); +printf('\nFor G(x) = x^2\n\n') +printf(' x0 itr xnew\n') +for(i = 1:5) + xnew(i) = horner(fx,t(i)) + t(i+1) = xnew(i); + printf('%f %d %f\n',x01,i,xnew(i)) +end +p(1) = x02; +for(i = 1:5) + xnew(i) = horner(fx,p(i)) + p(i+1) = xnew(i); + printf('%f %d %f\n',x02,i,xnew(i)) +end + + +//when g(x)=x^1/2 + +deff('z=f(x)','z=x^(1/2)'); +printf('\nFor G(x) = x^0.5\n\n') +printf(' x0 itr xnew\n') +for i=1:5 + xnew(i) = feval(t(i),f); + t(i+1) = xnew(i); + printf('%f %d %f\n',x01,i,xnew(i)) +end +for i=1:5 + xnew(i) = feval(p(i),f); + p(i+1) = xnew(i); + printf('%f %d %f\n',x02,i,xnew(i)) +end + + + diff --git a/260/CH4/EX4.7/4_7.sce b/260/CH4/EX4.7/4_7.sce new file mode 100644 index 000000000..e520d423b --- /dev/null +++ b/260/CH4/EX4.7/4_7.sce @@ -0,0 +1,37 @@ +//Eg-4.7 +//pg-154 + + + +// Method of Sucessive substitution + +clear ; +close ; +clc ; +//Coefficients of polynomial in increasing order of power of x + +x1=0.5; +deff('[z]=f(x)','z=0.3*exp(x)'); +errorcheck=1; +iter=1; +eps=10^-8; +imax=30; + +while errorcheck==1&iter eps) + printf('\niteration number %i\n',iter); + xnew1 = x1 - horner(fx,x1)/horner(diffx,x1); + printf('xnew1 = %f \n',xnew1); + abserr = abs((xnew1 - x1)/(x1)*100); + x1 = xnew1; + iter = iter + 1; +end + +printf('\nThe solution obtained after %d iterations is %f\n',iter-1,xnew1) \ No newline at end of file -- cgit