

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: Range, Kutta, Fehlberg, Algorithm, Approximate, Solution, Local, Truncation, Error, Tolerance, Stepsize, Initial, Condition
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


syms('F', 'OK', 'A', 'B', 'ALPHA', 'TOL', 'HMIN', 'HMAX', 'FLAG'); syms('NAME', 'OUP', 'H', 'T', 'W', 'K1', 'K2', 'K3', 'K4', 'K5', 'K6'); syms('R', 'DELTA', 't', 's'); TRUE = 1; FALSE = 0; fprintf(1,'This is the Runge-Kutta-Fehlberg Method.\n'); fprintf(1,'Input the function F(t,y) in terms of t and y\n'); fprintf(1,'For example: y-t^2+1 \n'); s = input(' ','s'); F = inline(s,'t','y'); 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 the initial condition\n'); ALPHA = input(' '); OK = FALSE; while OK == FALSE fprintf(1,'Input tolerance\n'); TOL = input(' '); if TOL <= 0 fprintf(1,'Tolerance must be a positive.\n'); else OK = TRUE; end; end; OK = FALSE; while OK == FALSE fprintf(1,'Input minimum and maximum mesh spacing on separate lines.\n'); HMIN = input(' '); HMAX = input(' '); if HMIN < HMAX & HMIN > 0 OK = TRUE; else fprintf(1,'Minimum mesh spacing must be a positive real\n');
fprintf(1,'number and less than the maximum mesh spacing\n'); 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, 'RUNGE-KUTTA-FEHLBERG METHOD\n\n'); fprintf(OUP, ' T(I) W(I) H R\n\n'); % STEP 1 H = HMAX; T = A; W = ALPHA; fprintf(OUP, '%12.7f %11.7f 0 0\n', T, W); OK = TRUE; % STEP 2 while T < B & OK == TRUE % STEP 3 K1 = HF(T,W); K2 = HF(T+H/4,W+K1/4);K3 = HF(T+3H/8,W+(3K1+9K2)/32); K4 = HF(T+12H/13,W+(1932K1-7200K2+7296K3)/2197); K5 = HF(T+H,W+439K1/216-8K2+3680K3/513-845K4/4104); K6 = HF(T+H/2,W-8K1/27+2K2-3544K3/2565+1859K4/4104-11K5/40); % STEP 4 R = abs(K1/360-128K3/4275-2197K4/75240.0+K5/50+2K6/55)/H; % STEP 5 if R <= TOL % STEP 6 % Approximation accepted T = T+H; W = W+25K1/216+1408K3/2565+2197K4/4104-K5/5; % STEP 7 fprintf(OUP, '%12.7f %11.7f %11.7f %11.7f\n', T, W, H, R); end; % STEP 8 % To avoid underflow if R > 1.0E- DELTA = 0.84 * exp(0.25 * log(TOL / R)); else DELTA = 10.0; end; % STEP 9 % Calculate new H if DELTA <= 0.