


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: Nonlinear, Shooting, Algorithm, Approximate, Solution, Boundary, Value, Problem, Conditions, Subintervals, Tolerance, Iterations
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



% To approximate the solution of 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; number of % subintervals N; tolerance TOL; maximum number of iterations M. % % OUTPUT: Approximations W(1,I) TO Y(X(I)); W(2,I) TO Y'(X(I)) % for each I=0,1,...,N or a message that the maximum % number of iterations was exceeded. syms('OK', 'A', 'B', 'ALPHA', 'BETA', 'TK', 'AA', 'N'); syms('TOL', 'NN', 'FLAG', 'NAME', 'OUP', 'H', 'K', 'W1'); syms('W2', 'U1', 'U2', 'I', 'X', 'T', 'K11', 'K12', 'K21'); syms('K22', 'K31', 'K32', 'K41', 'K42', 'J', 's', 'x', 'y', 'z'); TRUE = 1; FALSE = 0; fprintf(1,'This is the Nonlinear Shooting 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 the \n'); fprintf(1,'next line followed by the partial of F with respect to \n'); fprintf(1,'z or y-prime on the next 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'); A = input(' '); B = input(' '); if A >= B fprintf(1,'Left endpoint must be less than right endpoint.\n'); else OK = TRUE; end; end; fprintf(1,'Input Y(%.10e).\n', A); ALPHA = input(' '); fprintf(1,'Input Y(%.10e).\n', B); BETA = input(' '); TK = (BETA-ALPHA)/(B-A); fprintf(1,'TK = %.8e\n', TK); fprintf(1,'Input new TK? Enter Y or N.\n'); AA = input(' ','s'); if AA == 'Y' | AA == 'y' fprintf(1,'input new TK\n'); TK = input(' ');
end; OK = FALSE; while OK == FALSE fprintf(1,'Input an integer > 1 for the number of subintervals.\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 SHOOTING METHOD\n\n'); fprintf(OUP, ' I X(I) W1(I) W2(I)\n'); % STEP 1 W1 = zeros(1,N+1); W2 = zeros(1,N+1); H = (B-A)/N; K = 1; % TK already computed OK = FALSE;
% Newton's method applied to improve TK TK = TK-(W1(N+1)-BETA)/U1; K = K+1; end; end; % STEP 11 % method failed if OK == FALSE fprintf(OUP, 'Method failed after %d iterations\n', NN); end; end; if OUP ~= 1 fclose(OUP); fprintf(1,'Output file %s created successfully \n',NAME); end;