0% found this document useful (0 votes)
12 views2 pages

outputCode

The document is a MATLAB program that tests the order of 2D midpoint-rule surface integral approximation for a specific function. It calculates integral approximations using various discretization levels and records the results in output files for further analysis. The program computes errors relative to an exact integral value and outputs the results for visualization.

Uploaded by

esamech10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

outputCode

The document is a MATLAB program that tests the order of 2D midpoint-rule surface integral approximation for a specific function. It calculates integral approximations using various discretization levels and records the results in output files for further analysis. The program computes errors relative to an exact integral value and outputs the results for visualization.

Uploaded by

esamech10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

% program testorder

% Test the order of 2D midpoint-rule surface integral approximation.

% Function that is being integrated numerically using midpoint rule:


fi = @(x,y) -2.0*x + 3.0*(x-0.1).^2 - 7.0*x.^3 + (x-0.1).^5 + 6.0*y.^5 + x*y.^5;

% Open results files: for integral approximations by midpoint rule


fid1 = fopen('ointmidp', 'w');
fprintf(fid1, '# dx,sume,sumbd,sum2,sumq,sum4,esume,esumbd,esum2,esumq,esum4\n');

fid2 = fopen('ointerpol', 'w');


fprintf(fid2, '# dx,fifex,fifbd,fifcd2,fifq,fifcd4,ebd,ecd2,eq,ecd4\n');

% Integration length, dly; exact integral of analytic function over


% integration distance, sumex:
dly = 1.0;
dy = 2.0 * dly;
x0 = 0.0;
sumex = 1.02999;

% Test 6 levels of discretization: from dy=1 to dy=0.03125


ny = 1;
for k = 1:6
dy = 0.5 * dy;
dx = dy;
sume = 0.0;
sumbd = 0.0;
sumq = 0.0;
sum2 = 0.0;
sum4 = 0.0;

% Loop over all faces along integration distance


for i = 1:ny
% Face centroid coordinates
yf = 0.5 * dy + (i - 1) * dy;
xf = 0.0;

% X-coordinates of cell centroids on the left (P and W) and on the right


x1l = -0.5 * dx;
x2l = -1.5 * dx;
x1r = 0.5 * dx;
x2r = 1.5 * dx;

% Exact variable values at neighbor cell centroids


fi1l = fi(x1l, yf);
fi2l = fi(x2l, yf);
fi1r = fi(x1r, yf);
fi2r = fi(x2r, yf);

% Exact and approximate variable values at face centroid


fifex = fi(x0, yf);
fifbd = fi1l;
fifcd2 = 0.5 * (fi1l + fi1r);
fifq = 0.375 * fi1r + 0.75 * fi1l - 0.125 * fi2l;
fifcd4 = (9.0 * fi1l + 9.0 * fi1r - fi2l - fi2r) / 16.0;

% Compute contribution to the integral from the face "i"


sume = sume + fifex * dy;
sumbd = sumbd + fifbd * dy;
sum2 = sum2 + fifcd2 * dy;
sumq = sumq + fifq * dy;
sum4 = sum4 + fifcd4 * dy;
end

% Compute errors: percentage of the exact value


esume = 100 * abs((sumex - sume) / sumex);
esumbd = 100 * abs((sumex - sumbd) / sumex);
esumq = 100 * abs((sumex - sumq) / sumex);
esum2 = 100 * abs((sumex - sum2) / sumex);
esum4 = 100 * abs((sumex - sum4) / sumex);

% Write results for gnuplot evaluation - integral approximations


fprintf(fid1, '%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n', dx, sume, sumbd, sum2,
sumq, sum4, esume, esumbd, esum2, esumq, esum4);

ny = ny * 2;
end

fclose(fid1);
fclose(fid2);

You might also like