Q.1
(a) Generate data with the function y = ax3+ bx2+ cx + d in the range of x as it
varies from 0 to 2 with step increase of h . Assign suitable values of constants a, b, c and d.
Also assume the increment h to any value in between 0.1 to 0.25.
(b) Using the data obtained in (a), determine [dy/dx]x=p and [d2y/dx2]x=q
. Assign suitable values of p and q in the range 0.1 to 0.6.
(c) Compare your result with the original one and make a conclusion.
a = 1;
b = 2;
c = 3;
d = 4;
h = 0.1;
x = 0:h:2;
y = a*x.^3 + b*x.^2 + c*x +d;
p = 0.5;
i = 6;
dy_dx = (y(i+1) - y(i-1))/ (2*h);
dy_dx_ex = 3*a*p.^2 + 2*b*p + c;
fprintf("Approximation to dy/dx at x=%.1f is %.4f\n", x(i), dy_dx);
fprintf("Exact value of dy/dx at x=%.1f is  %.4f\n", p, dy_dx_ex);
q = 0.3;
i = 4;
d2y_dx2 = (y(i+1) - 2*y(i) + y(i-1)) / h^2;
d2y_dx2_ex = 6*a*q + 2*b;
fprintf("Approximation to d2y/dx2 at x=%.1f is %.4f\n", x(i), d2y_dx2);
fprintf("Exact value of d2y/dx2 at x=%.1f is  %.4f\n", q, d2y_dx2_ex);
Finite difference approximate first and second derivatives with accuracy proportional to h^2
Comments
Leave a comment