Question: Matlab code help! Use fourth order accurate centered finite difference formulas for the second derivative to compute the derivative directly from the data. Use second
Matlab code help!
Use fourth order accurate centered finite difference formulas for the second derivative to compute the derivative directly from the data. Use second order forward difference formulas for the first two points and second order backward difference formulas for the last two points.
Use MATLAB's gradient function twice on the data.
Use MATLAB's gradient function twice on interpolated data generated with a clamped spline with a derivative set to 0 at both ends and 0.5 inch increments in x.
The function should have three column vector outputs:
1. The bending moment values M(x) calculated using 4th order centered finite difference formulas.
2. The bending moment values M(x) calculated using the gradient function on the data.
3. The bending moment values M(x) calculated using the gradient function on the clamped spline interpolation of the data.
Only output 2 is correct.
Here is my code:
function [M_Oh4_FD, M_gradient, M_grad_spline] = student_solution(beam_deflection_data)
% Constants
E = 29.0E6; % psi
I = 156; % in^4
% Extract data
x = beam_deflection_data(:, 1);
v = beam_deflection_data(:, 2);
% Calculate h
h = x(2) - x(1);
n = length(v);
v_diff2 = zeros(n, 1);
% 2nd order forward difference for first two values
v_diff2(1) = (-v(4) + 4*v(2) - 5*v(2) + 2*v(1))/(h^2);
v_diff2(2) = (-v(5) + 4*v(4) - 5*v(3) + 2*v(2))/(h^2);
% 2nd order backward difference for last two values
v_diff2(n-1) = (2*v(n-1) - 5*v(n-2) + 4*v(n-3) - v(n-4))/(h^2);
v_diff2(n) = (2*v(n) - 5*v(n-1) + 4*v(n-2) - v(n-3))/(h^2);
% Calculate second derivative using 4th order centered finite difference scheme
for i = 3:n-2
v_diff2(i) = (-v(i+2) + 16*v(i+1) - 30*v(i) + 16*v(i-1) - v(i-2)) / (12*h^2);
end
M_Oh4_FD = (E*I)*v_diff2;
% Compute second derivative using gradient function on the data
dvdx = gradient(v, h);
d2vdx2 = gradient(dvdx, h);
M_gradient = (E*I)*d2vdx2;
% Compute M_grad_spline using clamped spline interpolation
x_interp = x(1):0.5:x(end);
v_clamp = zeros(n+2, 1);
v_clamp(2:end-1) = v;
cs = spline(x, v_clamp, x_interp);
dvidx = gradient(cs, h);
d2vidx2 = gradient(dvidx, h);
M_grad_spline = (E*I)*d2vidx2;
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
