Simbolic variables with hpf
Show older comments
I am writing a code that needs to evaluate big values of sinh and cosh without giving NaN. I was suggested to use hpf toolbox.
Now I would like to know if it is necessary to use symbolic variables to define all the variables or if it is enough if I put hpf inside THE argoment of sinh and cosh... I am building a series function of X and Y that includes ratios of sinh and cosh and I am currently doing it by the old fashioned for cycles and by defining arrays for x variable and Y variabile in order to get a Matrix of values of my function for each X-Y point. At the end its values in each point should be not extremely big values...
Apart from THE lenght of THE calculation I would like to know it I need to write a new script with symbolic variables or not.
11 Comments
Orlando Palone
on 17 May 2020
John D'Errico
on 17 May 2020
I don't understand. Use one or the other. But, no. You cannot mix HPF with SYM variables. But it is not clear why you think you need to use SYMs. Do you have an UNKNOWN symbolic variable? If so, then you need to use syms. You can have matrices of HPF numbers too.
Orlando Palone
on 17 May 2020
Edited: Orlando Palone
on 17 May 2020
Walter Roberson
on 17 May 2020
It is probably not sufficient to do that. You will probably need to create a small block of code that uses hpf, and you will need to explicitly convert the output values to double() .
It might be feasible to confine all of the hpf work to one function, but I suspect that you will need slightly more change than you outline.
Orlando Palone
on 17 May 2020
Walter Roberson
on 17 May 2020
You should not be switching back to double until after you have calculated the ratio.
Note that for sufficiently large x1, x2, then sinh(x1)/sinh(x2) is exp(x1)/exp(x2) to within any reasonable working precision, which is exp(x1-x2) .
Orlando Palone
on 17 May 2020
Walter Roberson
on 17 May 2020
double() applied to the hpf object.
John D'Errico
on 18 May 2020
Edited: John D'Errico
on 18 May 2020
Be very careful in converting numbers to double precision, if the exponent is too large, then you will get an overflow. The conversion to double will give you garbage, probably an inf.
A safer way to convert to a sym is
X = cosh(hpf(10000,50))
X =
4.4034091128314607936307480038222805017600020427958e4342
You cannot create a double from that, since it will overflow, since 10^4342 is well outside the dynamic range of a double. X lives in a 50 decimal digit representation. If we are careful, you can pass that into sym to convert it like this:
Y = sym(disp(X))
Y =
4.403409112831460793630748e+4342
vpa(Y,100)
ans =
4.403409112831460793630748003822280501760002042795800000000000000120636373104176091418844252285712053e+4342
class(Y)
ans =
'sym'
So Y is now a sym, that agrees with X to 50 decimal digits. By forcing VPA to go out to 100 digits, we got some extra garbage in there, but that is the fault of the SYM conversion, not due to HPF. The first 50 digits were as desired.
Orlando Palone
on 18 May 2020
John D'Errico
on 18 May 2020
Edited: John D'Errico
on 18 May 2020
Yes. You can in theory use a lower number of digits in HPF. I'm not sure if I'd go too short though, as then you might start seeing numerical problems. 15-20 digits is probably a good minimum.
X = hpf(1000000000,[18 2]);
Y = cosh(X)
Y =
4.00149088522032535e434294481
So X lives in 18 digits of precision, with some hidden digits for safe keeping. Does it save some time? A little, but not a huge amount.
X = hpf(1000000000,[50 2]);
timeit(@() cosh(X))
ans =
0.0114933514265
X = hpf(1000000000,[18 2]);
timeit(@() cosh(X))
ans =
0.0096059754265
So, by cutting the number of digits carried from 50 to 20, I managed roughly a 15% decrease in the time consumed for a call to cosh.
Don't try to convert it to a double of course. And you can't even go to a sym with that one. It actually overflows the sym class, resulting in a symbolic inf.
sym(disp(Y))
ans =
RD_INF
Answers (0)
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!