Create symbolic function from a function file

4 visualizzazioni (ultimi 30 giorni)
Penglin Cai
Penglin Cai il 18 Giu 2020
Risposto: Nipun Agarwal il 18 Giu 2020
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!

Risposte (1)

Nipun Agarwal
Nipun Agarwal il 18 Giu 2020
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)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by