Symbolic dependency apparently sometimes gives wrong answers

Hello,
I have a simple question but could not figour out how to tackle it by myself. Consider the following:
syms p1 y
f=[p1*y;p1];
hasSymType(f,'symfunDependingOn',y)
0
0
I am not happy with this since clearly I expected to get the answer [1,0]. I do not know why it behaves like this!!!
Please not that my actual problem are much more complicated and this is just to explain to you my question. So, please do not give me any answer which
just works for this simple example, rather I would like a generic sort of answer.
Any idea?
thanks in advance!
Babak

 Accepted Answer

Not a bug. Your first element is a symbolic expression involving the desired variable, but symfundependingon is for unresolved symbolic functions that have the variable as a parameter.
syms f(x) g(x, y) h(y)
expressions involving f or g would be symfun depending on x, but h would not be.

7 Comments

Hi Walter,
I do not get what you say. Trying to understand what you mentioned I decided to run the following
>> syms y p1 f(y)
>> f(y)=[p1*y;p1];
>> hasSymType(f,'symfunDependingOn',y)
ans =
2×1 logical array
0
0
I do not understand this!!! what is this??? So, based on what you mentioned here I have a 'symbolic function' not 'symbolic expression'.
???
syms f(x)
y = [x, f]
hasSymType(y, 'symfunDependingOn', x)
ans =
1×2 logical array
0 1
To match the test the expression must involve an unresolved symbolic variable name that has the given variable in one of its arguments
yes: f(x), f(x+3), f(x*y), g(x)
no: f(y), f(y+3), f(z*y), sin(x)
sin(x) is a known symbolic function, but the test is looking for names that are not known to be mathematical functions but which are being used in function form involving the variable.
An example of where you would use this would be in applying the chain rule for differentiation. If you have the name of a function not involving the variable then the derivative is 0. If you have a known mathematical function involving the variable then you need to dispatch to the logic for that function. If you have an unknown function of the variable then you need to drop in a diff() form as part of the output.
Well, I am sure there has been a good reason matlab developers decided to do so, as you mentioned chan rule is one example. But, I think I do not like this and will stick to my own code to tackle this problem. However, I have a suggestion and if you find it useful please add a new command in MATLAB.
I a (simple) mathematician. John D'Errico seems also to be a mathematician whose level is much higher than me. Both of us had inconveniences with this. People need to know the functional dependecies of ANYTHING in an easy and intuitive manner and when they google it they like to find the proper matlab command as fast as possible without having a great knowledge of mathematics. ANYTHING can be a 'symbolic expression', a 'symbolic function', 'a symbolic vector', a 'symbolic matrix', a 'symbolic cell', whatever. For instance, imagine we want to check dependency in variable 'x'. if E=[x y f(x,y)] then I expect to get the answer [1 0 1]. If E=[1 2 x^2;sin(x*y) x z] then I expect to get the answer [0 0 1;1 1 0].
Well, you are the boss!
Thanks for your time Walter!
Babal
To officially submit an enhancement request, please contact Technical Support directly using the Contact Support link under the Get Support heading at the end of this page.
Hi Steven,
Thanks!
I am going to contact technical support. But, a short question. Does all the concversations here have a 'reference number', whatever, so that I can mention it and they can see all the details? I prefer to make a short message and refer them to our discussions. This way I do not need to repeat all I wrote here and that they get a much better understanding on the core of problem, how we think, how their coleagues think, etc.?
Babak
If you click on the three dots at the right side of the question, an answer, or a comment one of the options is Link. This will give you a hyperlink you can send to Support that the Support staff can use to see the discussion that's taken place here.
For example, if you click on this link you should be directed to the comment you made on John D'Errico's answer.
I coontacted them!
Thanks Steven!
And, have a great weekend!

Sign in to comment.

More Answers (2)

To be honest, this appears to be more of a bug report than anything else, since that option in hasSymType does not apprear to be working as per the documentation. So I would report it as such, but Answers is not the correct place to report bugs.
Could you find some other solution that would work? Even if someone could find something that works here, it does not get past the fact that hasSymType is the correct tool to solve your specidfic problem, and it should work as per the help.

1 Comment

Hi John,
Thanks!
Of course, I have my alternative way to tackle this issue (I had to write a rather complicated function which
finds the functional dependencies. I first convert my symbolic expression to a character or string and then detect functional dependencies using character manipulations). But, I would be happier when MATLAB fixes this bug.
Thanks again,
Babak

Sign in to comment.

Hi Mohammad,
This works, at least for the examples cited in this comment. Haven't tested for anything but those examples.
syms x y z f(x,y)
F = @(expr,var) cellfun(@(s) any(has(s,var)),arrayfun(@(s) findSymType(s,'variable'),expr,'UniformOutput',false));
E = [x y f(x,y)] % then I expect to get the answer [1 0 1].
E = 
F(E,x)
ans = 1×3 logical array
1 0 1
E =[1 2 x^2;sin(x*y) x z] % then I expect to get the answer [0 0 1;1 1 0].
E = 
F(E,x)
ans = 2×3 logical array
0 0 1 1 1 0

1 Comment

Hi Paul,
This is great!
I hope it works always and is not limitted to some case examples.
Thanks!
Babak

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!