Derivative of nth power

please help me about my calculus project. the project is called Derivative of nth power and we will make a code of it. please help me to figure out the codes

Firstly, the purpose of such projects is that you do learn. You do learn by doing. You don't learn if someone else does the work. They don't learn either, because they have already invented bread and sliced wheel.

Second, pretend that we have no clue and explain in detail what this fabulous "Derivative of nth power" is.

Third, show the code that you have written. Then someone could point out whether/how it differs from what you did explain that it should do.
Very crudely, if you want to differentiate f(x) = x ^ p

d/dx = p * (x^ (p-1))

I think. been a long time since i had to do any calculus :)
In C++ one can consider a numerical approach to calculate the derivative of a function and, separately, an accurate assessment of it. So, a good approximation gives the formula:
f'(x0) = ( f(x0 + h) - f(x0 - h) ) / (2 * h) here h is an infinitesimal value.
So, for the function f(x) = x ^ n ( nth power of x) here is an attempt to do such a task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream> 

using namespace std;

double func(double, int);

int main () 
{
	int n;
	double x0;
	cout << "To compute the value of the derivative of f(x) = x ^ n  for x = x0, we need " << '\n';
	cout << "to enter n: ";
	cin >> n;
	cout << "and also to enter x0: ";
	cin >> x0;
	
	cout << fixed;
	cout.precision(6);
	
	double h = .000001;      // An infinitesimal change
	double x2 = x0 + h;	 // Pick a point to the right of x0
	double x1 = x0 - h;      // Pick a point to the left of x0
	double f2 = func(x2, n); // Evaluate function for x = x2
	double f1 = func(x1, n); // Evaluate function for x = x1
	double approxDerivative = (f2 - f1) / (2.0 * h); //Find the slope of the line through those two points.
	double exactValue = n * func(x0, n - 1); // Evaluate exact derivative f'(x) = n * x ^ (n - 1) for x = x0
	
	cout << "\nApprox. value = " << approxDerivative << '\n';
	cout << "Exact   value = " << exactValue << '\n';
	cout << "        Error = " << approxDerivative - exactValue << "\n\n";
	return 0;
}

double func(double x, int n)
{
	double f = 1.0;
	for(int i = 1; i <= n; ++i)
		f *= x;	// Evaluate f(x) = x*x*...*x   (alias f(x) = x ^ n)
	return f;
}

Some outputs:
1
2
3
4
5
6
7
To compute the value of the derivative of f(x) = x ^ n  for x = x0, we need 
to enter n: 5
and also to enter x0: 1.234

Approx. value = 11.593929
Exact   value = 11.593929
        Error = -0.000000

1
2
3
4
5
6
7
To compute the value of the derivative of f(x) = x ^ n  for x = x0, we need 
to enter n: 6
and also to enter x0: -2.3456

Approx. value = -426.011198
Exact   value = -426.011198
        Error = -0.000000

Hope this helps.
Last edited on
btw... thank you guys for your efforts :)
what about to the power of 3.2 or some sort of rational power? You may need to update your function or use the c++ pow :)
Well, nth power implies that n is a nonnegative integer <=> n belongs to N = {0, 1, 2, ...}.
In other cases is said in consequence, e.g.: let f(x) = xq where q belongs to Q = { q = m/n | where m, n != 0 are integers}.
Last edited on
n implies it is a variable named n. Most of the times it is used as an integer but who said it has to be a positive integer or even an integer at all?

Are you telling me this equation is wrong?

2n = 3
n = 1.5

According to you n would be either 2 or undefined

*fixed small typo
Last edited on
well, im applying it to my codes guys thanks for the help :)
@giblit
You are perfectly right. Thank you!
please help me about my Calculus project. the project is called Derivative of 1/u and we will make a code of it . thanks guys ^^ needed on monday
@KarlAngelo
Generally speaking the derivative of the function f(x) =1/u(x) is f'(x) =- u'(x)/u2(x) and here everything stops. For the problem to continue in C / C + + you must specify who is u (x), for ex.: u (x) = x or u (x) = x * exp (x) and so on. So be more specific with u(x).
@KarlAngelo

printf("Ambak nag Gmall Karl");

getch();
}
Topic archived. No new replies allowed.