# hw11_p01 - elseif i == n v(i) = (3.0*x(i) - 4.0*x(i-1) +...

This preview shows pages 1–2. Sign up to view the full content.

% To get a first derivatives at each point, % i = 1 --> Second order finite difference % i = 2 ~ n-1 --> Second order central difference % i = n --> Second order backward difference t = [0, 2, 4, 6, 8, 10, 12, 14, 16]; % Time (s) x = [0, 0.7, 1.8, 3.4, 5.1, 6.3, 7.3, 8.0, 8.4]; % Position (m) n = length(t); h = 2.0; k = 6; % index of t = 10 k % open file fid4 = fopen('hw11_p01_answer.txt','a'); % 'wt' means "write text" if (fid4 < 0) error('could not open file "hw11_p01_answer.txt"'); end; e % (a) centered finite-difference % velocity for i = k-2:k+2 if i == 1 v(i) = (-3.0*x(i) + 4.0*x(i+1) - x(i+2))/(2.0*h); elseif i == n v(i) = (3.0*x(i) - 4.0*x(i-1) + x(i-2))/(2.0*h); else v(i) = (x(i+1) - x(i-1))/(2.0*h); end end % acceleration a(k) = (v(k+1) - v(k-1))/(2.0*h); a fprintf(fid4, '(a)\ncentered finite difference\n'); fprintf(fid4, 'At t = %10.5f, v = %10.5f, a = %10.5f\n', t(k), v(k), a(k)); f % (b) forward finite-difference % velocity for i = k-2:k+2 if i == 1 v(i) = (-3.0*x(i) + 4.0*x(i+1) - x(i+2))/(2.0*h);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: elseif i == n v(i) = (3.0*x(i) - 4.0*x(i-1) + x(i-2))/(2.0*h); elseif i == n-1 v(i) = (x(i+1) - x(i-1))/(2.0*h); else v(i) = (-3.0*x(i) + 4.0*x(i+1) - x(i+2))/(2.0*h); end end % acceleration a(k) = (-3.0*v(k) + 4.0*v(k+1) - v(k+2))/(2.0*h); a fprintf(fid4, '(b)\nforward finite difference\n'); fprintf(fid4, 'At t = %10.5f, v = %10.5f, a = %10.5f\n', t(k), v(k), a(k)); f % (c) backward finite-difference % velocity for i = k-2:k+2 if i == 1 v(i) = (-3.0*x(i) + 4.0*x(i+1) - x(i+2))/(2.0*h); elseif i == 2 v(i) = (x(i+1) - x(i-1))/(2.0*h); elseif i == n v(i) = (3.0*x(i) - 4.0*x(i-1) + x(i-2))/(2.0*h); else v(i) = (3.0*x(i) - 4.0*x(i-1) + x(i-2))/(2.0*h); end end % acceleration a(k) = (3.0*v(k) - 4.0*v(k-1) + v(k-2))/(2.0*h); a fprintf(fid4, '(c)\nbackward finite difference\n'); fprintf(fid4, 'At t = %10.5f, v = %10.5f, a = %10.5f\n', t(k), v(k), a(k)); fclose(fid4);...
View Full Document

## This note was uploaded on 02/22/2010 for the course CHE 348 taught by Professor Chelikowsky during the Spring '08 term at University of Texas at Austin.

### Page1 / 2

hw11_p01 - elseif i == n v(i) = (3.0*x(i) - 4.0*x(i-1) +...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online