How to convert symbolic piecewise expression into a function handle
Show older comments
I require to convert a symbolic piecewise expression to a function handle in matlab.
I tried using matlabFunction() but it throws a symengine error of unbalanced or unexpected parenthesis or bracket.
Any help will be highly appreciated.
6 Comments
Walter Roberson
on 6 Mar 2016
Please show your attempt to use matlabFunction .
Vishal Tripathi
on 21 Mar 2016
Edited: Vishal Tripathi
on 26 Mar 2016
Walter Roberson
on 21 Mar 2016
In
alpha2 =(subs(alpha1))
which variable are you trying to substitute, and what value are you trying to substitute for it? You have not created any numeric variables with the same name as symbolic variables so there would not be anything obvious for the subs() to do. I recommend always be specific about what you are substituting for and which new value is being substituted in.
I do not see any piecewise definition in what you posted.
Vishal Tripathi
on 26 Mar 2016
Walter Roberson
on 26 Mar 2016
Edited: Walter Roberson
on 26 Mar 2016
There is no piecewise if you add the assumption that T >= 300
I recommend you use a different name for the upper bound, such as
syms Tmax
assume(Tmax>300)
fun = int(exp(-E/(8.314*T)),T,300,Tmax);
Now by the time you get to error1, you have a function in which both z and v are free variables. Is it correct that you are trying to minimize over both variables simultaneously?
Vishal Tripathi
on 26 Mar 2016
Edited: Vishal Tripathi
on 26 Mar 2016
Answers (1)
Walter Roberson
on 27 Mar 2016
0 votes
If you write it out with the final integration step not done, and you expand() the terms and combine exponents and simplify, you can show that the minimum must occur when z is the mean of the alphaexp.
Near v = 0 and E = 18000, the integrals are very steep. The key point is near v = 0.002486920643673647643722704 where just a hair less gets you an integral that is so large that it effectively cannot be computed, but a hair more gets you an integral that is amazingly small -- for example values on the order of 1/10^(10^13). I think it means that the minimum is going to be at your upper bound, v < 6000 but the difference between that and any v > 0.002486920643673647643722704 would need to be computed to billions of digits to determine the difference. The inherent inaccuracy in your constants such as 8.314 do not justify those kinds of computations.
4 Comments
Vishal Tripathi
on 27 Mar 2016
Edited: Vishal Tripathi
on 6 Apr 2016
Walter Roberson
on 6 Apr 2016
Let me put this more firmly: your system is not doing the integration because your formula is too complex to integrate. It also cannot do effective numeric integration because your slope is too steep. You need to go back and reason about what is happening, like I did, rather than trying for an automatic solution that is not practical to achieve.
Vishal Tripathi
on 6 Apr 2016
Walter Roberson
on 6 Apr 2016
Please do not name a variable "error" as that interferes with using the critical MATLAB function error() and makes it difficult for other people to read the code.
Please show the output of error1 and also the output of the matlabFunction call.
I have a vague memory that there might be a bug in the conversion of int() to function handle.
Categories
Find more on Assumptions 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!