{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 5: Solutions of Algebraic and Transcendental Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.10: Newton_Raphson_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.10\n", "//Newton Raphson Method\n", "//Page no. 167\n", "clc;clear;close;\n", "deff('x=f(x)','x=x*exp(-x)')\n", "deff('x=f1(x)','x=exp(-x)-x*exp(-x)')\n", "printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", "printf('-----------------------------------------------------------------------------------------------------\n')\n", "x0=2;e=0.00001\n", "for i=1:11\n", " x1=x0-f(x0)/f1(x0)\n", " e1=abs(x0-x1)\n", " printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", " x0=x1;\n", " if abs(x0)e\n", " z=(x1+x2)/2\n", " printf(' %i\t\t%f\t%f\t%f\t%f\n',i,x1,x2,z,f(z))\n", " if f(z)*f(x1)>0\n", " x1=z\n", " else\n", " x2=z\n", " end\n", " i=i+1\n", "end\n", "printf('\n\nThe solution of this equation is %g after %i Iterations',z,i-1)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.20: Laguerre_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.20\n", "//Laguerre Method\n", "//Page no. 180\n", "clc;clear;close;\n", "deff('y=f(x)','y=x^3+x^2+10*x-20')\n", "deff('y=f1(x)','y=3*x^2+2*x+10')\n", "deff('y=f2(x)','y=6*x+2')\n", "n=3;\n", "printf('i\tn\txi\t\tP(x)\t\tP1(x)\t\tP2(x)\t\tProot\t\tNroot\n')\n", "printf('---------------------------------------------------------------------------------------------------------\n')\n", "xi=1\n", "for i=0:9\n", " Proot=xi-(n*f(xi))/(f1(xi)+sqrt((n-1)*f1(xi)^2-n*f(xi)*f2(xi)))\n", " Nroot=xi-(n*f(xi))/(f1(xi)-sqrt((n-1)*f1(xi)^2-n*f(xi)*f2(xi)))\n", " printf(' %i\t%i\t%f\t%f\t%f\t%f\t%f\t%f\n',i,n,xi,f(xi),f1(xi),f2(xi),Proot,Nroot)\n", " xi=Proot\n", "end\n", "printf('\n\nProot = %f\n\nNroot = %f',Proot,Nroot)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.21: Mullers_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.21\n", "//Mullers Method\n", "//Page no. 182\n", "clc;clear;close;\n", "\n", "deff('y=f(x)','y=x^3-x-4')\n", "zi=[1;2;3];\n", "s=['i','z2','z0','z1','f2','f0','f1','a0','a1','a2','zr+','zr-']\n", "li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", "hi(1)=zi(3,1)-zi(2,1);\n", "for i=2:6\n", " for j=1:3\n", " fz(j,i-1)=f(zi(j,i-1))\n", " end\n", " di(i-1)=1+li(i-1)\n", " gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", " D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", " D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", " if abs(D1(i-1))>abs(D2(i-1)) then\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", " else\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", " end\n", " hi(i)=li(i)*hi(i-1);\n", " z(i-1)=zi(3,i-1)+hi(i)\n", " for j=1:2\n", " zi(j,i)=zi(j+1,i-1)\n", " end\n", " zi(3,i)=z(i-1)\n", "end\n", "for i=1:12\n", " if i==1 then\n", " printf(s(i))\n", " for j=1:5\n", " printf('\t\t\t%i',j-1)\n", " end\n", " elseif i<=4\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',zi(i-1,j))\n", " end\n", " elseif i<=7\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',fz(i-4,j))\n", " end\n", " elseif i<=8\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',li(j))\n", " end\n", " elseif i<=9\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',di(j))\n", " end\n", " elseif i<=10\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',gi(j))\n", " end\n", " elseif i<=11\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',z(j))\n", " end\n", " elseif i<=12\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',zi(j))\n", " end\n", " end\n", "end\n", "printf('\n\nAt the end of the %i iteration, the root of the equation is %.10f',j-2,z(j))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.22: Mullers_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.22\n", "//Mullers Method\n", "//Page no. 183\n", "clc;clear;close;\n", "\n", "deff('y=f(x)','y=x^3-x-4')\n", "zi=[1;2;3];\n", "s=['i','z0','z1','z2','f0','f1','f2','li','di','gi','li+1','hi','hi+1','zi+1','D+','D_']\n", "li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1))\n", "hi(1)=zi(3,1)-zi(2,1);\n", "for i=2:6\n", " for j=1:3\n", " fz(j,i-1)=f(zi(j,i-1))\n", " end\n", " di(i-1)=1+li(i-1)\n", " gi(i-1)=fz(1,i-1)*li(i-1)^2-fz(2,i-1)*di(i-1)^2+fz(3,i-1)*(li(i-1)+di(i-1))\n", " D1(i-1)=gi(i-1)+sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", " D2(i-1)=gi(i-1)-sqrt(gi(i-1)^2-4*fz(3,i-1)*di(i-1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i-1)+fz(3,i-1)))\n", " if abs(D1(i-1))>abs(D2(i-1)) then\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", " else\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", " end\n", " hi(i)=li(i)*hi(i-1);\n", " z(i-1)=zi(3,i-1)+hi(i)\n", " for j=1:2\n", " zi(j,i)=zi(j+1,i-1)\n", " end\n", " zi(3,i)=z(i-1)\n", "end\n", "for i=1:16\n", " if i==1 then\n", " printf(s(i))\n", " for j=1:5\n", " printf('\t\t\t%i',j-1)\n", " end\n", " elseif i<=4\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',zi(i-1,j))\n", " end\n", " elseif i<=7\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',fz(i-4,j))\n", " end\n", " elseif i<=8\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',li(j))\n", " end\n", " elseif i<=9\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',di(j))\n", " end\n", " elseif i<=10\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',gi(j))\n", " end\n", " elseif i<=11\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',li(j+1))\n", " end\n", " elseif i<=12\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',hi(j))\n", " end\n", " elseif i<=13\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',hi(j+1))\n", " end\n", " elseif i<=14\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',z(j))\n", " end\n", " elseif i<=15\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',D1(j))\n", " end\n", " elseif i<=16\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',D2(j))\n", " end\n", " end\n", "end\n", "printf('\n\nAt the end of the %ith iteration, the root of the equation is %.10f',j-1,z(j))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.23: Bairstow_Hitchcock_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.23\n", "//Bairstow Hitchcock Method\n", "//Page no. 187\n", "clc;clear;close;\n", "deff('y=f(x,p,q)','y=x^2+p*z+q')\n", "a=[1,-1,1,-1,1]\n", "a=a';a=[a,a,a,a,a]\n", "printf('Iteration-->')\n", "for i=1:5\n", " printf('\t%i\t',i)\n", "end\n", "printf('\n------------------------------------------------------------------------------------------')\n", "p(1,1)=-1.2;q(1,1)=0.95;\n", "s=['b1','b2','b3','b4','c1','c2','c3','c4','c','dp','dq','p','q']\n", "//s1=[b1;b2;b3;b4;c1;c2;c3;c4;c;dp;dq;p;q]\n", "for i=1:5\n", " b(1,i)=0;b(2,i)=a(1,i);c(1,i)=0;c(2,i)=a(1,i);\n", " for k=1:4\n", " b(k+2,i)=a(k+1,i)-p(1,i)*b(k+1,i)-q(1,i)*b(k,i)\n", " c(k+2,i)=b(k+2,i)-p(1,i)*c(k+1,i)-q(1,i)*c(k,i)\n", " end\n", " cb(1,i)=c(6,i)-b(6,i);\n", " dq(1,i)=(b(6,i)*c(4,i)-b(5,i)*cb(1,i))/(c(4,i)^2-cb(1,i)*c(3,i))\n", " dp(1,i)=(b(5,i)*c(4,i)-b(6,i)*c(3,i))/(c(4,i)^2-cb(1,i)*c(3,i))\n", " p(1,i+1)=p(1,i)+dp(1,i);q(1,i+1)=q(1,i)+dq(1,i);\n", "end\n", "for j=1:13\n", " printf('\n %s\t\t',s(j))\n", " if j<5 then\n", " for i=1:5\n", " printf('%.9f\t',b(j+2,i))\n", " end\n", " elseif j<9 then\n", " for i=1:5\n", " printf('%.9f\t',c(j-2,i))\n", " end\n", " elseif j<10\n", " for i=1:5\n", " printf('%.9f\t',cb(1,i))\n", " end\n", " elseif j<11\n", " for i=1:5\n", " printf('%.9f\t',dp(1,i))\n", " end\n", " elseif j<12\n", " for i=1:5\n", " printf('%.9f\t',dq(1,i))\n", " end\n", " elseif j<13\n", " for i=1:5\n", " printf('%.9f\t',p(1,i+1))\n", " end\n", " else\n", " for i=1:5\n", " printf('%.9f\t',q(1,i+1))\n", " end\n", " end\n", "end\n", "z=poly(0,'z');\n", "a=f(z,p(1,i+1),q(1,i+1));\n", "printf('\n\nRoots for Quadratic Equation Q = ')\n", "disp(a)\n", "a=roots(a)\n", "printf('\n\tare\n')\n", "disp(a(1))\n", "disp(a(2))\n", "\n", "" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.24: Bernoulli_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.24\n", "//Bernoulli Method\n", "//Page no. 189\n", "clc;clear;close;\n", "\n", "a=[1,-8,-15,10];\n", "for i=1:2\n", " c(i)=0;\n", "end\n", "c(3)=1;\n", "for k=4:13\n", " c(k)=-(a(2)*c(k-1)+a(3)*c(k-2)+a(4)*c(k-3))\n", " r(k-3)=c(k)/c(k-1) \n", "end\n", "disp(c,'Ck Values')\n", "disp(r,'Rk Values')\n", "disp(r(k-3),'Therefore the exact root is = ')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.25: Graeffe_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.25\n", "//Graeffe Method\n", "//Page no. 191\n", "clc;clear;close;\n", "\n", "a=[1,-6,11,-6]\n", "k=0;\n", "for k=2:6\n", " for i=1:4\n", " a(k,i)=(-1)^(i-1)*(a(k-1,i))^2\n", " j=1;\n", " while i+j<5 & i+j>2\n", " a(k,i)=a(k,i)+(-1)^(i-j-1)*2*(a(k-1,i-j))*a(k-1,i+j)\n", " break\n", " j=j+1;\n", " end\n", " end\n", "end\n", "printf('\t\t\t\ta1\t\t\t\ta2\t\t\t\ta3\n k\ta0\ta1\t\t--\t\ta2\t\t--\t\ta3\t\t--\t\n\t\t\t\ta0\t\t\t\ta1\t\t\t\ta2')\n", "printf('\n----------------------------------------------------------------------------------------------------\n')\n", "for i=1:4\n", " printf(' %i\t%g\t%.4g\t\t%.5g\t\t%.9g\t\t%.8g\t%g\t\t%.10g\n',i-1,a(i,1),a(i,2),abs(a(i,2)/a(i,1))^(1/(2^(i-1))),a(i,3),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),a(i,4),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))\n", "end\n", "for i=5:6\n", " printf(' %i\t%g\t%.4g\t%.5g\t\t%.9g\t%.8g\t%.7g\t%.10g\n',i-1,a(i,1),a(i,2),abs(a(i,2)/a(i,1))^(1/(2^(i-1))),a(i,3),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),a(i,4),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))\n", "end\n", "printf('\n\nThe Absolute Values of the roots are %g, %.8g and %g',abs(a(i,2)/a(i,1))^(1/(2^(i-1))),abs(a(i,3)/a(i,2))^(1/(2^(i-1))),abs(a(i,4)/a(i,3))^(1/(2^(i-1))))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.26: QD_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.26\n", "//QD Method\n", "//Page no. 194\n", "clc;clear;close;\n", "\n", "a=[32,-48,18,-1]\n", "for i=1:5\n", " e(i,1)=0;\n", " e(i,4)=0;\n", "end\n", "q(1,1)=-a(2)/a(1);\n", "q(1,2)=0;q(1,3)=0;\n", "e(1,2)=a(3)/a(2);\n", "e(1,3)=a(4)/a(3);\n", "for i=2:16\n", " for j=1:3\n", " q(i,j)=e(i-1,j+1)+q(i-1,j)-e(i-1,j)\n", " end\n", " for j=1:2\n", " e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j)\n", " end\n", "end\n", "printf('e0\t\tq1\t\te1\t\tq2\t\te2\t\tq3\t\te3\n')\n", "printf('------------------------------------------------------------------------------------------------------------\n')\n", "for i=1:14\n", " for j=1:3\n", " printf('\t\t%.10f\t',q(i,j))\n", " end\n", " printf('\n')\n", " for j=1:4\n", " printf('%.10f\t\t\t',e(i,j))\n", " end\n", " printf('\n')\n", "end\n", "printf('\t\t%.10f\t\t\t%.10f\t\t\t%.10f\n',q(15,1),q(15,2),q(15,3))\n", "printf('\nThe exact roots are \t%.10f and %.10f',q(15,1),q(15,3))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.27: Linear_Iteration_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.27\n", "//Linear Iteration Method\n", "//Page no. 198\n", "clc;clear;close;\n", "\n", "deff('x=f(x)','x=20/(x^2+2*x+10)')\n", "printf('n\tx\t\tf(x)\n')\n", "printf('-------------------------------------\n')\n", "x=1;\n", "for i=1:19\n", " printf(' %i\t%.10f\t%.10f\n',i,x,f(x))\n", " x1=x;\n", " x=f(x);\n", "end\n", "printf('\n\nx = %.10f',x1)" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.28: Aitkens_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.28\n", "//Aitkens Method\n", "//Page no. 199\n", "clc;clear;close;\n", "\n", "deff('x=f(x)','x=20/(x^2+2*x+10)')\n", "printf('n\tx0\t\tx1\t\tx2\t\tx3\t\ty\t\tdx0\n')\n", "printf('-----------------------------------------------------------------------------------------------------\n')\n", "x0=1;e=0.0001\n", "for i=1:3\n", " x1=f(x0);x2=f(x1);x3=f(x2);\n", " y=x3-((x3-x2)^2)/(x3-2*x2+x1)\n", " dx0=y-x0;\n", " \n", " printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i,x0,x1,x2,x3,y,dx0)\n", " x0=y;\n", " if abs(x0)e\n", " z=(x1+x2)/2\n", " printf(' %i\t\t%f\t%f\t%f\t%f\n',i,x1,x2,z,f(z))\n", " if f(z)*f(x1)>0\n", " x1=z\n", " else\n", " x2=z\n", " end\n", " i=i+1\n", "end\n", "printf('\n\nThe solution of this equation is %g after %i Iterations',z,i-1)\n", "\n", "printf('\n\n\nNote : There are computational errors in the answer given by the book for this example')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.31: Secant_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.31\n", "//Secant Method\n", "//Page no. 200\n", "clc;clear;close;\n", "deff('x=f(x)','x=(x-0.6)*(x-1.3)^2*(x-2)^3+0.01234*log(x)')\n", "printf('n\txn\t\tf(xn)\t\tXn+1\t\tf(Xn+1)\t\tXn+2\t\tError\n')\n", "printf('----------------------------------------------------------------------------------------------------------\n')\n", "x0=0.1;x1=1.2;e=0.00001\n", "for i=1:7\n", " x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0))\n", " e1=abs(x0-x2)\n", " printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),x1,f(x1),x2,e1)\n", " x0=x1;\n", " x1=x2\n", " if abs(x0)0 then\n", " x1=x3\n", " else\n", " x2=x3\n", " end\n", " if abs(f(x3))abs(D2(i-1)) then\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", " else\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", " end\n", " hi(i)=li(i)*hi(i-1);\n", " z(i-1)=zi(3,i-1)+hi(i)\n", " for j=1:2\n", " zi(j,i)=zi(j+1,i-1)\n", " end\n", " zi(3,i)=z(i-1)\n", "end\n", "printf('\n\n ')\n", "for i=1:16\n", " if i==1 then\n", " printf(s(i))\n", " for j=1:5\n", " printf('\t\t\t %i',j-1)\n", " end\n", " printf('\n----------------------------------------------------------------------------------------------------------------------------------')\n", " elseif i<=4\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',zi(i-1,j))\n", " end\n", " elseif i<=7\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',fz(i-4,j))\n", " end\n", " elseif i<=8\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',li(j))\n", " end\n", " elseif i<=9\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',di(j))\n", " end\n", " elseif i<=10\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',gi(j))\n", " end\n", " elseif i<=11\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',li(j+1))\n", " end\n", " elseif i<=12\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',hi(j))\n", " end\n", " elseif i<=13\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',hi(j+1))\n", " end\n", " elseif i<=14\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',z(j))\n", " end\n", " elseif i<=15\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',D1(j))\n", " end\n", " elseif i<=16\n", " printf('\n %s',s(i))\n", " for j=1:5\n", " printf('\t\t%.10f',D2(j))\n", " end\n", " end\n", "end\n", "printf('\n\nAt the end of the %ith iteration by mullers method, the root of the equation is %.10f',j-1,z(j))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.33: Newton_Raphson_and_Mullers_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.33\n", "//Newton Raphson and Mullers Method\n", "//Page no. 202\n", "clc;clear;close;\n", "deff('x=f(x)','x=x^4-8*x^3+18*x^2+0.12*x-24.24')\n", "deff('x=f1(x)','x=4*x^3-24*x^2+36*x+0.12')\n", "\n", "//newton raphson\n", "x9=[1.5,2.5,2.7,3.1;4,5,14,10]\n", "for h=1:4\n", " x0=x9(1,h);e=0.00001\n", "for i=1:x9(2,h)\n", " x1=x0-f(x0)/f1(x0)\n", " e1=abs(x0-x1)\n", " x0=x1;\n", " if abs(x0)abs(D2(i-1)) then\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1)\n", " else\n", " li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1)\n", " end\n", " hi(i)=li(i)*hi(i-1);\n", " z(i-1)=zi(3,i-1)+hi(i)\n", " for j=1:2\n", " zi(j,i)=zi(j+1,i-1)\n", " end\n", " zi(3,i)=z(i-1)\n", "end\n", "printf('\n\nAt the end of the %ith iteration by mullers method, the root of the equation is %.10f',j+2,z(j))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.34: QD_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.34\n", "//QD Method\n", "//Page no. 202\n", "clc;clear;close;\n", "a=[1,2,10,-20]\n", "for i=1:5\n", " e(i,1)=0;\n", " e(i,4)=0;\n", "end\n", "q(1,1)=-a(2)/a(1);\n", "q(1,2)=0;q(1,3)=0;\n", "e(1,2)=a(3)/a(2);\n", "e(1,3)=a(4)/a(3);\n", "for i=2:7\n", " for j=1:3\n", " q(i,j)=e(i-1,j+1)+q(i-1,j)-e(i-1,j)\n", " end\n", " for j=1:2\n", " e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j)\n", " end\n", "end\n", "printf('e0\t\tq1\t\te1\t\tq2\t\te2\t\tq3\t\te3\n')\n", "printf('------------------------------------------------------------------------------------------------------------\n')\n", "for i=1:7\n", " for j=1:3\n", " printf('\t\t%.10f\t',q(i,j))\n", " end\n", " printf('\n')\n", " for j=1:4\n", " printf('%.10f\t\t\t',e(i,j))\n", " end\n", " printf('\n')\n", "end\n", "printf('\t\t%.10f\t\t\t%.10f\t\t\t%.10f\n',q(7,1),q(7,2),q(7,3))\n", "printf('\nThe exact roots are \t%.10f and %.10f',q(7,1),q(7,3))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 5.35: Newton_Raphson_Method.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example 5.35\n", "//Newton Raphson Method\n", "//Page no. 203\n", "clc;clear;close;\n", "deff('x=f(x)','x=x^3-30*x^2+2552')\n", "deff('x=f1(x)','x=3*x^2-60*x')\n", "//newton raphson\n", "printf('n\txn\t\t\f(xn)\t\tf1(xn)\t\tXn+1\t\tError\n')\n", "printf('-----------------------------------------------------------------------------------------------------\n')\n", "x0=10;e=0.00001\n", "for i=1:4\n", " x1=x0-f(x0)/f1(x0)\n", " e1=abs(x0-x1)\n", " printf(' %i\t%.10f\t%.10f\t%.10f\t%.10f\t%.10f\n',i-1,x0,f(x0),f1(x0),x1,e1)\n", " x0=x1;\n", " if abs(x0)0 then\n", " x1=x3\n", " else\n", " x2=x3\n", " end\n", " if abs(f(x3))0 then\n", " x1=x4\n", " else\n", " x2=x4\n", " end\n", " if abs(f(x4))