# How to add function argument for nthroot() ?

8 vues (au cours des 30 derniers jours)
Life is Wonderful le 28 Nov 2021
Commenté : Walter Roberson le 15 Déc 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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Walter Roberson le 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
##### 16 commentairesAfficher 14 commentaires plus anciensMasquer 14 commentaires plus anciens
Life is Wonderful le 14 Déc 2021
Modifié(e) : Life is Wonderful le 14 Déc 2021
still there is an issue!! codegen is unsuccessful
Please ignore the editor error since it is unsupported
syms x real
syms n integer
assumeAlso(n, 'positive');
T = taylor(x^(1/n), x, 'ExpansionPoint', 512, 'Order', 12);
Tfun = matlabFunction(T, 'vars', {x, n}, 'File', 'nthrootTaylor.m', 'optimize', false);
Tfun_opt = matlabFunction(T, 'vars', {x, n}, 'File', 'nthrootTaylor_opt.m', 'optimize', true);
x_example = fi(0,0,32,27); % x can be negative as well, but start with positive numbers
n_example = fi(0,0,32,30); % n can be negative as well, but start with positive numbers
codegen nthrootTaylor.m -args {x_example, n_example} ...
-o nthrootTaylor_fixedpoint_mex -config:mex
??? The computed word length of the result is 142 bits. This exceeds the maximum supported wordlength of 128 bits.
Unable to run the 'fevalJSON' function because it calls the 'codegen' function, which is not supported for this product offering.
codegen nthrootTaylor_opt.m -args {x_example, n_example} ...
-o nthrootTaylor_opt_fixedpoint_mex -config:mex
??? Function 'exp' is not defined for values of class 'embedded.fi'.
Walter Roberson le 15 Déc 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 !!!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by