Statistical programming, matrix languages, and more

compute a formula

Reply
Grand Advisor
Posts: 9,333

compute a formula

I want compute such formula. How could I get it ?

10= 1.2*(1+ x ) +  1.2*(1+ x )**2 + 1.2*(1+ x )**3 + 1.2*(1+ x )**4

I want x= ?

Thanks.

SAS Employee
Posts: 94

Re: compute a formula

For a 4th degree poynomial there will be generally 4 complex solutions. Looks like the polyroot() function  is what you need, check the IML documentation. I suggest you first compute z = polyroot( {1.2 1.2 1.2 1.2 -10) to get the imaginary roots, then  since z = 1 + x, x = z-1, just subtract 1 from the first column of z to get x, i. e.

proc iml;

a = {1.2 1.2 1.2 1.2 -10};

z = polyroot(a);

x = z - {1 0,1 0,1 0,1 0};

print x;

Trusted Advisor
Posts: 1,192

Re: compute a formula

Hi,

If you take 1.2 as a common above equation becomes

10=1.2{(1+x) + (1+x)**2 + (1+x)**3 + (1+x)**4}

10 = 1.2[{1-(x+1)**5}/{1-(x+1)} as a result of geometric series in (1+x)

and more simplified form is

1.2(x+1)**5 - 10x - 1=0

Hope this helps.

Thanks

SAS Super FREQ
Posts: 3,234

Re: compute a formula

If you want only the real roots on a bounded interval, use the FROOT function. See A simple way to find the root of a function of one variable - The DO Loop

Grand Advisor
Posts: 9,333

Re: compute a formula

Thanks Hutch@sas stat@sas Rick Wicklin .  I just suppose SAS can give me a simple code to get X like Hutch@sas did .

Sorry .I can't mark it as answered , because I can't find that button .

Regrads

Xia Keshan

SAS Super FREQ
Posts: 3,234

Re: compute a formula

Here is the program from the blog post. I replaced the function in the post with the example that you give:

proc iml;
start Func(x);
   fx = 1.2*(1+ x) +  1.2*(1+ x)##2 + 1.2*(1+ x)##3 + 1.2*(1+ x)##4;
   return(fx - 10);  /* find x where f(x)=10 */
finish;

intervals = {-4 -1,         /* 1st interval [-4, -1] */
             -1  1};        /* 2nd interval [-1,  1] */
z = froot("Func", intervals);
print z;

Grand Advisor
Posts: 9,333

Re: compute a formula

Thanks Doctor Rick Wicklin ,

I think I should start to learn IML in near future .

I also can get it by brutally enumerate the X value . right ?

SAS Super FREQ
Posts: 3,234

Re: compute a formula

Well, sort of. If you want a DATA step solution, there are ways that are better than enumeration.  You can use the bisection method. Do an internet search and you'll find DATA step implementations of the bisection method, such as this one: http://analytics.ncsu.edu/sesug/2007/PO21.pdf   The key is that these algorithms find the "zeros" (or roots) so you want to solve for the roots of f(x)-10=0.

Grand Advisor
Posts: 9,333

Re: compute a formula

Thanks Doctor Rick Wicklin , and PG . I learn a lot from you guys.

Regards

Xia Keshan

Respected Advisor
Posts: 4,606

Re: compute a formula

You could hijack proc nlin to do the job:

data test;

y = 10;

run;

proc nlin data=test;

parms x 0;

model y = 1.2*(1+ x ) +  1.2*(1+ x )**2 + 1.2*(1+ x )**3 + 1.2*(1+ x )**4 ;

run;

PG

PG
Post a Question
Discussion Stats
  • 9 replies
  • 504 views
  • 0 likes
  • 5 in conversation