Converting function file to Anonomys function.
8 views (last 30 days)
Show older comments
Commented: Stephen23 on 2 Jun 2015
Given we have the function file
function y = lorenz(t,x,sigma,rho,beta)
% INPUT: t is a a real value indicating time
% x is a column vector of size 3 x 1
% sigma, rho, beta are parameters of the Lorenz
% OUTPUT: y is a column vector of size 3 x 1 that gives
% the right hand side of the Lorenz equations
How can we convert this into a Anonymous function
f = @(t,x) lorenz(t,x,sigma,rho,beta)
Stephen23 on 2 Jun 2015
If you are getting an error message please give us the complete message (i.e. all of the red text). We need this to know what is happening.
Also please do not insert empty lines into your code, it makes it difficult to read on this forum.
Guillaume on 1 Jun 2015
If you're asking how to replace the whole code in your lorenz function by an anonymous function, I'm not sure it's a good idea as it's not going to be very readable due to the severe limitations of anonymous functions in matlab.
This would be something like:
f = @(t, x) [sigma*(t+(x(2)-x(1))), (x(1)+t)*(rho-x(2)-t)-x(3)-t, (x(1)+t)*(x(2)+t)-beta*(x(3)+t)];
Keep your function in a file. It's a lot clearer.
Sean de Wolski on 2 Jun 2015
Edited: Sean de Wolski on 2 Jun 2015
And not just clearer; faster also!
Nobel Mondal on 1 Jun 2015
Edited: Nobel Mondal on 1 Jun 2015
fH = str2func('lorenz');
y = feval(fH,t,x,sigma,rho,beta);
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!