2

usually, symbolic functions return vectors for vector inputs:

syms('x');
f=x*2;
subs(f,[1 2 3])
outputs: [2 4 6]

but doing

f=sym('0');
subs(f,[1 2 3]);
outputs: 0
and not: [0 0 0]

so basically, my questions is how do I make f behave as a "normal" symbolic function. I can do something ugly like f=x-x to create a function that always returns zero, but is there a prettier way?

1
  • 2
    Just in case someone else stumbles upon this: Works as expected in R2013a. Commented Mar 18, 2013 at 7:36

2 Answers 2

1

Actually, there is no way.

sym('0') creates a symbolic constant (0, in this case). subs() replaces all variables with each value from the given vector. However, you have no variables, so subs() just returns the given symbolic constant.

It gets better. sym() internally does some simplification, so sym('0*x') or sym('x-x') both become sym('0') and you get the exact same behavior. Similarly, sym('x/x') turns into sym('1') and you just get the scalar 1 back from subs() even if you pass it a vector.

The only way you're going to get around this is by writing a helper function that detects if the size() of the output from subs() is less than the vector, and turns it into the correct size of a vector if needed.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you're wrong. I tried sym('x-x') and it did produce the desired effect.
@noam - I'm using R2009b, and verified this before I posted it. If you're using something older, you might not want to depend on that behavior remaining if it's possible you'll end up updating your install.
-1

I don't have the Symbolic Toolbox so this is a blind guess. Your function declarations don't look identical enough:

syms('x');
f=x*2;

against

f=sym('0');

Perhaps what you have done in the first case is define a function which returns the double of its inputs, in the second case defined a function which returns 0 whatever its inputs. Maybe

syms('x');
f=x*0;

is what you need ?

2 Comments

nope, it doesn't work. I suggest you delete this suggestion... :)
Well, I had thought that posting a blind guess would so provoke a thundering heard of SO Matlabbers that you would have been overwhelmed with right answers by now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.