if want solve variable (p) form following equation, else known :
is there method out there in java let me this?
i can calculator, , sure there out there python - know doable. thank you.
here quick reference graph:
the x-value circle (p) satisfies both sides of equation. calculating values, checking 0 not efficient way of doing this.
you have implement function giving difference of both sides of equation , kind of univariate solver. due convexity of equation on positive axis, classical methods, esp. secant method, should work flawlessly.
class myfunc { public myfunc(int nn, int cc, double aalpha) {...} public double eval(double p) { double sum = 1; double term = 1; for(int = 1; i<=c; i++) { term *= (n*p)/i; sum += term; } return sum*math.exp(-n*p) - alpha; } } .............. public double secant(myfunc f, double a, double b) { double fa = f.eval(a); double fb = f.eval(b); while(math.abs(b-a)>1e-10) { double c = (a*fb-b*fa)/(fb-fa); a=b; fa = fb; b=c; fb = f.eval(b); } return b; }
and call like
p = secant(new myfunc(n,c,alpha), 0, 0.1);
it turns out secant method unstable, use modified regula falsi
import java.lang.*; interface realfunc { public double eval(double x); } class myfunc implements realfunc { int c,n; double alpha; public myfunc(int nn, int cc, double aalpha) { c=cc; n=nn; alpha = aalpha; } public double eval(double p) { double sum = 1; double term = 1; for(int = 1; i<=c; i++) { term *= (n*p)/i; sum += term; } return sum*math.exp(-n*p) - alpha; } } public class secantsolverso34980366 { static public double illinois(realfunc f, double a, double b) { double fa = f.eval(a); double fb = f.eval(b); while(math.abs(b-a) > 1e-10) { //system.out.printf("a:f(%16.12f) = %16.12f | b: f(%16.12f) = %16.12f \n ",a,fa,b,fb); double c = (a*fb-b*fa)/(fb-fa); double fc = f.eval(c); if( fa*fc < 0 ) { fa *= 0.5; } else { = b; fa = fb; } b = c; fb = fc; } return b; } public static void main(string[] args) { int n = 1; for(double alpha = 0.2; alpha <=1.0001; alpha += 0.1) { system.out.printf("alpha=%4.2f: ", alpha); for(int c = 2; c < 15; c+=2) { double p = illinois(new myfunc(n,c,alpha), 0.,(c+13.0)/n); system.out.printf("(c=%2d):%12.9f ",c,p); } system.out.printf("\n"); } } }
Comments
Post a Comment