Simple symbolic object question

1 view (last 30 days)
Peter
Peter on 18 Nov 2013
Commented: Peter on 18 Nov 2013
Hi!
I have got a function:
function Iak= Ia(k)
syms A0;
if (even(k))
Iak=0;
else
Iak=(2*A0)/(k^2*pi^2);
end
end
even(k) is implemented well, returns 1 if k is even, 0 if it's not. I expected for (2*A0)/9.9896 as answer of Ik(1). But Matlab gives:
(562949953421312*A)/2778046668940015
Why? What is wrong with my function?

Accepted Answer

Walter Roberson
Walter Roberson on 18 Nov 2013
The Symbolic Engine automatically combines all numeric multipliers and dividers, so 2/9.9896 is calculated and shown in numeric form.
The Symbolic Engine automatically converts floating point values into rational values unless you take care otherwise, and "pi" is a floating point value.
I suggest,
function Iak= Ia(k)
syms A0;
if (even(k))
Iak = sym(0);
else
Iak = (2*A0)/(k^2*sym('pi')^2);
end
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!