function handling and calling

조회 수: 2 (최근 30일)
abhijeet singh
abhijeet singh 2021년 1월 21일
편집: Walter Roberson 2021년 1월 25일
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
abhijeet singh
abhijeet singh 2021년 1월 21일
@Sargondjani thanks for the reply too and recommending 'matlabFunction'
Sargondjani
Sargondjani 2021년 1월 22일
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 :/

댓글을 달려면 로그인하십시오.

답변 (2개)

abhijeet singh
abhijeet singh 2021년 1월 25일
Andrei Bobrov can you please look into it

Walter Roberson
Walter Roberson 2021년 1월 25일
편집: Walter Roberson 2021년 1월 25일
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.

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Preferences에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by