0

Suppose I have the following MATLAB code.

syms a b c1 c2

c1 = a + b + pi*b
c2 = a + b + 0.5*b

Then c1 gets evaluated to a + b + pi*b and c2 to a + (3*b)/2

However, I need MATLAB to calculate double precision values for the coefficients for a and b and not just symbolic numbers.

How do I do this? e.g. I want c1 to be evaluated as a + 4.1416*b and c2 as a+ 1.5*b

Also suppose I am interested in doing arithmetic like c1*c2, c1*5, I would like the coefficients of a and b to evaluated as double-precision numbers and not abstract symbolic expressions like fractions.

How should I do this?

smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

1 Answers1

1

Symbolic expressions cannot contain exactly “double precision” numbers, but you can get floating-point numbers:

c1 = a + b + vpa(pi)*b

or

c2 = vpa(a + b + 0.5*b)

Some computations, like solve or int, will still switch to exact numbers internally, but your results should always have the floating-point form.

You can change the precision used in the computations using digits:

digits(16) % double doesn't have more either
Christopher Creutzig
  • 8,656
  • 35
  • 45