Untitled
unknown
plain_text
2 years ago
815 B
14
Indexable
#include <stdio.h>
#include <math.h>
// Function to calculate the derivative of f(x) = 2x^2 + 1 using central difference
double derivative(double x, double h) {
double result;
// Central difference formula: f'(x) ˜ (f(x + h) - f(x - h)) / (2 * h)
result = (2 * pow(x + h, 2) + 1 - (2 * pow(x - h, 2) + 1)) / (2 * h);
return result;
}
int main() {
double x, h, result;
// Input the value of x
printf("Enter the value of x: ");
scanf("%lf", &x);
// Input the step size (h)
printf("Enter the step size (h): ");
scanf("%lf", &h);
// Calculate the derivative
result = derivative(x, h);
// Output the result
printf("The derivative of f(x) = 2x^2 + 1 at x = %.2lf is approximately: %.4lf\n", x, result);
return 0;
}
Editor is loading...
Leave a Comment