function handling and calling

1 view (last 30 days)
abhijeet singh
abhijeet singh on 21 Jan 2021
Edited: Walter Roberson on 25 Jan 2021
I have created two different function handles containing multiple variable. Say f1(x,t,H,T,bed_level) and f2(x,t,bed_level). In general f1 and f2 are general expressions. bed level is function of x and t. Now i called each function once for different expression of bed_level and stored in A and B.
Say A= f1(x,t,H,T,x*t) %for bed_level =x*t
and, B=f2(x,t,2*x) %for bed_level=2*x*t^2.
Now when i am multipling C= A.*x+B.*t, i am expecting a simplified and solved expression that is function of variables x,t,H,T. But Matlab is not producing a simplified one. It is writing it as it. For e.g (x*t)-t*(x) should cancel out each other but it is writing it as same (x*t)-t*(x)
Kindly help.
  4 Comments
abhijeet singh
abhijeet singh on 21 Jan 2021
@Sargondjani thanks for the reply too and recommending 'matlabFunction'
Sargondjani
Sargondjani on 22 Jan 2021
If you use H as an input, when creating a function handle, then it will treat H as a parameter (so the value of H(x)). I guess you need to explicitly calculatue H(x) inside A.
Im not really an expert on this, so I hope someone else steps in :/

Sign in to comment.

Answers (2)

abhijeet singh
abhijeet singh on 25 Jan 2021
Andrei Bobrov can you please look into it

Walter Roberson
Walter Roberson on 25 Jan 2021
Edited: Walter Roberson on 25 Jan 2021
For e.g (x*t)-t*(x) should cancel out each other but it is writing it as same (x*t)-t*(x)
There are two possibilities that can cause that:
1) one of the x or t is not the same variable as the other one. Symbolic variables are internally not global variables but rather local ones, and you can get a local copy of one returned. For example suppose you are in the middle of a function building a formula that involves
syms x positive
and you call myfun(x) that is
function r = myfun(y)
syms x negative
r = solve(x^2 + 5*y - 6)
You are passing in x assumed positive to myfun that receives it as y and works with x assumed negative. Is the x that is inside y the same x as syms x negative? If it is, then does adding the assumption of negative have the side effect of changing the assumption on the x that lives in the outer function? Or would there be an error about conflicting assumptions? Or would it treat it as two different variables that just happen to display the same way? To be honest, matlab sometimes makes some bad choices as to what actually happens, but inside the symbolic engine itself some of the choices are different and if you tickle it the right way then you can get different variables with the same display
2) one of the x or t might be a symbolic function but the other one might be regular variable. It is easier to provoke matlab into a clash, especially if you sym() or str2sym an expression.

Community Treasure Hunt

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

Start Hunting!