epsilonxe
epsilonxe

Reputation: 69

How can I define f(x) = x/x in MATLAB (Symbolic Computation)

I try with these commands

x = sym('x');
f(x) = sym('f(x)');
f(x) = x/x;

and

f(x) = sym('x/x');

, but both of them produce

f(x) = 1

(yes.. for every real x including 0)

The question is how I can avoid the pre-evaluation in the command "sym", or there exists another way to handle this problem.

Thank you very much!

update 21.05.2014:

Let me describe the problem a little bit.

Consider

f(x) = x/x

and

g(x) = 1

It is obvious that domains of f and g are R-{0} and R respectively.

The automatic simplification in sym/syms may lead to lose some info.

Upvotes: 0

Views: 632

Answers (2)

horchler
horchler

Reputation: 18484

The answer from @pabaldonedo is good. It seems that the designers of MuPad and the Symbolic Toolbox made a choice as x/x is indeterminate.

If you actually want 0, Inf, -Inf, or NaN, to result in NaN rather than 1 then you can use symbolic variables in conjunction with an anonymous function:

f = @(x)sym(x)./sym(x);
f([-Inf -1 0 1 Inf NaN])

which returns

ans =

[ NaN, 1, NaN, 1, NaN, NaN]

Or, if the input is already symbolic you can just use this:

f = @(x)x./x;
f(sym([-Inf -1 0 1 Inf NaN]))

Upvotes: 1

pabaldonedo
pabaldonedo

Reputation: 957

There is no pre-evaluation in your code. F(x) = x/x is always 1, even for x = 0, Matlab is just simplifying how the function is expressed but there is no pe-evaluation.

I think you should have a look to indeterminate forms to understand why for x = 0, x/x = 1. Have a look to wikipedia: http://en.wikipedia.org/wiki/Indeterminate_form

Upvotes: 1

Related Questions