Q-basic Numerical Analysis Programs

  • Uploaded by: Omed Ghareb
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Q-basic Numerical Analysis Programs as PDF for free.

More details

  • Words: 6,766
  • Pages: 54
Kurdistan Region-Iraq Sulaimani University College of Science Physics Department

Numerical Analysis Programs Using Q-basic (2009)

Prepared by Dr. Omed Gh. Abdullah

Problem: Write a program in Q-basic to solve the equation below, by using bisection method: f ( x) = x 2 + 0.9 x − 0.1 , [0,1] , e = 0.0001 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS DEF fnf (x) = x * x + .9 * x - .1 READ a, b, e DATA 0,1,0.0001 5 c = (a + b) / 2 PRINT c f1 = fnf(a): f2 = fnf(b): f3 = fnf(c) IF f1 * f3 = 0 THEN 25 IF f2 * f3 = 0 THEN 25 IF f1 * f3 < 0 THEN 10 a=c GOTO 15 10 b = c 15 IF ABS(a - b) < e THEN 25 GOTO 5 25 PRINT "The root is:", c END

Problem: Write the program in Q-basic to find the root of the function below by using false position method: f ( x) = x log( x) − 1 , [1,2] , e = 0.0001 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS DEF fnf (x) = x * LOG(x) - 1 READ a, b, e DATA 1,2,0.0001 c0 = b f1 = fnf(a): f2 = fnf(b) 5 c = (a * f2 - b * f1) / (f2 - f1) PRINT c f3 = fnf(c) IF f1 * f3 = 0 THEN 25 IF f2 * f3 = 0 THEN 25 IF f1 * f3 < 0 THEN 15 a = c: f1 = f3 GOTO 20 15 b = c: f2 = f3 20 IF ABS(c - c0) < e THEN 25 c0 = c GOTO 5 25 PRINT "The root is:", c END

Problem: Write a program in Q-basic by using secand method to find the root of: f ( x) = x 3 − 3x + 2 , [−2.4,−2.6] , e = 0.005 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS DEF fnf (x) = x ^ 3 - 3 * x + 2 READ a, b, e DATA -2.4,-2.6,0.005 f1 = fnf(a) 10 f2 = fnf(b) c = (a * f2 - b * f1) / (f2 - f1) PRINT c f3 = fnf(c) IF ABS(a - b) < e THEN 25 a = b: b = c: f1 = f2 GOTO 10 25 PRINT "The root is:", c END

Problem: Write a program in Q-basic to find the root of the function below, by using Newton-Raphson method: xο = 3 , e = 0.005 f ( x) = x 2 − 4 sin( x) , ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS READ x0, e DATA 1,0.0001 DEF fnf (x) = x ^ 2 - 4 * SIN(x) DEF fng (x) = 2 * x - 4 * COS(x) 5 x1 = x0 - (fnf(x0) / fng(x0)) PRINT x1 IF ABS(x1 - x0) < e THEN 25 x0 = x1: GOTO 5 25 PRINT "The root is:", c END

Problem: Write a program in Q-basic to find the root of the function below, by using iteration method: xο = 4 , e = 0.001 f ( x) = x 2 − 2 x − 3 , :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS READ x0, e DATA 4,.001 DEF fnf (x) = SQR(2 * x + 3) 5 x1 = fnf(x0) PRINT x1 IF ABS(x1 - x0) < e THEN 25 x0 = x1: GOTO 5 25 PRINT "The root is:", x1 END

Problem: Write a program in Q-basic to find the root of the following system, by using iterative method: (xο , yο ) = (3,4) , e = 0.001 f ( x) = x 2 − xy − 7 , f ( x) = x 2 + 2 y − x :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS READ x0, y0, e DATA 3,4,.001 DEF fnf (x, y) = SQR(x * y + 7) DEF fng (x, y) = SQR(2 * y + x) 10 x1 = fnf(x0, y0) 20 y1 = fng(x0, y0) PRINT x1, y1 IF ABS(x1 - x0) < e AND ABS(y1 - y0) < e THEN 25 x0 = x1 y0 = y1: GOTO 10 25 PRINT "x1="; x1 PRINT "y1="; y1 END

Problem: Write a program in Q-basic to find the root of the function below, by using itiken method: xο = 3 , e = 0.001 f ( x) = x 2 − x − 2 , ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS READ x0, e DATA 3,.001 DEF fnf (x) = 1 + 2 / x 10 x1 = fnf(x0) x2 = fnf(x1) x3 = fnf(x2) PRINT x0, x1, x2 xx = x2 - ((x2 - x1) ^ 2 / (x2 - 2 * x1 + x0)) PRINT xx IF ABS(xx - x0) < e THEN 25 x0 = xx: GOTO 10 25 PRINT "The root is:"; xx END

Problem: Write a program in Q-basic to solve the system, by using Gauss elimination: 4 x1 − 9 x2 + 2 x3 = 5 2 x1 − 4 x2 + 6 x3 = 3 x1 − x2 + 3x3 = 4 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS n = 3: m = n + 1 DIM a(n, m), x(n) FOR i = 1 TO n FOR j = 1 TO m READ a(i, j) DATA 4,-9,2,5,2,-4,6,3,1,-1,3,4 NEXT j NEXT i FOR k = 1 TO n - 1 FOR i = k + 1 TO n b = a(i, k) / a(k, k) FOR j = 1 TO m a(i, j) = a(i, j) - a(k, j) * b NEXT j NEXT i NEXT k x(n) = a(n, m) / a(n, n) FOR i = n - 1 TO 1 STEP -1 s=0 FOR j = n TO i + 1 STEP -1 s = s + a(i, j) * x(j) NEXT j x(j) = (a(i, m) - s) / a(i, i) NEXT i FOR i = 1 TO n PRINT x(i) NEXT i END

Problem: Write a program in Q-basic to solve the system, by using Gauss Jorden method: x1 + x3 = 1 x1 + x 2 = 1 x 2 + x3 = 1

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS n = 3: m = n + 1 DIM a(n, m), x(n) FOR i = 1 TO n FOR j = 1 TO m READ a(i, j) DATA 1,0,1,1,1,1,0,1,0,1,1,1 NEXT j NEXT i FOR k = 1 TO n - 1 FOR i = k + 1 TO n b = a(i, k) / a(k, k) FOR j = 1 TO m a(i, j) = a(i, j) - a(k, j) * b NEXT j NEXT i NEXT k FOR k = n TO n - 1 STEP -1 FOR i = k - 1 TO 1 STEP -1 b = a(i, k) / a(k, k) FOR j = m TO 1 STEP -1 a(i, j) = a(i, j) - a(k, j) * b NEXT j NEXT i NEXT k FOR i = 1 TO n x(i) = a(i, m) / a(i, i) PRINT x(i) NEXT i

Problem: Write a program in Q-basic to solve the system, by using Jaccobi method: 10 x1 + x2 + x3 = 12 x1 + 10 x2 + x3 = 12 x1 + x2 + 10 x3 = 12 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: CLS READ n, e DATA 3,0.00001 DIM a(n, n), b(n), x(n), y(n) FOR i = 1 TO n FOR j = 1 TO n READ a(i, j) DATA 10,1,1,1,10,1,1,1,10 NEXT j NEXT i FOR i = 1 TO n READ b(i) DATA 12,12,12 NEXT i FOR i = 1 TO n y(i) = 0 NEXT i 5 FOR i = 1 TO n s = 0: d = 0 FOR j = 1 TO n IF i = j THEN 7 s = s + a(i, j) * y(j) 7 NEXT j x(i) = (b(i) - s) / a(i, i) PRINT x(i) d = d + ABS(x(i) - y(i)) NEXT i IF d < e THEN 2 FOR i = 1 TO n y(i) = x(i) NEXT i GOTO 5 2 PRINT FOR i = 1 TO n PRINT x(i) NEXT i END

Problem: Write a program in Q-basic to solve the system, by using Gauss Seidel method: 10 x1 + x2 + x3 = 12 x1 + 10 x2 + x3 = 12 x1 + x2 + 10 x3 = 12 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CLS READ n, e DATA 3,0.00001 DIM a(n, n), b(n), x(n), y(n) FOR i = 1 TO n FOR j = 1 TO n READ a(i, j) DATA 10,1,1,1,10,1,1,1,10 NEXT j NEXT i FOR i = 1 TO n READ b(i) DATA 12,12,12 NEXT i FOR i = 1 TO n y(i) = 0 NEXT i 5 FOR i = 1 TO n s1 = 0: s2 = 0 FOR j = 1 TO n IF i = j THEN 2 IF i < j THEN s1 = s1 + a(i, j) * y(j) IF i > j THEN s2 = s2 + a(i, j) * x(j) 2 NEXT j s = s1 + s2 x(i) = (b(i) - s) / a(i, i) NEXT i d=0 FOR i = 1 TO n d = d + ABS(x(i) - y(i)) NEXT i IF d < e THEN 3 FOR i = 1 TO n y(i) = x(i) NEXT i

GOTO 5 3 PRINT FOR i = 1 TO n PRINT x(i) NEXT i END

Problem: Write a program to find the interpolated value for x = 3 , using Lagrangian Polynomial, from the following data. X F(x)

3.2 22

2.7 17.8

1 14.2

4.8 38.3

REM "Lagrange Interpolation" CLS DIM x(100), y(100) INPUT "No. of pairs"; n INPUT "x="; x FOR k = 0 TO n - 1 READ x(k), y(k) NEXT k DATA 3.2,22,2.7,17.8,1,14.2,4.8,38.3 sum = 0 FOR i = 0 TO n - 1 prod = 1 FOR k = 0 TO n - 1 IF i = k THEN 5 prod = prod * (x - x(k)) / (x(i) - x(k)) 5 NEXT k sum = sum + prod * y(i) NEXT i PRINT "x="; x; "y="; sum END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

No. of Pairs? 4 X=? 3 X=3 y=20.21196

Problem: Write a program to find the Lagrangian Polynomial.

REM "Lagrange Polynomial" CLS DIM x(10), y(10) INPUT "No. of pairs"; n FOR k = 1 TO n READ x(k), y(k) NEXT k DATA 3,9,5,35,8,119,10,205 DIM a(10), b(10), c(10), xx(10, 10), s(10) FOR i = 1 TO n prod = 1 FOR k = 1 TO n IF i = k THEN 5 prod = prod * 1 / (x(i) - x(k)) 5 NEXT k s(i) = prod * y(i) PRINT "------------------" PRINT s(i) NEXT i FOR k = 1 TO n FOR m = 1 TO n IF k = m THEN 4 xx(k, m) = x(m) GOTO 2 4 xx(k, m) = 0 2 NEXT m NEXT k FOR i = 1 TO n a=0 FOR j = 1 TO n a = a + xx(i, j) 6 NEXT j a(i) = -1 * a NEXT i FOR i = 1 TO n b=0 FOR j = 1 TO n - 1 FOR k = j + 1 TO n b = b + xx(i, j) * xx(i, k) NEXT k NEXT j b(i) = b

NEXT i FOR i = 1 TO n c=0 FOR j = 1 TO n FOR k = j + 1 TO n FOR l = k + 1 TO n c = c + xx(i, j) * xx(i, k) * xx(i, l) NEXT l NEXT k NEXT j c(i) = -1 * c NEXT i PRINT "------------------" FOR i = 1 TO n PRINT a(i), b(i), c(i) NEXT i a1 = 0: a2 = 0: a3 = 0: a4 = 0 FOR i = 1 TO n a1 = a1 + s(i) a2 = a2 + s(i) * a(i) a3 = a3 + s(i) * b(i) a4 = a4 + s(i) * c(i) NEXT i PRINT : PRINT PRINT "Lagrange Polynomial is:" PRINT "--------------------------------": PRINT IF n = 2 THEN PRINT "P(x)=("; a1; "x)+("; a2; ")" IF n = 3 THEN PRINT "P(x)=("; a1; "x^2)+("; a2; "x)+("; a3; ")" IF n = 4 THEN PRINT "P(x)=("; a1; "x^3)+("; a2; "x^2)+("; a3; "x)+("; a4; ")" PRINT END 20 FOR i = 1 TO n INPUT "x="; t p = a1 * t ^ 3 + a2 * t ^ 2 + a3 * t + a4 PRINT "p(x)="; p NEXT i END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a program to compute the divided difference table, from the tabulated data. X F(x)

1 0

2 1

3 4

4 6

5 10

REM "divided differences" DIM x(10), y(10) CLS INPUT "No. of data=", n FOR i = 1 TO n READ x(i), y(i) NEXT i DATA 1,0,2,1,3,4,4,6,5,10 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j PRINT FOR i = 1 TO n - 1 PRINT SPC(5 * i); FOR j = 1 TO n - i y(j) = (y(j + 1) - y(j)) / (x(j + i) - x(j)) PRINT y(j); SPC(10); NEXT j PRINT NEXT i END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a program to find the interpolated value for x = 1.5 , using divided difference form, for these tabulated data. X F(x)

1 0

2 1

3 4

4 6

REM "Newton Interpolation Divided Differences" DIM x(10), y(10), d(10, 10) CLS INPUT "No. of data=", n INPUT "x="; x FOR i = 1 TO n READ x(i), y(i) NEXT i DATA 1,0,2,1,3,4,4,6,5,10 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j s = y(1) FOR i = 1 TO n - 1 PRINT SPC(5 * i); FOR j = 1 TO n - i y(j) = (y(j + 1) - y(j)) / (x(j + i) - x(j)) d(i, j) = y(j) PRINT y(j); SPC(10); NEXT j PRINT NEXT i FOR i = 1 TO n p=1 FOR j = 1 TO i p = p * (x - x(j)) NEXT j PRINT p, d(i, 1) s = s + d(i, 1) * p NEXT i

5 10

PRINT "x="; x, "y="; s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Y=-.171875

Problem: Write a program to find the interpolated value for x = 3.4 , using Newton forward method, for these tabulated data. X F(x)

1 13

2 15

3 12

REM "Newton Forward Interpolation" DIM x(10), y(10), d(10, 10) CLS INPUT "No. of data=", n INPUT "x="; x FOR i = 1 TO n READ x(i), y(i) NEXT i DATA 1,13,2,15,3,12,4,9,5,13 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j s = y(1) FOR i = 1 TO n - 1 PRINT SPC(6 * i); FOR j = 1 TO n - i y(j) = (y(j + 1) - y(j)) d(i, j) = y(j) PRINT y(j); SPC(10); NEXT j PRINT NEXT i k = (x - x(1)) / (x(2) - x(1)) FOR i = 1 TO n - 1 p=1 FOR j = 0 TO i - 1 p = p * (k - j) NEXT j f=1 FOR j = 1 TO i: f = f * j: NEXT j

4 9

5 13

s = s + d(i, 1) * p / f NEXT i PRINT : PRINT "x="; x, "y="; s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Y= 10.4528

Problem: Write a program to find the interpolated value for x = 3.6 , using Newton backward method, for these tabulated data. X F(x)

1 10

2 -9

3 -36

REM "Newton Backward Interpolation" DIM x(10), y(10), d(10, 10) CLS INPUT "No. of data=", n INPUT "x="; x FOR i = 1 TO n READ x(i), y(i) NEXT i DATA 1,10,2,-9,3,-36,4,-41,5,30 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j s = y(n) FOR i = 1 TO n - 1 PRINT SPC(6 * i); FOR j = 1 TO n - i y(j) = (y(j + 1) - y(j)) d(i, j) = y(j) PRINT y(j); SPC(10); NEXT j PRINT NEXT i k = (x - x(n)) / (x(2) - x(1)) FOR i = 1 TO n - 1 p=1 FOR j = 0 TO i - 1 p = p * (k + j) NEXT j f=1 FOR j = 1 TO i: f = f * j: NEXT j

4 -41

5 30

s = s + d(i, n - i) * p / f PRINT p, d(i, n - i) NEXT i PRINT : PRINT "x="; x, "y="; s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Y=-44.5584

Problem: Write a program to determine the parameters a1 & a 2 so that

f ( x ) = a1 + a 2 x , fits the following data in least squares sense. X y

0 1

0.3 2.7

0.6 4.3

0.9 6

1.2 7.5

1.5 9

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA 0,1,.3,2.7,.6,4.3,.9,6,1.2,7.5,1.5,9,1.8,10.6,2.1,12 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) sy = sy + y(i) sxx = sxx + x(i) ^ 2 sxy = sxy + x(i) * y(i) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d PRINT "a1="; a1 PRINT "a2="; a2 s=0 FOR i = 1 TO n

1.8 10.6

2.1 12

f(i) = a1 + a2 * x(i) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sx=8.4 sxx=12.6 sy=53.1 sxy=75.57 a1=1.133333 a2=5.242064 Standard deviation = 6.726178 E -02

Problem: Write a program to determine the parameters A & B so that f ( x) = A ln( x) + B , fits the following data in least squares sense. X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .1,.27,.2,.72,.3,1.48,.4,2.66,.5,4.48,.6,7.26,.7,11.43,.8,17.64,.9,26.78 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + LOG(x(i)) sy = sy + y(i) sxx = sxx + LOG(x(i)) ^ 2 sxy = sxy + LOG(x(i)) * y(i) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 b = a1 PRINT "A="; a

PRINT "B="; b s=0 FOR i = 1 TO n f(i) = a * LOG(x(i)) + b s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A=9.74113 B=16.66255 S=260.5141

Problem: Write a program to determine the parameters A & C so that

f ( x ) = C e A x , fits the following data in least squares sense. X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .1,.27,.2,.72,.3,1.48,.4,2.66,.5,4.48,.6,7.26,.7,11.43,.8,17.64,.9,26.78 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) sy = sy + LOG(y(i)) sxx = sxx + x(i) ^ 2 sxy = sxy + x(i) * LOG(y(i)) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 c = EXP(a1) PRINT "A="; a

PRINT "C="; c s=0 FOR i = 1 TO n f(i) = c * EXP(a * x(i)) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= 5.512738 C= .2359106 S=52.53117

Problem: Write a program to determine the parameters A & C so that f ( x ) = C x A , fits the following data in least squares sense. X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .1,.27,.2,.72,.3,1.48,.4,2.66,.5,4.48,.6,7.26,.7,11.43,.8,17.64,.9,26.78 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + LOG(x(i)) sy = sy + LOG(y(i)) sxx = sxx + LOG(x(i)) ^ 2 sxy = sxy + LOG(x(i)) * LOG(y(i)) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 c = EXP(a1) PRINT "A="; a

PRINT "C="; c s=0 FOR i = 1 TO n f(i) = c * x(i) ^ a s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= 2.092605 C= 23.42711 S= 75.07242

Problem: Write a program to determine the parameters C & D so that Dx f ( x ) = C x e , fits the following data in least squares sense. X y

0.1 0.27

0.2 0.72

0.3 1.48

0.4 2.66

0.5 4.48

0.6 7.26

0.7 11.4 3

0.8 17.6 4

0.9 26.7 8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .1,.27,.2,.72,.3,1.48,.4,2.66,.5,4.48,.6,7.26,.7,11.43,.8,17.64,.9,26.78 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) sy = sy + LOG(y(i) / x(i)) sxx = sxx + x(i) ^ 2 sxy = sxy + x(i) * LOG(y(i) / x(i)) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d d = a2 c = EXP(a1) PRINT "C="; c

PRINT "D="; d s=0 FOR i = 1 TO n f(i) = c * x(i) * EXP(d * x(i)) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C= 1.993407 D= 3.004763 S= 1.117279 E-03

Problem: Write a program to determine the parameters A & B so that A f ( x ) = + B , fits the following data in least squares sense. x X y

0.5 3.3

2 2

3.5 1.4

4.1 1.2

5 1

6.2 0.8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .5,3.3,2,2,3.5,1.4,4.1,1.2,5,1,6.2,.8,7.5,.64,9.2,.5 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + 1 / x(i) sy = sy + y(i) sxx = sxx + (1 / x(i)) ^ 2 sxy = sxy + (1 / x(i)) * y(i) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 b = a1

7.5 0.64

9.2 0.5

PRINT "A="; a PRINT "B="; b s=0 FOR i = 1 TO n f(i) = (a / x(i)) + b s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= 1.353131 B= .7405201 S=.7070401

Problem: Write a program to determine the parameters D & C so that D f ( x) = , fits the following data in least squares sense. x+C X y

0.5 3.3

2 2

3.5 1.4

4.1 1.2

5 1

6.2 0.8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .5,3.3,2,2,3.5,1.4,4.1,1.2,5,1,6.2,.8,7.5,.64,9.2,.5 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) * y(i) sy = sy + y(i) sxx = sxx + (x(i) * y(i)) ^ 2 sxy = sxy + (x(i) * y(i)) * y(i) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d c = -1 / a2 d = a1 * c PRINT "C="; c

7.5 0.64

9.2 0.5

PRINT "D="; d s=0 FOR i = 1 TO n f(i) = d / (x(i) + c) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ C= 1.369226 D= 6.209051 S=5.723841 E-02

Problem: Write a program to determine the parameters A & B so that 1 f ( x) = , fits the following data in least squares sense. A x+B X y

0.5 3.3

2 2

3.5 1.4

4.1 1.2

5 1

6.2 0.8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .5,3.3,2,2,3.5,1.4,4.1,1.2,5,1,6.2,.8,7.5,.64,9.2,.5 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) sy = sy + 1 / y(i) sxx = sxx + x(i) ^ 2 sxy = sxy + x(i) * 1 / y(i) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 b = a1 PRINT "A="; a

7.5 0.64

9.2 0.5

PRINT "B="; b s=0 FOR i = 1 TO n f(i) = 1 / (a * x(i) + b) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= .1953442 B= 9.250808 E-02 S= 3.864387

Problem: Write a program to determine the parameters A & B so that x f ( x) = , fits the following data in least squares sense. A x+B X y

0.5 3.3

2 2

3.5 1.4

4.1 1.2

5 1

6.2 0.8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .5,3.3,2,2,3.5,1.4,4.1,1.2,5,1,6.2,.8,7.5,.64,9.2,.5 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + 1 / x(i) sy = sy + 1 / y(i) sxx = sxx + (1 / x(i)) ^ 2 sxy = sxy + 1 / (x(i) * y(i)) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a1 b = a2

7.5 0.64

9.2 0.5

PRINT "A="; a PRINT "B="; b s=0 FOR i = 1 TO n f(i) = x(i) / (a * x(i) + b) s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= 1.279117 B= -.5697291 S= 16.41069

Problem: Write a program to determine the parameters A & B so that 1 f ( x) = 2 , fits the following data in least squares sense. A x+B

(

)

X y

0.5 3.3

2 2

3.5 1.4

4.1 1.2

5 1

6.2 0.8

REM "List Square Fitting" CLS DIM x(15), y(15) INPUT "No. of Data=", n FOR j = 1 TO n READ x(j), y(j) NEXT j DATA .5,3.3,2,2,3.5,1.4,4.1,1.2,5,1,6.2,.8,7.5,.64,9.2,.5 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j sx = 0: sxx = 0: sy = 0: sxy = 0 FOR i = 1 TO n sx = sx + x(i) sy = sy + (y(i)) ^ (-.5) sxx = sxx + x(i) ^ 2 sxy = sxy + x(i) * (y(i)) ^ (-.5) NEXT i PRINT PRINT "sx="; sx PRINT "sxx"; sxx PRINT "sy="; sy PRINT "sxy="; sxy d = n * sxx - sx ^ 2 a1 = (sxx * sy - sx * sxy) / d a2 = (n * sxy - sx * sy) / d a = a2 b = a1 PRINT "A="; a

7.5 0.64

9.2 0.5

PRINT "B="; b s=0 FOR i = 1 TO n f(i) = 1 / (a * x(i) + b) ^ 2 s = s + (y(i) - f(i)) ^ 2 NEXT i PRINT "Standard Deviation=", s END

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ A= 9.919496 E-02 B= .5035566 S= 2.27583 E-03

Problem: Write a quick-basic program to find first, second, third, and fourth derivation of the function f ( x) = ln( x 3 ) at x = 1 and h = 0.01

REM "Derivation using Different Formula" CLS DEF fnf (x) = LOG(x ^ 3) INPUT "x=", x INPUT "h=", h PRINT "---------------------------------------" REM "df: Forward, db: Backward, dc: Central" df = (fnf(x + h) - fnf(x)) / h db = (fnf(x) - fnf(x - h)) / h dc = (fnf(x + h) - fnf(x - h)) / (2 * h) PRINT "Forward Derivation is", df PRINT "Backward Derivation is", db PRINT "Central Derivation is", dc PRINT "---------------------------------------" REM "d3p: Three Point, d5p: Five Point" d3p = (fnf(x + h) - fnf(x - h)) / (2 * h) d5p = (-1 * fnf(x + 2 * h) + 8 * fnf(x + h) - 8 * fnf(x - h) + fnf(x - 2 * h)) / (12 * h) PRINT "Three Point Derivation is", d3p PRINT "Five Point Derivation is", d5p PRINT "---------------------------------------" REM "d2: Second derivation, d3: Third Derivation, d4: Fourth Derivation" d2 = (fnf(x + h) - 2 * fnf(x) + fnf(x - h)) / h ^ 2 d3 = (fnf(x + 2 * h) - 2 * fnf(x + h) + 2 * fnf(x - h) - fnf(x - 2 * h)) / (2 * h ^ 3) d4 = (fnf(x + 2 * h) - 4 * fnf(x + h) + 6 * fnf(x) - 4 * fnf(x - h) + fnf(x - 2 * h)) /h^4 PRINT "Second Derivation is", d2 PRINT "Third Derivation is ", d3 PRINT "Fourth Derivation is ", d4 END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

x=1 h=.01 ------------------------------------------------------------------Forward Derivation is 2.985096 Backward Derivation is 3.015098 Central Derivation is 3.000097 ------------------------------------------------------------------Three Point Derivation is 3.000097 Five Point Derivation is 2.999997 ------------------------------------------------------------------Second Derivation is -3.000144 Third Derivation is 6.003306 Fourth Derivation is -17.8814

Problem: Write a quick-basic program to find first derivation from the following data at x = 0.1 using derivation of Lagrange polynomial. x y

0.1 0.2 0.3 0.4 0.01 0.04 0.09 0.16

REM "Derivation using Lagrange Formula" CLS INPUT "No. of Data"; n INPUT "x="; x DIM x(n), y(n) FOR i = 1 TO n READ x(i), y(i) NEXT i DATA .1,.01,.2,.04,.3,.09,.4,.16 sum = 0 FOR i = 1 TO n s=0 FOR j = 1 TO n IF j = i THEN GOTO 10 p=1 FOR k = 1 TO n IF k = j OR k = i THEN GOTO 20 p = p * (x - x(k)) / (x(i) - x(k)) 20 NEXT k s = s + p / (x(i) - x(j)) 10 NEXT j sum = sum + s * y(i) NEXT i PRINT "The First Derivation=", sum END

The First Derivation= 0.2000000 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a quick-basic program to find first derivation from the following data using derivation of Newton Forward polynomial. x y

1 -4

2 -1

3 10

4 35

5 80

6 151

7 254

y = x3 − 2 x2 + 2 x − 5

REM "Derivative from Newton Forward" DIM x(10), y(10), d(10, 10) CLS INPUT "No. of data=", n FOR i = 1 TO n READ x(i), y(i) NEXT i DATA 1,-4,2,-1,3,10,4,35,5,80,6,151,7,254 PRINT "x:" FOR i = 1 TO n PRINT x(i), NEXT i PRINT PRINT "y:" FOR j = 1 TO n PRINT y(j), NEXT j FOR i = 1 TO n - 1 PRINT SPC(6 * i); FOR j = 1 TO n - i y(j) = (y(j + 1) - y(j)) d(i, j) = y(j) PRINT y(j); SPC(10); NEXT j PRINT NEXT i s=0 FOR i = 1 TO n - 1 s = s + (1 / i) * d(i, 1) * (-1) ^ (i + 1) NEXT i d = s / (x(2) - x(1)) PRINT "First Derivation="; d END

The First Derivation= 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a quick-basic program to find the value of the integral 1 dt ∫0 (t 2 + 1) (3t 2 + 4) using Trapezoidal rule with n = 6

REM "Trapezoidal rule" CLS DEF fnf (t) = 1 / (SQR((t ^ 2 + 1) * (3 * t ^ 2 + 4))) INPUT "low level of integral"; a INPUT "high level of integral"; b INPUT "No. of sub-integral"; n h = (b - a) / n s=0 x=a FOR i = 1 TO n - 1 x=x+h s = s + fnf(x) NEXT i t = h * (fnf(a) / 2 + s + fnf(b) / 2) PRINT " Integration by Trapezoidal rule="; t END

Integration by Trapezoidal rule = .4016085 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a quick-basic program to find the value of the integral 1 dt ∫0 (t 2 + 1) (3t 2 + 4) using Simpson’s 1/3 rule with n = 6

REM "Simpson's 1/3 rule" CLS DEF fnf (t) = 1 / (SQR((t ^ 2 + 1) * (3 * t ^ 2 + 4))) INPUT "low level of integral"; a INPUT "high level of integral"; b INPUT "No. of sub-integral"; n h = (b - a) / n s1 = 0: s2 = 0 x=a FOR i = 2 TO n STEP 2 x = a + h * (i - 1) s1 = s1 + fnf(x) NEXT i FOR i = 3 TO n STEP 2 x = a + h * (i - 1) s2 = s2 + fnf(x) NEXT i PRINT s = (h / 3) * (fnf(a) + 4 * s1 + 2 * s2 + fnf(b)) PRINT " Integration by Simpson's 1/3 rule="; s END

Integration by Simpson's 1/3 rule=.4021834 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a quick-basic program to find the value of the integral 1 dt ∫0 (t 2 + 1) (3t 2 + 4) using Simpson’s 3/8 rule with n = 6

REM "Simpson's 3/8 rule" CLS DEF fnf (t) = 1 / (SQR((t ^ 2 + 1) * (3 * t ^ 2 + 4))) INPUT "low level of integral"; a INPUT "high level of integral"; b INPUT "No. of sub-integral"; n DIM y(n) h = (b - a) / n s=0 x=a FOR i = 0 TO n x = a + h * (i) y(i) = fnf(x) NEXT i FOR i = 1 TO (n / 2 - 1) s = s + y(3 * i - 3) + 3 * (y(3 * i - 2) + y(3 * i - 1)) + y(3 * i) NEXT i PRINT sim = (3 * h / 8) * s PRINT "Integration by Simpson's 3/8 rule="; sim END

Integration by Simpson’s 3/8 rule= 0.4021832 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a program in quick-basic to solve the differential equation y′ = e −2 x − 2 y using Euler method over [0,2] , with y(0) = 0.1 , let h = 0.1 (i.e. n = 20 ).

REM "Euler's Method" CLS INPUT "initial value"; a INPUT "final value"; b INPUT "No. of steps"; n DIM x(n), y(n) INPUT "y(0)=", y(0) x(0) = a h = (b - a) / n DEF fnf (x, y) = EXP(-2 * x) - 2 * y FOR i = 0 TO n - 1 x(i + 1) = x(i) + h y(i + 1) = y(i) + h * fnf(x(i), y(i)) NEXT i FOR i = 0 TO n PRINT x(i), y(i) NEXT i END

initial value? 0 final value? 2 No. of steps? 20 y(0)=? 0.1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

0.1 0.18 0.2258731 0.2477305 0.2530656 0.2473853 0.2346962 0.2178764 0.1989608 0.1793583 0.1600165 0.1415467 0.1243177 0.108526 9.424812E-02 0.0814795 0.0701623 6.020606E-02 5.150218E-02 4.393411E-02 3.738436E-02

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ −2 x − 2 y is: Exact solution for y′ = e y=

1 −2 e 10

x

− x e −2

x



y(2) = 0.03846284

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Problem: Write a program in quick-basic to solve the differential equation y′ = e −2 x − 2 y using Runge-Kutta method over [0,2] , with y(0) = 0.1 , let h = 0.1 (i.e. n = 20 ).

REM "Runge-Kutta Method" CLS INPUT "initial value"; a INPUT "final value"; b INPUT "No. of steps"; n DIM x(n), y(n) INPUT "y(0)=", y(0) x(0) = a h = (b - a) / n DEF fnf (x, y) = EXP(-2 * x) - 2 * y FOR i = 0 TO n - 1 x(i) = a + h * i k1 = h * fnf(x(i), y(i)) k2 = h * fnf(x(i) + h / 2, y(i) + k1 / 2) k3 = h * fnf(x(i) + h / 2, y(i) + k2 / 2) k4 = h * fnf(x(i) + h, y(i) + k3) y(i + 1) = y(i) + 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4) NEXT i FOR i = 0 TO n PRINT x(i), y(i) NEXT i END

initial value? 0 final value? 2 No. of steps? 20 y(0)=? 0.1

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

0.1 0.163744 0.2010928 0.2195209 0.2246607 0.2207241 0.2108327 0.1972747 0.1817045 0.1652969 0.1488672 0.1329626 0.11797324 0.1039823 9.121463E-02 7.965902E-02 0.0692956 6.007185E-02 5.191512E-02 4.474165E-02 3.846299E-02

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exact solution for y′ = e −2 x − 2 y is: 1 −2 x e − x e −2 x y(2) = 0.03846284 ⇒ 10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ y=

Related Documents


More Documents from ""