title: Using solve instead of vpasolve to speed up calculations in Matlab?
body: <p>I am relatively new to Matlab. At the moment I am solving an equation using vpasolve. Since that needs to loop over 80 variables, it is slow. </p>
  
  <pre><code>% input variables
  % ffsample = Fourier transformed reference waveform
  % ffref = Fourier transformed reference waveform
  
  len=80;
  n1=1;
  n2=1.95;
  alpha_grad=30;
  beta_grad=14.858;
  
  % constants
  tp12=2.*n1.*cos(alpha)./(n2.*cos(alpha)+n1.*cos(beta));
  tp21=2.*n2.*cos(beta)./(n1.*cos(beta)+n2.*cos(alpha));
  
  %functions with unknown n3
  syms n3
  gamma= asin(n2.*sin(beta)./n3);
  rp23 = (n3 .* cos(beta) - n2.*cos(gamma)) ./ (n3 .* cos(beta) + n2 .* cos(gamma));
  
  t = cputime;
  res_para=zeros(len,1);
  
  %loop to solve for n3
  digits(5):
  for i=1:len
      res_para(i)=vpasolve(ffsample(i)./ffref(i) == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
  end
  
  e = cputime-t
  
  %convert results to complex numbers
  res_para=double(res_para);
  res_para=squeeze(res_para);
  res_para=res_para';
  </code></pre>
  
  <p>Now, I am trying to convert this into an equation which uses solve before entering the loop. As far as I understand it, I should use solve in conjunction with vpa? At the moment I am trying like the following code, but it wouldn't start:</p>
  
  <pre><code>res_p=solve(x == (tp12*tp21*rp23) ./ (tp12*tp21), n3);
  
  t = cputime;
  res_para=zeros(len,1);
  
  for i=1:len
  
      x=ffsample(i)./ffref(i);
      res_para=vpa(res_p(x));
  end
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <h3>Update</h3>
  
  <p>Thank you for your replies. I changed the digits(5) position. 
  And yes, digits(5) is a very low value indeed however, it speeds teh above code up by approximately 3 times. </p>
  
  <p>As I said, I am relatively new to matlab and thought that solving the equation symbolically first and then just insert the variables would greatly reduce the time, but maybe I am wrong here.</p>
  
  <p>Considering I use vpasolve to solve a very simple equation numerically:</p>
  
  <pre><code>function [ xval ] = example(  )
  syms x
  for y=1:10
      xval(y) = vpasolve(y == 5.*x+6, x);
  end
  </code></pre>
  
  <p>Wouldn't it be faster if I solve the function y=5x+6 symbolically for x first and then just include the msising values in the loop? Something like this:</p>
  
  <pre><code>function [ xval ] = example2(  )
  syms x
  % symbolic solve for x (???)
  % y=5x+6 ==&gt; x=(y-6)/5
  sol=solve( y == 5.*x+6, x)
  
  for y=1:10
      xval(y) = sol(y);
  end
  </code></pre>
  
  <p>Obviously example2 does not work. </p>