Let the mathematical function f defined by

$$f(x)=\frac{(2x^3+3)(x^2-1)}{\sqrt{3x^2+1}}$$

  • Write a C function that returns the value of f (x) for a point x passed as a parameter.
  • An approximation of the derivative f' of the function f is given in each point x for h small enough (close to 0), by: $$f'(x)\simeq\frac{f(x+h)-f(x)}{h}$$.
    Write a function C that calculates an approximation of the derivative f' of f at a point x entered on the keyboard. We will pass the value of h as a parameter of the function.
  • The second derivative of f is the derivative of the derivative. Write a function C that calculates an approximation of the second derivative f'' of f at a point x entered on the keyboard. We will pass the value of h as a parameter of the function.
  • Write a C function that determines the sign of the second derivative of f as a function of x. We can make a main program that reads x on the keyboard and displays the result.
  • Write a C function that gives the user the choice to display the value of the function f, its first derivative or its second derivative at a point x read from the keyboard.

Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>
#define size 100

double f(double x)
{
    return (2*pow(x,3)+3)*(pow(x,2)-1)/sqrt(3*pow(x,2)+1);
}

double fd(double x, double h)
{
    return (f(x+h)-f(x))/(h);
}

double fd2(double x, double h)
{
    return (fd(x+h,h)-fd(x,h))/(h);
}

int sign(double x, double h)
{
    return fd2(x,h)>=0?1:-1;
}


int main()
{
    double x, h=0.00001;
    printf("Enter x: ");
    scanf("%lf", &x);
    
    printf("f(%.4lf)=%.4lf\n",x,f(x));
    printf("f'(%.4lf)=%.4lf\n",x,fd(x,h));
    printf("f''(%.4lf)=%.4lf\n",x,fd2(x,h));
    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Length of the longest ascending sequence of characters