restart: # PIECEWISE LINEAR RAYLEIGH-RITZ ALGORITHM 11.5 # # To approximate the solution of the boundary-value problem # # -D(P(X)Y')/DX + Q(X)Y = F(X), 0 <= X <= 1, # Y(0) = Y(1) = 0, # # with a piecewise linear function: # # INPUT: integer N: mesh points X(0) = 0 < X(1) < ... # < X(N) < X(N+1) = 1 # # OUTPUT: coefficients C(1),...,C(N) of the basis functions SIMPSON := proc(FN,A,B) local H, I1, Y, Z, simpson: H := (B-A)/4: for I1 from 0 to 4 do Y := A+I1*H: if FN = 1 then Z[I1] := (4-I1)*I1*H*H*QQ(Y): fi: if FN = 2 then Z[I1] := (I1*H)*(I1*H)*QQ(Y): fi: if FN = 3 then Z[I1] := (H*(4-I1))*(H*(4-I1))*QQ(Y): fi: if FN = 4 then Z[I1] := P(Y): fi: if FN = 5 then Z[I1] := I1*H*F(Y): fi: if FN = 6 then Z[I1] := (4-I1)*H*F(Y): fi: od: simpson := (Z[0]+Z[4]+2*Z[2]+4*(Z[1]+Z[3]))*H/3: RETURN(simpson): end: print(`This is the Piecewise Linear Rayleigh-Ritz Method.`): print(`Input F(X), Q(X), and P(X) in terms of x separated by a space.`): print(`For example:`): print(`2*3.141592654^2*sin(3.141592654*x) 3.141592654^2 1`): F := scanf(`%a`)[1]: QQ := scanf(`%a`)[1]: P := scanf(`%a`)[1]: print(`F(x) = `):print(F):print(`Q(x) = `):print(QQ):print(`P(x) = `):print(P): F := unapply(F,x): QQ := unapply(QQ,y): P := unapply(P,z): print(` `):print(`X(0), ..., X(N+1) are to be supplied.`):print(` `): print(`Are the preparations complete? Answer 1 for yes or 2 for no.`):print(` `): AA := scanf(`%d`)[1]: OK := FALSE: if AA = 1 then OK := FALSE: while OK = FALSE do print(`Input integer N where X(0) = 0, X(N+1) = 1.`): N := scanf(`%d`)[1]:print(`N = `):print(N): if N <= 1 then print(`N must be greater than one.`): else OK := TRUE: fi: od: X[0] := 0: X[N+1] := 1: print(`Choice of method to input X(1), ..., X(N):`): print(`1. Input from keyboard at the prompt`): print(`2. Equally spaced nodes to be calculated`): print(`3. Input from text file`): print(`Please enter 1, 2, or 3.`): FLAG := scanf(`%d`)[1]: print(`Input is `):print(FLAG): if FLAG = 2 then HC := 1/(N+1): for J from 1 to N do X[J] := evalf(J*HC): H[J-1] := HC: od: H[N] := HC: else if FLAG = 3 then print(`Has the input file been created? `): print(`Enter 1 for yes or 2 for no.`): AA := scanf(`%d`)[1]: print(`Input is `):print(AA): if AA = 1 then print(`Enter the input file name using the format`): print(` - drive:\134\134name.ext,`): print(`for example: A:\134\134DATA.DTA`): NAME := scanf(`%s`)[1]: print(`Input file is `):print(NAME): INP := fopen(NAME,READ,TEXT): for J from 1 to N do X[J] := evalf(fscanf(INP, `%f`)[1]): od: for J from 0 to N do H[J] := X[J+1]-X[J]: od: fclose(INP): else print(`The program will end so that the input `): print(`file can be created.`): OK := FALSE: fi: else for J from 1 to N do print(`Input X(j) for j =`):print(J): X[J] := evalf(scanf(`%f`)[1]): print(`Input is `):print(X[J]): H[J-1] := X[J]-X[J-1]: od: H[N] := X[N+1]-X[N]: fi: fi: else print(`The program will end so that the preparations`): print(`can be completed.`): OK := FALSE: fi: # Step 1 is done in the input procedure. if OK = TRUE then N1 := N-1: # Step 3 for J from 1 to N1 do Q[0,J-1] := SIMPSON(1,X[J],X[J+1])/((H[J])*(H[J])): Q[1,J-1] := SIMPSON(2,X[J-1],X[J])/((H[J-1])*(H[J-1])): Q[2,J-1] := SIMPSON(3,X[J],X[J+1])/((H[J])*(H[J])): Q[3,J-1] := SIMPSON(4,X[J-1],X[J])/((H[J-1])*(H[J-1])): Q[4,J-1] := SIMPSON(5,X[J-1],X[J])/H[J-1] : Q[5,J-1] := SIMPSON(6,X[J],X[J+1])/H[J]: od: Q[1,N-1] := SIMPSON(2,X[N-1],X[N])/((H[N-1])*(H[N-1])): Q[2,N-1] := SIMPSON(3,X[N],X[N+1])/((H[N])*(H[N])): Q[3,N-1] := SIMPSON(4,X[N-1],X[N])/((H[N-1])*(H[N-1])): Q[3,N] := SIMPSON(4,X[N],X[N+1])/((H[N])*(H[N])): Q[4,N-1] := SIMPSON(5,X[N-1],X[N])/H[N-1]: Q[5,N-1] := SIMPSON(6,X[N],X[N+1])/H[N]: # Step 4 for J from 1 to N1 do ALPHA[J-1] := Q[3,J-1]+Q[3,J]+Q[1,J-1]+Q[2,J-1]: BETA[J-1] := Q[0,J-1]-Q[3,J]: B[J-1] := Q[4,J-1]+Q[5,J-1]: od: # Step 5 ALPHA[N-1] := Q[3,N-1]+Q[3,N]+Q[1,N-1]+Q[2,N-1]: B[N-1] := Q[4,N-1]+Q[5,N-1]: # Steps 6 - 10 solve a tridiagonal linear system using Algorithm 6.7 # Step 6 A[0] := ALPHA[0]: ZETA[0] := BETA[0]/ALPHA[0]: Z[0] := B[0]/A[0]: # Step 7 for J from 2 to N1 do A[J-1] := ALPHA[J-1]-BETA[J-2]*ZETA[J-2]: ZETA[J-1] := BETA[J-1]/A[J-1]: Z[J-1] := (B[J-1]-BETA[J-2]*Z[J-2])/A[J-1]: od: # Step 8 A[N-1] := ALPHA[N-1] - BETA[N-2] * ZETA[N-2]: Z[N-1] := (B[N-1]-BETA[N-2]*Z[N-2])/A[N-1]: # Step 9 C[N-1] := evalf(Z[N-1]): for J from 1 to N1 do J1 := N - J: C[J1-1] := evalf(Z[J1-1]-ZETA[J1-1]*C[J1]): od: print(`Choice of output method:`): print(`1. Output to screen`): print(`2. Output to text file`): print(`Please enter 1 or 2.`): FLAG := scanf(`%d`)[1]: print(`Input is `):print(FLAG): if FLAG = 2 then print(`Input the file name in the form - drive:\134\134name.ext`): print(`for example A:\134\134OUTPUT.DTA`): NAME := scanf(`%s`)[1]: print(`Output file is `):print(NAME): OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: fprintf(OUP, `PIECEWISE LINEAR RAYLEIGH-RITZ METHOD\134n\134n`): fprintf(OUP, ` I X(I-1) X(I) X(I+1) C(I)\134n\134n`): for J from 1 to N do fprintf(OUP, `%3d %11.8f %11.8f %11.8f %13.8f\134n`, J, X[J-1],X[J], X[J+1], C[J-1]): od: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`): fi: fi: SVNUaGlzfmlzfnRoZX5QaWVjZXdpc2V+TGluZWFyflJheWxlaWdoLVJpdHp+TWV0aG9kLkc2Ig== SWluSW5wdXR+RihYKSx+UShYKSx+YW5kflAoWCl+aW5+dGVybXN+b2Z+eH5zZXBhcmF0ZWR+Ynl+YX5zcGFjZS5HNiI= SS1Gb3J+ZXhhbXBsZTpHNiI= SVMyKjMuMTQxNTkyNjU0XjIqc2luKDMuMTQxNTkyNjU0KngpfjMuMTQxNTkyNjU0XjJ+MUc2Ig== SShGKHgpfj1+RzYi LCQtSSRzaW5HNiQlKnByb3RlY3RlZEdJKF9zeXNsaWJHNiI2IywkSSJ4R0YoJCIrYUVmVEohIiokIisiKTMjUig+ISIp SShRKHgpfj1+RzYi JCIrL1dncCkqISIq SShQKHgpfj1+RzYi JCIjNSEiIg== SSJ+RzYi SUZYKDApLH4uLi4sflgoTisxKX5hcmV+dG9+YmV+c3VwcGxpZWQuRzYi SSJ+RzYi SWduQXJlfnRoZX5wcmVwYXJhdGlvbnN+Y29tcGxldGU/fkFuc3dlcn4xfmZvcn55ZXN+b3J+Mn5mb3J+bm8uRzYi SSJ+RzYi SUxJbnB1dH5pbnRlZ2Vyfk5+d2hlcmV+WCgwKX49fjAsflgoTisxKX49fjEuRzYi SSVOfj1+RzYi IiIq SUtDaG9pY2V+b2Z+bWV0aG9kfnRvfmlucHV0flgoMSksfi4uLix+WChOKTpHNiI= SUYxLn5+SW5wdXR+ZnJvbX5rZXlib2FyZH5hdH50aGV+cHJvbXB0RzYi SUoyLn5+RXF1YWxseX5zcGFjZWR+bm9kZXN+dG9+YmV+Y2FsY3VsYXRlZEc2Ig== STkzLn5+SW5wdXR+ZnJvbX50ZXh0fmZpbGVHNiI= STlQbGVhc2V+ZW50ZXJ+MSx+Mix+b3J+My5HNiI= SSpJbnB1dH5pc35HNiI= IiIj STlDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDpHNiI= STQxLn5PdXRwdXR+dG9+c2NyZWVuRzYi STcyLn5PdXRwdXR+dG9+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SSpJbnB1dH5pc35HNiI= IiIi PIECEWISE LINEAR RAYLEIGH-RITZ METHOD I X(I-1) X(I) X(I+1) C(I) 1 0.00000000 0.10000000 0.20000000 0.31028648 2 0.10000000 0.20000000 0.30000000 0.59019996 3 0.20000000 0.30000000 0.40000000 0.81234055 4 0.30000000 0.40000000 0.50000000 0.95496359 5 0.40000000 0.50000000 0.60000000 1.00410814 6 0.50000000 0.60000000 0.70000000 0.95496359 7 0.60000000 0.70000000 0.80000000 0.81234055 8 0.70000000 0.80000000 0.90000000 0.59019996 9 0.80000000 0.90000000 1.00000000 0.31028648 JSFH