# How to add function argument for nthroot() ?

3 views (last 30 days)
Life is Wonderful on 28 Nov 2021
Commented: Walter Roberson on 15 Dec 2021
Hi
I want to calculate nthroot() for 'embedded.fi', following is the code.
I don't understand , how to fix rem(n(:),2) ? / reminder after division
x = fi(1,0,32,31);
n = fi(1,0,32,31);
if isreal(x) || isreal(n)
nroot = nthroot(x, n);
end
Check for incorrect argument data type or missing argument in call to function 'rem'.

Error in nthroot (line 27)
if any((x(:) < 0) & (n(:) ~= fix(n(:)) | rem(n(:),2)==0))
rem(1,1) is 0

Walter Roberson on 28 Nov 2021
That fix you link to has no relationship to your current difficulty.
Data Types: single | double
Notice that fi is not listed. The nthroot() function is not defined for fixed point objects.
x = fi(1,0,32,31);
n = fi(1,0,32,31);
if isreal(x) && isreal(n)
nroot = sign(x) .* abs(x).^(1./n);
else
nroot = x .^ (1./n);
end
nroot
nroot =
1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 40 FractionLength: 31
Walter Roberson on 15 Dec 2021
This does not astonish me. When you have a series of expressions that are not being stored into a variable with defined fixed point characteristics, then the Fixed Point Toolbox dynamically expands the word length in order to try to preserve precision.
I suspect it might be possible to attach behaviour to the fi() so that the output results of expressions does not automatically expand for precision, but that is not something I have researched.
One approach would be to break up the expression into smaller segments that you assign into variables that you have set the precision for.
For example if you were to take children(T) then you would get a cell array of terms that are to be added together. You could then vertcat() the cell expansion to turn it into a vector, and you could compute the vector in the generated code, and fi() the results and sum() them.
You would still have some difficulty -- the individual term for the derivative to the 9th power would want to blow up the precision.
Probably it would be more rigourous to calculate the terms in a loop, applying fi() at each step.
But... really I think this approach is a dead end. Look at the precision loss near 0 or 1 !!!

R2021b

### Community Treasure Hunt

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

Start Hunting!