Converting function file to Anonomys function.

조회 수: 1 (최근 30일)
Tom Craven
Tom Craven 2015년 6월 1일
댓글: Stephen23 2015년 6월 2일
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
% equations
% OUTPUT: y is a column vector of size 3 x 1 that gives
% the right hand side of the Lorenz equations
y=[0;0;0];
x=x+t;
y(1)=sigma*(x(2)-x(1));
y(2)=x(1)*(rho-x(2))-x(3);
y(3)=x(1)*x(2)-beta*x(3);
end
How can we convert this into a Anonymous function
f = @(t,x) lorenz(t,x,sigma,rho,beta)
??.
  댓글 수: 6
Walter Roberson
Walter Roberson 2015년 6월 2일
Please show the complete trace of the "too many input arguments" error.
Stephen23
Stephen23 2015년 6월 2일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2015년 6월 1일
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.
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2015년 6월 2일
편집: Sean de Wolski 2015년 6월 2일
^This
And not just clearer; faster also!

댓글을 달려면 로그인하십시오.


Nobel Mondal
Nobel Mondal 2015년 6월 1일
편집: Nobel Mondal 2015년 6월 1일
fH = str2func('lorenz');
y = feval(fH,t,x,sigma,rho,beta);
  댓글 수: 1
Tom Craven
Tom Craven 2015년 6월 1일
Is the second line supposed to read f= feval(fH,t,x,sigma,rho,beta);
?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by