Practical 1 1a) . Program for iterative calculation Problem: For Maclaurin series expansion, 𝑦 = 1 + 𝑥 + 𝑥 2 2! + 𝑥 3 3! +. . . + 𝑥 𝑛 𝑛! Starting with the simplest version 𝑒 𝑥 = 1 , add terms one at a time to estimate 𝑒 0.5 After each new term is added, compute the true and approximate percent relative errors. Note that the true value is 𝑒 0.5 = 5 1.648721 . . . . Add terms until the absolute value of the approximate error estimate 𝜀 𝑎 falls below a Pre-specified error criterion 𝜀 𝑎 conforming to three significant figures. PROGRAM: clc x=0.5 y=1 TrueValue=1.648721 n=input("The result is correct to at least what significant figures? ") Es=(0.5*10^(2-n)) disp(Es,"Es = ") for i=1:50 previousApproximation=y y=y+((x^i)/factorial(i)) disp(y,"For y = ") CurrentApproximation=y Et=((TrueValue-CurrentApproximation)/TrueValue)*100 disp(Et,"True percent relative error, Et=") Ea=((CurrentApproximation-previousApproximation)/CurrentApproximation)*100 disp(Ea,"Approximate percent relative error, Ea=") if (Ea<Es) break; end end OUTPUT: The result is correct to at least what significant figures? 3 Es = 0.05 For y = 1.5 True percent relative error, Et= 9.0203861 Approximate percent relative error, Ea= 33.333333 For y = 1.625 True percent relative error, Et= 1.4387516 Approximate percent relative error, Ea= 7.6923077 For y = 1.6458333 True percent relative error, Et= 0.1751459 Approximate percent relative error, Ea= 1.2658228 For y = 1.6484375 True percent relative error, Et= 0.0171951 Approximate percent relative error, Ea= 0.1579779 For y = 1.6486979 True percent relative error, Et= 0.0014001 Approximate percent relative error, Ea= 0.0157953 1b). Program to calculate the roots of a quadratic equation using the formula. Problem: Compute the values of the roots of a quadratic equation with a = 1, b = 3000.001, and c = 3. Check the computed values versus the true roots of r 1 = -0.001 and r 2 = -3000. PROGRAM: clc a=input("Enter coefficient of x^2 ") b=input("Enter coefficient of x ") c=input("Enter constant ") if (a==0) if (b==0) disp("Invalid") else disp(c/b,"Root is") end else r1=(-b+sqrt(b^2-4*a*c))/(2*a) r2=(-b-sqrt(b^2-4*a*c))/(2*a) disp("Roots are:") disp(r1,"r1=") disp(r2,"r2=") end OUTPUT: Enter coefficient of x^2 1 Enter coefficient of x 3000.001 Enter constant 3 Roots are: r1= - 0.001 r2= - 3000. 1c. Program to evaluate e^x using infinite series Problem: The exponential function y =e x is given by the infinite serie 𝒚 = 𝟏 + 𝒙 + 𝒙 𝟐 𝟐! + 𝒙 𝟑 𝟑! +. .. Evaluate this function for x =10 PROGRAM: clc x=10 y=1 n=input("Number of iterations? ") for i=1:n y=y+((x^i)/factorial(i)) if(i==n) disp(y,"Final value of y=") else disp(i,"When n=") disp(y," y = ") end end OUTPUT: Number of iterations? 30 When n= 1. y = 11. When n= 2. y = 61. When n= 3. y = 227.66667 When n= 4. y = 644.33333 When n= 5. y = 1477.6667 When n= 6. y = 2866.5556 When n= 7. y = 4850.6825 When n= 8. y = 7330.8413 When n= 9. y = 10086.573 When n= 10. y = 12842.305 When n= 11. y = 15347.516 When n= 12. y = 17435.192 When n= 13. y = 19041.096 When n= 14. y = 20188.171 When n= 15. y = 20952.887 When n= 16. y = 21430.835 When n= 17. y = 21711.98 When n= 18. y = 21868.172 When n= 19. y = 21950.379 When n= 20. y = 21991.482 When n= 21. y = 22011.055 When n= 22. y = 22019.952 When n= 23. y = 22023.82 When n= 24. y = 22025.432 When n= 25. y = 22026.076 When n= 26. y = 22026.324 When n= 27. y = 22026.416 When n= 28. y = 22026.449 When n= 29. y = 22026.46 Final value of y= 22026.464 ___________________________________________________________ Practical 2 2.1) Program to solve algebraic and transcendental equations by Bisection method. 2.1.1) write a scilab code to find the real root of the equation x^3-20=0 in the interval [1,4]. Using bisection method. perfrom 7 iterations. Scilab code: clc x1=input("enter lower limit ") x2=input("enter upper limit ") k=input("enter no of iterations") deff("[y]=f(x)","y=x^3-20") if (f(x1)*f(x2)>0) then disp("wrong limits") else j=1 while (j<=k) x3=(x1+x2)/2 printf("%f %f %f %f %f %f\n",x1, x2, x3, f(x1), f(x2), f(x3)) if (f(x1)*f(x3)<0) then x2=x3 else x1=x3 end j=j+1 end disp(x3,"approximate root in bisection method is ") end Output: enter lower limit 1 enter upper limit 4 enter no of iterations7 x1 x2 x3 f(x1) f(x2) f(x3) 1.000000 4.000000 2.500000 -19.000000 44.000000 -4.375000 2.500000 4.000000 3.250000 -4.375000 44.000000 14.328125 2.500000 3.250000 2.875000 -4.375000 14.328125 3.763672 2.500000 2.875000 2.687500 -4.375000 3.763672 -0.589111 2.687500 2.875000 2.781250 -0.589111 3.763672 1.513947 2.687500 2.781250 2.734375 -0.589111 1.513947 0.444393 2.687500 2.734375 2.710938 -0.589111 0.444393 -0.076827 approximate root in bisection method is 2.7109375 2.1.2) write a Scilab code to find the real root of the equationx 3 – x -1 =0 using Bisection method correct upto four decimal places. Scilab Code: clc x1=1 x2=2 d=0.0001 deff("y=f(x)","y=x^3-x-1") c=1 printf("x1 x2 m f(x1) f(x2) f(m)\n") while abs(x1-x2)>d m=(x1+x2)/2 printf(" %f %f %f %f %f %f\n",x1,x2,m,f(x1),f(x2),f(m)) if f(m)*f(x1)>0 x1=m else x2=m end c=c+1 end printf("the solution of equation after %i iteration is %f ",c,m) Output x1 x2 m f(x1) f(x2) f(m) 1.000000 2.000000 1.500000 -1.000000 5.000000 0.875000 1.000000 1.500000 1.250000 -1.000000 0.875000 -0.296875 1.250000 1.500000 1.375000 -0.296875 0.875000 0.224609 1.250000 1.375000 1.312500 -0.296875 0.224609 -0.051514 1.312500 1.375000 1.343750 -0.051514 0.224609 0.082611 1.312500 1.343750 1.328125 -0.051514 0.082611 0.014576 1.312500 1.328125 1.320313 -0.051514 0.014576 -0.018711 1.320313 1.328125 1.324219 -0.018711 0.014576 -0.002128 1.324219 1.328125 1.326172 -0.002128 0.014576 0.006209 1.324219 1.326172 1.325195 -0.002128 0.006209 0.002037 1.324219 1.325195 1.324707 -0.002128 0.002037 -0.000047 1.324707 1.325195 1.324951 -0.000047 0.002037 0.000995 1.324707 1.324951 1.324829 -0.000047 0.000995 0.000474 1.324707 1.324829 1.324768 -0.000047 0.000474 0.000214 the solution of equation after 15 iteration is 1.324768 2.2) Program to solve algebraic and transcendental equations by False position method. 2.2.1) write a Scilab Code to find root of the equation x3-8x+40=0 using RegulaFalsi method by 5 iterations correct upto 4 decimal places with x1=-5 and x2 = -4 Scilab Code: clc x1=input("enter lower limit ") x2=input("enter upper limit ") k=input("enter no of iterations") deff("[y]=f(x)","y=x^3-8*x+40") if (f(x1)*f(x2)>0) then disp("wrong limits") else j=1 while (j<=k) x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)) printf("%f %f %f %f %f %f\n",x1, x2, x3, f(x1), f(x2), f(x3)) if (f(x1)*f(x3)<0) then x2=x3 else x1=x3 end j=j+1 end disp(x3,"approximate root in Regulafalsi method is ") end Output: enter lower limit -5 enter upper limit -4 enter no of iterations5 -5.000000 -4.000000 -4.150943 -45.000000 8.000000 1.685418 -5.000000 -4.150943 -4.181596 -45.000000 1.685418 0.334460 -5.000000 -4.181596 -4.187634 -45.000000 0.334460 0.065576 -5.000000 -4.187634 -4.188816 -45.000000 0.065576 0.012827 -5.000000 -4.188816 -4.189047 -45.000000 0.012827 0.002508 approximate root in Regulafalsi method is - 4.1890468 2.2.2) write a Scilab Code to find root of the equation x 3 -12.2x 2 +7.45 x+42=0between 11 and 12 using RegulaFalsi method correct upto 4 decimal places Scilab Code clc x1=11 x2=12 d=0.0001 deff("y=f(x)","y=x^3-12.2*x^2+7.45*x+42") c=1 printf("x1 x2 m f(x1) f(x2) f(m)\n") while abs(x1-x2)>d m=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)) printf(" %f %f %f %f %f %f\n",x1,x2,m,f(x1),f(x2),f(m)) if f(m)*f(x1)>d x1=m else x2=m end c=c+1 end printf("the solution of equation after %i iteration is %f ",c,m) Output x1 x2 m f(x1) f(x2) f(m) 11.000000 12.000000 11.171579 -21.250000 102.600000 -3.123026 11.171579 12.000000 11.196050 -3.123026 102.600000 -0.436120 11.196050 12.000000 11.199453 -0.436120 102.600000 -0.060464 11.199453 12.000000 11.199924 -0.060464 102.600000 -0.008374 11.199924 12.000000 11.199990 -0.008374 102.600000 -0.001160 the solution of equation after 6 iteration is 11.199990 2.3) Program to solve algebraic and transcendental equations by Newton Raphson Method method. 2.3.1) write a scilab code to find root of the equation x 3 – 4x – 9 = 0 using newton Raphson Method. Scilab Code: clc x0=input("Initial guess ") k=input("Enter number of iterations ") deff("[y]=f(x)","y=x^3-4*x-9") deff("[y]=df(x)","y=3*x^2-4") j=1 while(j<=k) x1=x0-f(x0)/df(x0) printf("%f %f %f %f\n",x0,f(x0),df(x0),x1) j=j+1 x0=x1 end disp(x1,"Approximate root in numerical method is") Output: Initial guess 2 Enter number of iterations 4 2.000000 -9.000000 8.000000 3.125000 3.125000 9.017578 25.296875 2.768530 2.768530 1.145993 18.994274 2.708196 2.708196 0.030014 18.002983 2.706529 Approximate root in numerical method is 2.7065292 2.4) Program to solve algebraic and transcendental equations by Secant Method. Que : solve x^3 -20 =0 using secant method by taking initial guess as x0=4 and x1=5.5. find the answer correct upto one decimal place. scilab code: clc x1=4; x2=5.5; d=0.1; c=1; deff("y=f(x)","y=x^3-20"); printf("i\t x1\t x2\t f(x1)\t f(x2)\t x\n"); while (abs(x2-x1)>d) x = x2 - f(x2)*(x2-x1)/(f(x2)-f(x1)); printf("%d %f %f %f %f %f\n",c,x1,x2,f(x1),f(x2),x); x1=x2; x2=x; c=c+1; end printf("by Secant method the approx. root is %f",x); Output: i x1 x2 f(x1) f(x2) x 1 4.000000 5.500000 44.000000 146.375000 3.355311 2 5.500000 3.355311 146.375000 17.774479 3.058884 3 3.355311 3.058884 17.774479 8.621276 2.779683 4 3.058884 2.779683 8.621276 1.477603 2.721933 by Secant method the approx. root is 2.721933 Practical 3 Interpolations 3.1) Program for Newton’s forward interpolation 3.1.1) Write a scilab Code to find f(8) using Newton’s Forward Difference interpolation formula for the following data X 1 3 5 7 f(X) 24 120 336 720 Scilab Code: clc x=[1 3 5 7] y=[24 120 336 720] h=2 disp(h,"h=") c=1 for i=1:3 d1(c)=y(i+1)-y(i) c=c+1 end c=1 for i=1:2 d2(c)=d1(i+1)-d1(i) c=c+1 end c=1 for i=1:1 d3(c)=d2(i+1)-d2(i) c=c+1 end d=[d1(1) d2(1) d3(1)] x0=8 y_x=y(1) p=(x0-x(1))/h for i=1:3 pp=1 for j=1:i pp=pp*(p-(j-1)) disp(pp,"pp=") end y_x=y_x+pp*d(i)/factorial(i) end printf("The value of a function at %f is %f",x0,y_x) Output: h= 2. pp= 3.5 pp= 3.5 pp= 8.75 pp= 3.5 pp= 8.75 pp= 13.125 The value of a function at 8.000000 is 990.000000 3.2) Program for Newton’s backward interpolation 3.2.1 ) Write a scilab Code to find f(7.5) using Newton’s ba ckward Difference interpolation formula for the following data x 1 2 3 4 5 6 7 8 f(x) 0.0 0.004 0.02 0.12 0.15 0.257 0.325 0.231 Scilab Code: clc x=[1 2 3 4 5 6 7 8] y=[0.01 0.004 0.02 0.12 0.15 0.257 0.325 0.231] h=1 disp(h,"h=") c=1 for i=1:7 d1(c)=y(i+1)-y(i) c=c+1 end c=1 for i=1:6 d2(c)=d1(i+1)-d1(i) c=c+1 end c=1 for i=1:5 d3(c)=d2(i+1)-d2(i) c=c+1 end c=1 for i=1:4 d4(c)=d3(i+1)-d3(i) c=c+1 end c=1 for i=1:3 d5(c)=d4(i+1)-d4(i) c=c+1 end c=1 for i=1:2 d6(c)=d5(i+1)-d5(i) c=c+1 end c=1 for i=1:1 d7(c)=d6(i+1)-d6(i) c=c+1 end d=[d1(7) d2(6) d3(5) d4(4) d5(3) d6(2) d7(1)] disp(d) x0=7.5 y_x=y(1) p=(x0-x(8))/h for i=1:7 pp=1 for j=1:i pp=pp*(p+(j-1)) disp(pp,"pp=") end y_x=y_x+pp*d(i)/factorial(i) end printf("The value of a function at %f is %f",x0,y_x) Output: h= 1. - 0.094 - 0.162 - 0.123 - 0.007 0.256 0.82 1.901 pp= - 0.5 pp= - 0.5 pp= - 0.25 pp= - 0.5 pp= - 0.25 pp= - 0.375 pp= - 0.5 pp= - 0.25 pp= - 0.375 pp= - 0.9375 pp= - 0.5 pp= - 0.25 pp= - 0.375 pp= - 0.9375 pp= - 3.28125 pp= - 0.5 pp= - 0.25 pp= - 0.375 pp= - 0.9375 pp= - 3.28125 pp= - 14.765625 pp= - 0.5 pp= - 0.25 pp= - 0.375 pp= - 0.9375 pp= - 3.28125 pp= - 14.765625 pp= - 81.210938 The value of a function at 7.500000 is0.030763 3.3) Program for Lagrange’s interpolation 3.3.1) Write Scilab Code to find f(7) using Lagrange’s interpolation of the following data X 1 3 4 F(x) 4 12 19 clc funcprot(0) n= input("no. of data :") for i=1:n printf("Enter x%dvalue",i) x(i)=input("") printf(" Enter y%dvalue",i) y(i)=input("") end a= input("Enter value of a :") sum=0 for i=1:n product=1 for j=1:n if j==i then continue end product=product*(a-x(j))/(x(i)-x(j)) end product=product*y(i) sum=sum + product end printf("value of f(a)is %f",sum) Output: no. of data :3 Enter x1 value 1 Enter y1 value 4 Enter x2 value 3 Enter y2 value 12 Enter x3 value 4 Enter y3 value 19 Enter value of a :7 value of f(a)is 52.000000 3.3.2) Write a scilab code to find the polynomial usin g Lagrange’s interpolation for the following data X 1 3 4 F(x) 4 12 19 Scilab Code clc funcprot(0) n= input("no. of data :") for i=1:n printf("Enter x%dvalue",i) x(i)=input("") printf("Enter y%dvalue",i) y(i)=input("") end a=poly(0,'x') sum=0 for i=1:n product=1 for j=1:n if j==i then continue end product=product*(a-x(j))/(x(i)-x(j)) end product=product*y(i) sum=sum + product end disp(sum) Output no. of data :3 Enter x1 value 1 Enter y1 value 4 Enter x2 value 3 Enter y2 value 12 Enter x3 value 4 Enter y3 value 19 2 3 + x