Why do I receive an error about a SWITCH expression when using ODE15S within MATLAB?

3 views (last 30 days)
When solving a differential-algebraic system (DAE) via the command:
options = odeset('Mass', rand(3));
[t,z] = ode15s('odefun', TSPAN, X0, options);
where odefun is the derivative function, I receive the following error:
??? SWITCH expression must be a scalar or string constant.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This error is a result of using conflicting syntaxes from different versions of MATLAB. In this example, the derivative function is specified as a string, while the "Mass" property is specified as a constant mass matrix.
The abilities to set the "Mass" property of the option argument to a constant mass matrix and to use function handles for the derivative function were added in MATLAB 6.0 (R12).
In older versions, the derivative function was called using a string, and the acceptable settings for the "Mass" property were "none", "M", "M(t)", and "M(t, y)". The value for the mass matrix had to be returned by the derivative function called using the flag "mass" even for constant mass matrices.
In MATLAB 6.0 (R12) and later versions, you can specify a constant mass matrix as the value for the "Mass" property in the options argument. Because this new functionality is only available when using the new syntax, you must then call the ODE functions using a function handle rather than a string.
Therefore, the statement:
[t,z] = ode15s('odefun', TSPAN, X0, options);
should be changed to:
[t,z] = ode15s(@odefun, TSPAN, X0, options);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!