how to use str2func inside functions that are solved by ODE45
Show older comments
I need to input functions as strings to the odefun that will be solved by ODE45. Problem is that the odefun takes the form of a cell and therefore can't be passed to ODE45 anymore. Is there any workaround this?
As an example:
function dc = pinene_kin2(t,c,k)
dc = cell(5,1); %5 components
str1 = '- k(1)*c(1) + k(2)*c(2)';
str2 = '+ k(1)*c(1) - k(2)*c(2)';
tmp1 = str2func(['@(c,k) ' str1]);
tmp2 = str2func(['@(c,k) ' str2]);
dc{1} = @(c)tmp1(c,k);
dc{2} = @(c)tmp2(c,k);
This function when called through ODE45:
ourparams = [.59 .3];
[T,C] = ode15s(@(t,c) pinene_kin2(t,c,our_params),[0 4],[100 0]);
produces the following error: Error using odearguments (line 110) Inputs must be floats, namely single or double.
NOTE: writing the equation as it is recommended by matlab doc is not acceptable. the odefun should be generated by the strings and then turned to the form required by ODE solver.
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!