# hw11_p04 - for i = 1:n sum_x = sum_x x(i sum_y = sum_y y(i...

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, 5, 10, 15, 20, 25]; % Time (s) T = [80 44.5 30.0 24.1 21.7 20.7]; % Position (m) n = length(t); h = 5.0; Ta = 20.0; T % Numerical differentiation with finite differences % velocity for i = 1:n if i == 1 T_prime(i) = (-3.0*T(i) + 4.0*T(i+1) - T(i+2))/(2.0*h); elseif i == n T_prime(i) = (3.0*T(i) - 4.0*T(i-1) + T(i-2))/(2.0*h); else T_prime(i) = (T(i+1) - T(i-1))/(2.0*h); end end e % Linear regression: y = a0 + a1*x x = T - Ta; y = T_prime; y sum_x = 0.0; sum_y = 0.0; sum_xy = 0.0; sum_x2 = 0.0; x_m = 0.0; y_m = 0.0;

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: for i = 1:n sum_x = sum_x + x(i); sum_y = sum_y + y(i); sum_xy = sum_xy + x(i)*y(i); sum_x2 = sum_x2 + x(i)*x(i); end x_m = sum_x/n; y_m = sum_y/n; y a1 = (n*sum_xy - sum_x*sum_y)/(n*sum_x2 - sum_x*sum_x); a0 = y_m - a1*x_m; a k = -a1; y_regressed = a0 + a1*x; plot (x, y, 'o', x, y_regressed); xlabel('T-Ta'); ylabel('dT/dt'); legend('Data', 'Regression'); l % open file fid4 = fopen('hw11_p04_answer.txt','a'); % 'wt' means "write text" if (fid4 < 0) error('could not open file "hw11_p04_answer.txt"'); end; e fprintf(fid4, '\na1 = %10.5f with an intercept %10.5f\n', a1, a0); fprintf(fid4, 'k = %10.5f with an intercept\n', 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.

### Page1 / 2

hw11_p04 - for i = 1:n sum_x = sum_x x(i sum_y = sum_y y(i...

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

View Full Document
Ask a homework question - tutors are online