Does the use of symbolic variable slows down running time?

25 ビュー (過去 30 日間)
J_stat
J_stat 2019 年 6 月 5 日
コメント済み: J_stat 2019 年 6 月 18 日
I wrote a code to find zeros of f using Newton Raphson Method.
Is it possible to slow down the running time if I use symbolic variables like below? Or is there anything else in my code that slows down the speed?
x=3;n=25; alp=0.05, del=0.000001;
syms a k;
f=1-alp/2-.5*nchoosek(n,x)*a^x*(1-a)^(n-x) - symsum(nchoosek(n,k)*a^k*(1-a)^(n-k), k, 0, x-1); %Enter the Function here
g=diff(f,a); %The Derivative of the Function
plb0 = input('Enter the intial approximation of lower bound:');
tic
for i=1:1000000000
f0=vpa(subs(f,a,plb0)); %Calculating the value of function at initial value
f0_der=vpa(subs(g,a,plb0)); %Calculating the value of function derivative at initial value
plb=plb0-f0/f0_der; % The Formula, Newton Raphson Method
err=abs(plb-plb0);
if err<del %checking the amount of error at each iteration
break
end
plb0=plb;
end
t1= toc
i
  1 件のコメント
J_stat
J_stat 2019 年 6 月 18 日
I changed the code to remove symsum as follows.
Now it just running forever. Where should I chage reduce running time?
x=3;n=25; alp=0.05, del=0.000001;
plb0 = input('Enter the intial approximation of lower bound:');
tic
for i=1:1000000000
f0=1-alp/2-.5*binopdf(x,n,plb0)-binocdf(x-1,n,plb0); %Calculating the value of function at initial value
f0_der= -.5*binopdf(x,n,plb0)*((x-n*plb0)/(plb0*(1-plb0))) -nck_der(plb0,n,x); %Calculating the value of function derivative at initial value
plb=plb0-f0/f0_der; % The Formula, Newton Raphson Method
err=abs(plb-plb0);
if err<del %checking the amount of error at each iteration
break
end
plb0=plb;
end
t1= toc
i
%%%%%%%%%%% As separate m file in the same directory%%%%%%%%%%
function S = nck_der(p0,n,x); %To find the last part of the derivative of function f0.
S=n*(1-p0)^n;
for k=1:x-1;
S=S+binopdf(k,n,p0)*((k-n*p0)/(p0*(1-p0)));
end
end

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 6 月 5 日
Yes, use of symbolic calculations is almost always slower than the equivalent pure numeric routines.
There are exceptions. For example sometimes the symbolic engine is able to find a closed form integral in situations that are numerically "bumpy" and so require a lot of numeric function evaluations for the numeric integration routine to feel comfortable that it has successfully estimated the integral.
Most finite symsum are better replaced by evaluating the function at each point and using sum() on the vector of results.
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 6 月 18 日
It is not obvious to me at the moment that the two versions are mathematically equivalent. They might be the same, but the route to equivalence is not coming to my mind at the moment.
J_stat
J_stat 2019 年 6 月 18 日
I beleive they are the same mathematically because they are pdf and cdf of binomial distribution. I took derivative of cdf and pdf in the derivaties with respect to probability of binomial distribution.
Other than math, I am curious if having symbolic variables can make it slower than having for loop.
Thank you for looking at the code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by