


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is solution to one of problems in Numerical Analysis. This is matlab code. Its helpful to students of Computer Science, Electrical and Mechanical Engineering. This code also help to understand algorithm and logic behind the problem. This code includes: Finite, Difference, Algorithm, Nonlinear, Approximations, Boundary, Value, Problem, Iterations, Tolerance, Partial
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



% To approximate the solution to the nonlinear boundary-value problem % % Y'' = F(X,Y,Y'), A<=X<=B, Y(A) = ALPHA, Y(B) = BETA: % % INPUT: Endpoints A,B; boundary conditions ALPHA, BETA; % integer N; tolerance TOL; maximum number of iterations M. % % OUTPUT: Approximations W(I) TO Y(X(I)) for each I=0,1,...,N+ % or a message that the maximum number of iterations was % exceeded. syms('OK', 'AA', 'BB', 'ALPHA', 'BETA', 'N', 'TOL', 'NN'); syms('FLAG', 'NAME', 'OUP', 'N1', 'H', 'I', 'W', 'K', 'X'); syms('T', 'A', 'B', 'D', 'C', 'L', 'U', 'Z', 'V'); syms('VMAX', 'J', 'x', 'y', 'z', 's'); TRUE = 1; FALSE = 0; fprintf(1,'This is the Nonlinear Finite-Difference Method.\n'); fprintf(1,'Input the function F(X,Y,Z) in terms of x, y, z\n'); fprintf(1,'followed by the partial of F with respect to y on \n'); fprintf(1,'the next line and the partial of F with respect \n'); fprintf(1,'to z = y-prime on the third line. \n'); fprintf(1,'For example: (32+2x^3-yz)/8 \n'); fprintf(1,' -z/8 \n'); fprintf(1,' -y/8 \n'); s = input(' ','s'); F = inline(s,'x','y','z'); s = input(' ','s'); FY = inline(s,'x','y','z'); s = input(' ','s'); FYP = inline(s,'x','y','z'); OK = FALSE; while OK == FALSE fprintf(1,'Input left and right endpoints on separate lines.\n'); AA = input(' '); BB = input(' '); if AA >= BB fprintf(1,'Left endpoint must be less than right endpoint.\n'); else OK = TRUE; end; end; fprintf(1,'Input Y( %.10e).\n', AA); ALPHA = input(' '); fprintf(1,'Input Y( %.10e).\n', BB); BETA = input(' '); OK = FALSE; while OK == FALSE fprintf(1,'Input an integer > 1 for the number of\n'); fprintf(1,'subintervals. Note that h = (b-a)/(n+1)\n'); N = input(' '); if N <= 1 fprintf(1,'Number must exceed 1.\n');
else OK = TRUE; end; end; OK = FALSE; while OK == FALSE fprintf(1,'Input Tolerance.\n'); TOL = input(' '); if TOL <= 0 fprintf(1,'Tolerance must be positive.\n'); else OK = TRUE; end; end; OK = FALSE; while OK == FALSE fprintf(1,'Input maximum number of iterations.\n'); NN = input(' '); if NN <= 0 fprintf(1,'Must be positive integer.\n'); else OK = TRUE; end; end; if OK == TRUE fprintf(1,'Choice of output method:\n'); fprintf(1,'1. Output to screen\n'); fprintf(1,'2. Output to text File\n'); fprintf(1,'Please enter 1 or 2.\n'); FLAG = input(' '); if FLAG == 2 fprintf(1,'Input the file name in the form - drive:\name.ext\n'); fprintf(1,'for example A:\OUTPUT.DTA\n'); NAME = input(' ','s'); OUP = fopen(NAME,'wt'); else OUP = 1; end; fprintf(OUP, 'NONLINEAR FINITE-DIFFERENCE METHOD\n\n'); fprintf(OUP, ' I X(I) W(I)\n'); % STEP 1 A = zeros(1,N); B = zeros(1,N); C = zeros(1,N); D = zeros(1,N); W = zeros(1,N); V = zeros(1,N); Z = zeros(1,N); U = zeros(1,N); L = zeros(1,N); N1 = N-1; H = (BB-AA)/(N+1); % STEP 2 for I = 1 : N
end; % STEP 13 % test for accuracy if VMAX <= TOL I = 0; fprintf(OUP, '%3d %13.8f %13.8f\n', I, AA, ALPHA); for I = 1 : N X = AA+I*H; fprintf(OUP, '%3d %13.8f %13.8f\n', I, X, W(I)); end; I = N+1; fprintf(OUP, '%3d %13.8f %13.8f\n', I, BB, BETA); OK = FALSE; else % STEP 18 K = K+1; end; end; % STEP 19 if K > NN fprintf(OUP, 'No convergence in %d iterations\n', NN); end; end; if OUP ~= 1 fclose(OUP); fprintf(1,'Output file %s created successfully \n',NAME); end;