Deciding when to call a Matlab function: Within my symbolic math code, or within my converted numerical code?

46 次查看(过去 30 天)
Hi there!
I wrote a symbolic math code and used the algebraic solver solve( ) to get the equations that I want. I then converted these equations to Matlab functions, using matlabFunction( ) and wrote these converted equations to a separate file. Then I have a file with a full system of numerical equations. And, last but not least, I have a file which will solve these numerical equations, using, say, ode45( ). My equations are not solvable analytically using dsolve( ).
My question is: At some point in my workflow, I have to call certain specific models / functions, which I've defined as Matlab functions, in separate .m-files. If I call a numerical Matlab function within my very first symbolic-math script, then must the function I'm calling also be symbolic? Is it perhaps better to call my numerical Matlab functions later in the workflow, such as after the conversion from symbolic to numerical? What is the best practice? I suspect that it is not converting numerical Matlab functions to symbolic functions; that does not seem sensible to do, but I could be wrong. (E.g. there are lots of parameters within each Matlab numerical function that are decimal representations, but perhaps this isn't really important.)
Thanks in advance!

采纳的回答

Walter Roberson
Walter Roberson 2024-11-18,23:49
Typically you compute in two phases:
  1. Symbolic-only phase. Everything that is changeable is generalized as a symbolic variable. The final outputs are written to files by using matlabFunction. This all might be in a seperate design session.
  2. Numeric-only phase, using the functions produced above. This might well be in a different session, even within an executable produced by MATLAB Compiler.
Sometimes this needs to be varied a bit. There are cases where the symbolic engine is able to integrate or solve() when a variable is given a specific numeric value, but not able to handle the general case. In such a situation, sometimes you need to fetch the specific numeric value first, then enter the symbolic phase, and then the numeric phase.
  4 个评论
Paul
Paul 2024-11-19,2:54
I've always thought it best to delay numerical evaluation as long as possible to give the SMT maximum opportunity to simplify expressions. But having said that one does need to guard against situations such as
syms big1 big2 big3
x = big1*big2/big3;
x might be a reasonsably sized number and would be found as such using VPA with numeric values for big*, but converting x to a matlabFunction and then evaluating with doubles for big* could result in overflow.
I'd be curious to see an example where an SMT function works better with a numeric value in a symbolic expression.
Noob
Noob about 13 hours 前
Hi Walter and Paul!
After some thought, perhaps it is best to call a numerical model later in my workflow, when things are numerical and not symbolic, perhaps for a fundamental reason: The symbolic math toolbox is meant for, say, symbolic differentiation, integration, and solving equations. By the way, I use none of the symbolic differentiation, integration or dsolve; I am simply using its algebraic-solving capabilities, using the function solve( ). In addition, symbolic functions aren't meant to take inputs, while numerical Matlab functions do require one to specify the inputs. So, if I call a numerical model early in my workflow, within my symbolic file, and that numerical model has lots of parameters encoded as decimal representations, then it's sort of like writing a symbolic code that has inputs specified, if that makes sense. And, perhaps that isn't the best practice. If you have any more comments, please feel free to share.
Thanks!

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by