None.
Open in the MATLAB editor: d02ty_example
function d02ty_example fprintf('d02ty example results\n\n'); global a; % For communication with local functions % Initialize variables and arrays. neq = int64(1); nlbc = int64(1); nrbc = int64(1); ncol = int64(4); mmax = int64(2); m = int64([2]); tols = [1.0e-05]; % Set values for problem-specific physical parameters. a = 1.0; % Set up the mesh. nmesh = int64(6); mxmesh = int64(100); ipmesh = zeros(mxmesh, 1, 'int64'); mesh = zeros(mxmesh, 1); % Set initial mesh with constant spacing. dx = a/double(nmesh-1); mesh(1:nmesh) = [0:dx:a]; % Specify mesh end points as fixed. ipmesh(1) = 1; ipmesh(2:nmesh-1) = 2; ipmesh(nmesh) = 1; % d02tv is a setup routine to be called prior to d02tk. [work, iwork, ifail] = d02tv(... m, nlbc, nrbc, ncol, tols, nmesh, mesh, ipmesh); fprintf('\n Tolerance = %8.1e A = %8.2f\n\n', tols(1), a); % Call d02tk to solve BVP for this set of parameters. [work, iwork, ifail] = d02tk(... @ffun, @fjac, @gafun, @gbfun, @gajac, @gbjac,... @guess, work, iwork); % Call d02tz to extract mesh from solution. [nmesh, mesh, ipmesh, ermx, iermx, ijermx, ifail] = ... d02tz( ... mxmesh, work, iwork); % Output mesh results. fprintf(' Used a mesh of %d points\n', nmesh); fprintf([' Maximum error = %10.2e in interval %d for component %d\n\n', ... ' Mesh points:\n'], ermx, iermx, ijermx); for imesh = 1:nmesh fprintf( '%4d(%d) %6.4f', imesh, ipmesh(imesh), mesh(imesh)); if mod(imesh, 4) == 0 fprintf('\n'); end end % Output solution, and store it for plotting. xarray = zeros(nmesh, 1); yarray = zeros(nmesh, 2); fprintf('\n\n Computed solution\n'); fprintf(' x solution derivative\n'); for imesh = 1:nmesh % Call d02ty to perform interpolation on the solution. xx = mesh(imesh); [y, work, ifail] = d02ty(... xx, neq, mmax, work, iwork); fprintf(' %8.2f %10.5f %10.5f\n', xx, y(1,1), y(1,2)); xarray(imesh) = xx; yarray(imesh, :) = y(1, :); end % Plot results. fig1 = figure; display_plot(xarray, yarray); function [f] = ffun(x, y, neq, m) % Evaluate derivative functions (rhs of system of ODEs). f = zeros(neq, 1); if y(1,1) < 0 f(1,1) = 0; fprintf(' In ffun\n'); else f(1,1) = y(1,1)^1.5/sqrt(x); end function [dfdy] = fjac(x, y, neq, m) % Evaluate Jacobians (partial derivatives of f). dfdy = zeros(neq, neq, 2); if y(1,1) < 0 dfdy(1,1,1) = 0; fprintf(' In fjac\n'); else dfdy(1,1,1) = 1.5*sqrt(y(1,1))/sqrt(x); end function [ga] = gafun(ya, neq, m, nlbc) % Evaluate boundary conditions at left-hand end of range. ga = zeros(nlbc, 1); ga(1,1) = ya(1,1) - 1; function [dgady] = gajac(ya, neq, m, nlbc) % Evaluate Jacobians (partial derivatives of ga). dgady = zeros(nlbc, neq, 2); dgady(1,1,1) = 1; function [gb] = gbfun(yb, neq, m, nrbc) % Evaluate boundary conditions at right-hand end of range. gb = zeros(nrbc, 1); gb(1,1) = yb(1,1); function [dgbdy] = gbjac(yb, neq, m, nrbc) % Evaluate Jacobians (partial derivatives of gb). dgbdy = zeros(nrbc, neq, 2); dgbdy(1,1,1) = 1; function [y, dym] = guess(x, neq, m) % Evaluate initial approximations to solution components and derivatives. global a y = zeros(neq, 2); dym = zeros(neq, 1); y(1,1) = 1 - x/a; y(1,2) = -1/a; dym(1) = 0; function display_plot(x, y) % Plot both curves. [hline] = plot(x, y(:,1), x, y(:,2)); % Add title. title({['Thomas-Fermi Equation for Determining'],... ['Effective Nuclear Charge in Heavy Atoms']}); % Label the axes. xlabel('Relative Distance'); ylabel('Effective Nuclear Charge'); % Add a legend. legend('Nuclear Charge','Charge Gradient','Location','Best') % Set some features of the three lines. set(hline(1), 'Linewidth', 0.25, 'Marker', '+', 'LineStyle', '-'); set(hline(2), 'Linewidth', 0.25, 'Marker', 'x', 'LineStyle', '--');
d02ty example results Tolerance = 1.0e-05 A = 1.00 Used a mesh of 11 points Maximum error = 3.09e-06 in interval 1 for component 1 Mesh points: 1(1) 0.0000 2(3) 0.1000 3(2) 0.2000 4(3) 0.3000 5(2) 0.4000 6(3) 0.5000 7(2) 0.6000 8(3) 0.7000 9(2) 0.8000 10(3) 0.9000 11(1) 1.0000 Computed solution x solution derivative 0.00 1.00000 -1.84496 0.10 0.84944 -1.32330 0.20 0.72721 -1.13911 0.30 0.61927 -1.02776 0.40 0.52040 -0.95468 0.50 0.42754 -0.90583 0.60 0.33867 -0.87372 0.70 0.25239 -0.85369 0.80 0.16764 -0.84248 0.90 0.08368 -0.83756 1.00 0.00000 -0.83655