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');
, and both of them produce
f(x) = 1
(yes.. for every real x included 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!

 Akzeptierte Antwort

Star Strider
Star Strider am 20 Mai 2014

1 Stimme

I’m obviously missing something in your post. Wouldn’t (x/x)=1 for all x under any conditons?

4 Kommentare

epsilonxe
epsilonxe am 20 Mai 2014
Bearbeitet: epsilonxe am 21 Mai 2014
x/x is not equal to 1 when x is only zero.
Please consider the following
if f(x) = x/x then Domain of f is R-{0}.
if f(x) = 1 then Domain of f is R.
If x=0 then the function is undefined (or NaN in the IEEE standard), but you are defining the function here as a symbolic. The symbolic engine automatically simplifies it.
If you want it to behave in a mathematically correct way, create it as an ‘anonymous function’ instead. (These are allowed in the Symbolic Math Toolbox as well as the rest of MATLAB.) I did it here by hand, but you can use matlabFunction to do it for you.
For example:
syms x
f = @(x) x/x
y = f(0)
produces:
y =
NaN
as you want it to.
Thank you very much for your answers. This is what I want.
I missed the point that x/x is symbolically 1. Thanks again for this prompt.
Moreover I still have one more question:
Is there a method to avoid automatic simplification on "sym/syms" commands?
(just want to store the value "as is" via sym/syms)
My pleasure!
The various MATLAB Toolboxes still teach me about them. The occasional version differences keep it interesting.
I’m not aware of a way to automatically suppress it, since simplifying expressions is likely the reason most people use the various symbolic engines. I believe that it (and others like Maple and Mathematica) automatically simplifies to an extent.
I experimented just now with the assume command, and got a very interesting result:
syms f(x)
assume(x == 0)
f(x) = x/x
g = symfun(x/x, x)
In this experiment, f and g both evaluate to 1, even with x ‘set’ to zero. I suggest you report this as a bug.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sean de Wolski
Sean de Wolski am 21 Mai 2014

1 Stimme

MuPad can handle a function like x/x without automatic simplification. I'm not sure why you want this though. Why do you care about it, is it just for display or something else?

1 Kommentar

x/x is not equal to 1 when x is only zero.
Please consider the following:
Let f(x) = x/x and g(x) = 1 then domains of f and g are R - {0} and R respectively.
The automatic simplification in "sym/syms" may leads to lose some info.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by