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

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!

 Accepted Answer

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 Comments

Hi Walter!
First, thanks as always for your help. If I call a numerical Matlab function pretty early in my workflow, say, within my symbolic-math script, and the algebraic solver solve( ) gives me a set of equations that matlabFunction() then writes to a separate file, can I safely assume that things are ok, and that I can proceed to solve my converted numerical equations with, say, ode45( )? Or is it too big an assumption? The symbolic equations that solve( ) gives me are quite long, many pages long, so checking each term for accuracy isn’t really feasible, I don’t think.
Sounds okay.
Caution:
When you use matlabFunction() and specify writing to a file, then the default is that Optimize is turned on. Historically, optimization has been broken. I do not know whether optimization has been repaired. It is safer to turn optimize off when you matlabFunction() writing to a file.
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.
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!

Sign in to comment.

More Answers (0)

Products

Release

R2024b

Asked:

on 18 Nov 2024

Commented:

on 19 Nov 2024

Community Treasure Hunt

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

Start Hunting!