how to get numerical solution for an equation

Hi everybody

i want a numerical solution for "p" in this equation and i don't know how to implement that!
the variables W, n, m are known and have initialized with 32, 3, 5.

thank you all in advanced.

http://i56.tinypic.com/2hzq2xl.png

p.s. : sorry for the link, i couldn't put the image in this post!
For exponentiation use pow() from <cmath>. Other than that, it's just your usual +-*/ operators.
Hi hamsterman

thank you for replay but its not as simple as you said, because as we can see in equation, the "p" variable exists in both side of equation and the c++ compiler can not understand that!
i want to ask you guys if there is a library or, i don't know, functions to do this.
closed account (iLUjLyTq)
Numerical methods depend on iterations to find a result to an equation. Each iterations returns you a more precise result. Variable 'p' on the right side of the equation is the approximation of the solution on the i-1 level. In short, you determine the new approximation with the help of a previous approximation. This requires to choose a start value for 'p' (i=0 level) to begin the approximating iterations i.e. your numerical method.
Look up Newton Raphson method.
thank you guys. it helped so much and problem solved!
this is the code:
1
2
3
4
5
6
7
8
9
10
while(treshold<Diff) {
	p = p_new;
	double fun_p  = p-1+pow(1-((1/W)*((2-(4*p))/(1-p-(p*pow(2*p,m))))),n);    // f(p)
	double fun_pp = 1-(((1-n)/(W))*(pow(1-((1/W)*((2-(4*p))/(1-p-(p*pow(2*p,m))))),n-1))*
		(( (-4)*(1-p-(p*pow(2*p,m)))-(-1-(pow(2*p,m)+ (pow(2,m)*m*pow(p,m-1))))) /
                (pow((1-p-(p*pow(2*p,m))),2))) );    // f'(p)

	p_new = p-((double)fun_p/fun_pp);         // Newton Raphson method
	Diff = p_new-p;
}
Last edited on
Assuming the function isn't badly behaved, instead of calculating the derivative analytically, you can do it numerically.

I would be a bit concerned about numerical instability in your calculation of the derivative, as it looks horrible, with lots of opportunities to add big numbers to small numbers and to divide by zero. An approximation like (f(x+h)-f(x-h))/2h may be a bit better.
Topic archived. No new replies allowed.