Create symbolic function from a function file
显示 更早的评论
Dear all.
I have created a function to represent a dynamical system with variables z=[x1,y1,x2,y2,...xn,yn] as following:

Alpha, beta, gamma and S are constants. Now l have a new script and l want to call this function in my new script as a symbolic function but MATLAB reterened me error said that ''undefined z''. Could you tell me that how l can create a symbolic function using the function l have. Thank you very much!
回答(1 个)
Nipun Agarwal
2020-6-18
Hello,
Since you want to convert this into symbolic function, the problem with symbolic functions is you can create functions in the form of expressions. Since in your case many values of function depends on other values, I came up with the approach of precomputing all the values before in a function block returning that to a output vector and using them to create a symbolic function expression. I am also writing the code for your reference.
%calling the coupled oscillatorsfun to precompute values for symbolic
%expression
[a,z_squares,x,y,Cx,Cy,alpha,beta,gamma,n] = coupledOscillatorsFun([5 6 7 8],5,5,5,5);
% defining all x systems vector as a symbolic (odd indexes)
syms f_x(a_s,z_squares_s,x_s,y_s,Cx_s,Cy_s,alpha_s,beta_s,gamma_s,n_s)
f_x(a_s,z_squares_s,x_s,y_s,Cx_s,Cy_s,alpha_s,beta_s,gamma_s,n_s) = (a_s-z_squares_s).*x_s-alpha_s.*y_s.*z_squares_s+gamma_s.*Cx_s-gamma_s.*(x_s-beta_s.*y_s);
% defining all y systems vector as a symbolic function (even indexes)
syms f_y(a_s,z_squares_s,x_s,y_s,Cx_s,Cy_s,alpha_s,beta_s,gamma_s,n_s)
f_y(a_s,z_squares_s,x_s,y_s,Cx_s,Cy_s,alpha_s,beta_s,gamma_s,n_s) = (a_s-z_squares_s).*y_s+alpha_s.*x_s.*z_squares_s+gamma_s.*Cy_s-gamma_s.*(y_s+beta_s.*x_s);
%calling the symbolic function (odd index)
f_x(a,z_squares,x,y,Cx,Cy,alpha,beta,gamma,n)
%calling the symbolic function (even index)
f_y(a,z_squares,x,y,Cx,Cy,alpha,beta,gamma,n)
类别
在 帮助中心 和 File Exchange 中查找有关 Operations on Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!