How can I retrieve an element from this array of functions, as a matlabFunction?
1 view (last 30 days)
Show older comments
y = @(t,y) [10*(y(2)-y(1)), y(1)*(28-y(1))-y(2), y(1)*y(2)-8*y(2)/3, (y(1)+y(2))^2, (y(1)+y(2)^4)/(5+y(1)*y(2))];
I want to have that
z = y(1) = 10*(y(2)-y(1))
but y(1) already throws an error
0 Comments
Accepted Answer
Stephen23
on 19 Mar 2018
Edited: Stephen23
on 19 Mar 2018
Use a cell array to store function handles:
C{1} = @(t,y) 10*(y(2)-y(1));
C{2} = @(t,y) y(1)*(28-y(1))-y(2);
C{3} = @(t,y) y(1)*y(2)-8*y(2)/3;
C{4} = @(t,y) (y(1)+y(2))^2;
C{5} = @(t,y) (y(1)+y(2)^4)/(5+y(1)*y(2));
And use it like this:
z = C{1};
z(tval,yval)
or even simply:
C{1}(tval,yval)
I note that none of your functions use the first input t : is this intentional?
2 Comments
Stephen23
on 19 Mar 2018
"Also is there a way to calculate gradient of let's say C{1}?"
Numerically: define vectors of points and call the function for each point: currently you will need to use a loop (if you wrote vectorized code a loop would not be required). After that, call numeric gradient on those points.
Symbolically: you would need to define the functions using symbolic variables, and then use symbolic gradient.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!