How to Properly Use VPA in MATLAB?

125 views (last 30 days)
BM
BM on 1 Jun 2018
Commented: Ameer Hamza on 1 Jun 2018
I have a couple of questions regarding vpa in MATLAB. MATLAB states that " You must wrap all inner inputs with vpa, such as exp(vpa(200)). Otherwise the inputs are automatically converted to double by MATLAB®." I wanted to first ask about nested vpa statements.
Question 1: Say I had something like
log(1 + exp(ix)) (1)
and I wanted greater accuracy for the value of
1 + exp(ix) (2)
Given MATLAB's previous statement, do I need to use nested vpa statements like
log(vpa(1+vpa(exp(vpa(ix)))) (3)
or would higher accuracy calculations for statement (2) above be attained by simply writing the code
log(1+exp(vpa(ix))) ? (4)
Would the statements (3) and (4) be equivalent? In other words, to use vpa, do I only have to wrap the innermost argument in a line with vpa, even if I am interested chiefly in the vpa operation identified by statement (2)?
-------------
Question 2: My next question concerns previously defined variables and vpa. Say I had something like
sigma = 1 + exp(cos(q+r));
alpha = 1 + exp(sin(q+r));
.
.
.
result = 1 + exp(sigma + alpha);
and lets say I want to need vpa for the result exp(sigma), would I be fine with modifying the last line of the code above to read
result = 1 + exp(vpa(sigma+alpha));
or must I use vpa in the code where I define sigma bearing it too has functions with inner arguments?
-----------------
Question 3: Bearing I wrap the innermost argument by vpa, there is no other circumstance where my calculations would be converted back to double by MATLAB, correct?
-----------------
Question 4: Could I scatter vpa arguments in places throughout a calculation, or would this be a mistake in how vpa should be utilized?
Thank you for bearing through this list of questions!

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 Jun 2018
1) Both (3) and (4) are equivalent, but (4) is more readable and easy to write. You just need to use vpa for innermost argument. vpa converts the number to a symbolic expression. After that, MATLAB uses symbolic toolbox to evaluate the outer expression.
2) Using vpa at the end will not increase accuracy. As you mentioned about the evaluation in floating point precision. The previous expressions will be evaluated as floating point, therefore they will lose precision. Using vpa at the end will have no effect. But it also does not mean that you need to add vpa in expressions for sigma, alpha etc. you can just define p and q as symbolic at beginning and rest of calculation will be done in the symbolic form. For example
p = vpa(p);
q = vpa(q);
3) you will need to explicitly convert the value form symbolic form to numerical form.
4) don't scatter them randomly. Just define lower level variables as symbolic expression and rest of the calculation will automatically be done in symbolic form,

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!