


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: Continuation, Method, Systems, Algorithm, Nonlinear, Approximation, Initial, Solution, Iterations, Functions
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



% To approximate the solution of the nonlinear system F(X)=0 given % an initial approximation X: % % INPUT: Number n of equations and unknowns; initial approximation % X=(X(1),...,X(n)); number of RK4 steps N. % % OUTPUT: Approximate solution X=(X(1),...,X(n)) or a message % that the number of iterations was exceeded. syms('OK', 'N', 'I', 'J', 'P', 'NN', 'X','ZZ'); syms('FLAG', 'NAME', 'OUP', 'K', 'A', 's', 'ss','mm'); syms('KK1', 'I1', 'Z1', 'IR1', 'IA1', 'J1', 'C1', 'L1', 'JA1'); syms('K1','K2','K3','K4','X1','KK','H','b'); TRUE = 1; FALSE = 0; fprintf(1,'This is the Continuation Method for Nonlinear Systems.\n'); fprintf(1,'The functions could be input or defined in code.\n'); fprintf(1,'This code assumes input of functions - see \n'); fprintf(1,'comments in code for alternate version.\n'); fprintf(1,'This program also uses M-files JAC.M and FN.M \n'); fprintf(1,'If the number of equations exceeds 7 then JAC.M\n'); fprintf(1,'and FN.M must be changed.\n'); OK = FALSE; while OK == FALSE fprintf(1,'Input the number n of equations.\n'); N = input(' '); if N >= 2 OK = TRUE; else fprintf(1,'N must be an integer greater than 1.\n'); end; end; A = zeros(N,N+1); K1 = zeros(4,N); X = zeros(1,N); Y = zeros(1,N); X1 = zeros(1,N); b = zeros(1,N); mm = zeros(1,4); mm(1) = 0.5; mm(2) = 0.5; mm(3) = 1.0; mm(4) = 0; % Define components of F as follows: s(1) = '3y1-cos(y2y3)-0.5'; s(2) = 'y1^2-81(y2+0.1)^2+sin(y3)+1.06'; s(3) = 'exp(-y1y2)+20y3+(10pi-3)/3'; % for I = 1 : N % fprintf(1,'Input the function F_(%d) in terms of y1 ... y%d \n' ,I ,N); % s(I) = input(' ','s'); % end; % for I = 1 : N % for J = 1 : N
% fprintf(1,'Input the partial of F_(%d) with respect to x_%d \n',I,J); % fprintf(1,'in terms of y1, ..., y%d \n',N); % ss((I-1)N+J) = input(' ','s'); % end; % end; % Define the entries of the Jacobian in row major ordering. ss(1) = '3'; ss(2) = 'y3sin(y2y3)'; ss(3) = 'y2sin(y2y3)'; ss(4) = '2y1'; ss(5) = '-162(y2+0.1)'; ss(6) = 'cos(y3)'; ss(7) = '-y2exp(-y1y2)'; ss(8) = '-y1exp(-y1y2)'; ss(9) = '20'; OK = FALSE; while OK == FALSE fprintf(1,'Input the number of RK4 steps.\n'); NN = input(' '); if NN > 0 OK = TRUE; else fprintf(1,'Must be a positive integer.\n'); end; end; for I = 1 : N fprintf(1,'Input initial approximation X(%d).\n', I); X(I) = input(' '); end; fprintf(1,'Select output destination\n'); fprintf(1,'1. Screen\n'); fprintf(1,'2. Text file\n'); fprintf(1,'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; % STEP 1 H = 1/NN; for I = 1 : N ZZ = -HFN(I,N,X,s); b(I) = ZZ; end; % STEP 2 K = 1; while OK == TRUE & K <= NN % STEPS 3 - 6 for I = 1 : N X1(I) = X(I);
for L1 = JA1 : N C1 = C1-A(J1,L1)Y(L1); end; Y(J1) = C1/A(J1,J1); end; end; end; if OK == FALSE fprintf(1,'Linear system is singular\n'); end; if OK == TRUE for I = 1 : N K1(KK,I) = Y(I); X1(I) = X(I) + mm(KK)K1(KK,I); end; KK = KK + 1; end; end; % STEP 7 if OK == TRUE for I = 1 : N X(I) = X(I) + (K1(1,I)+2K1(2,I)+2K1(3,I)+K1(4,I))/6; end; fprintf(OUP, ' %2d', K); for I = 1 : N fprintf(OUP, ' %11.8f', X(I)); end; fprintf(OUP, '\n'); end; K = K + 1; end; % STEP 8 if OUP ~= 1 fclose(OUP); fprintf(1,'Output file %s created successfully \n',NAME); end;