Q-Basic Numerical Analysis Programs
Q-Basic Numerical Analysis Programs
Sulaimani University
College of Science
Physics Department
(2009)
Prepared by
Dr. Omed Gh. Abdullah
Problem:
Write a program in Q-basic to solve the equation below, by using bi-
section 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:
f ( x) = x 2 − 4 sin( x) , xο = 3 , e = 0.005
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
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:
f ( x) = x 2 − 2 x − 3 , xο = 4 , e = 0.001
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
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:
f ( x) = x 2 − xy − 7 , (xο , yο ) = (3,4) , e = 0.001
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:
f ( x) = x 2 − x − 2 , xο = 3 , e = 0.001
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
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.
No. of Pairs? 4
X=? 3
X=3 y=20.21196
Problem: Write a program to find the Lagrangian Polynomial.
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 1 2 3 4 5
F(x) 0 1 4 6 10
X 1 2 3 4 5
F(x) 0 1 4 6 10
Y=-.171875
Problem:
Write a program to find the interpolated value for x = 3.4 , using Newton
forward method, for these tabulated data.
X 1 2 3 4 5
F(x) 13 15 12 9 13
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 1 2 3 4 5
F(x) 10 -9 -36 -41 30
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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sx=8.4
sxx=12.6
sy=53.1
sxy=75.57
a1=1.133333
a2=5.242064
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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A= 2.092605
C= 23.42711
S= 75.07242
Problem:
Write a program to determine the parameters C & D so that
f ( x ) = C x e , fits the following data in least squares sense.
Dx
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A= 1.279117
B= -.5697291
S= 16.41069
Problem:
Write a program to determine the parameters A & B so that
1
f ( x) =
( A x+B )
2 , fits the following data in least squares sense.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 1 2 3 4 5 6 7
y = x3 − 2 x2 + 2 x − 5
y -4 -1 10 35 80 151 254
t = h * (fnf(a) / 2 + s + fnf(b) / 2)
PRINT " Integration by Trapezoidal rule="; t
END
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Problem:
Write a quick-basic program to find the value of the integral
dt
∫0 (t 2 + 1) (3t 2 + 4) using Simpson’s 1/3 rule with n = 6
1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 ).
0 0.1
0.1 0.18
0.2 0.2258731
0.3 0.2477305
0.4 0.2530656
0.5 0.2473853
0.6 0.2346962
0.7 0.2178764
0.8 0.1989608
0.9 0.1793583
1 0.1600165
1.1 0.1415467
1.2 0.1243177
1.3 0.108526
1.4 9.424812E-02
1.5 0.0814795
1.6 0.0701623
1.7 6.020606E-02
1.8 5.150218E-02
1.9 4.393411E-02
2 3.738436E-02
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exact solution for y′ = e
−2 x
− 2 y is:
1 −2
y= e x
− x e −2 x
⇒ y(2) = 0.03846284
10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 ).
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.1 0.163744
0.2 0.2010928
0.3 0.2195209
0.4 0.2246607
0.5 0.2207241
0.6 0.2108327
0.7 0.1972747
0.8 0.1817045
0.9 0.1652969
1 0.1488672
1.1 0.1329626
1.2 0.11797324
1.3 0.1039823
1.4 9.121463E-02
1.5 7.965902E-02
1.6 0.0692956
1.7 6.007185E-02
1.8 5.191512E-02
1.9 4.474165E-02
2 3.846299E-02
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exact solution for y′ = e −2 x − 2 y is:
1 −2 x
y= e − x e −2 x ⇒ y(2) = 0.03846284
10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^