How to create a function from a symbolic expression in MATLAB?

How can I make a function from a symbolic expression? For example, I have the following:

syms beta n1,n2,m,aa= Constants u = sqrt(n2-beta^2); w = sqrt(beta^2-n1); a = tan(u)/w+tanh(w)/u; b = tanh(u)/w; f = (a+b)*cos(aa*u+m*pi)+ab*sin(aa*u+m*pi); %# The main expression 

If I want to use f in a special program to search for zeros, how can I convert f to a function? Or what should I do to find the zeros of f and such nested expressions?

+7
matlab symbolic-math
source share
4 answers

You have several options ...

Option # 1: Automatically create a function

If you have version 4.9 (R2007b +) or later in Symbolic Tools, you can convert a symbolic expression into an anonymous function or an M file function using the matlabFunction function. Example from the documentation:

 >> syms xy >> r = sqrt(x^2 + y^2); >> ht = matlabFunction(sin(r)/r) ht = @(x,y)sin(sqrt(x.^2+y.^2)).*1./sqrt(x.^2+y.^2) 

Option # 2: Create a function manually

Since you have already written a set of symbolic equations, you can simply cut and paste part of this code into a function. Here is what your previous example would look like:

 function output = f(beta,n1,n2,m,aa) u = sqrt(n2-beta.^2); w = sqrt(beta.^2-n1); a = tan(u)./w+tanh(w)./u; b = tanh(u)./w; output = (a+b).*cos(aa.*u+m.*pi)+(ab).*sin(aa.*u+m.*pi); end 

When calling this function f you need to enter the values ​​of beta and 4 constants, and it will return the result of the evaluation of your main expression.


Note: Since you mentioned wanting to find the zeros of f , you can try using SOLVE according to your symbolic equation:

 zeroValues = solve(f,'beta'); 
+11
source share

Someone mentioned this question with Matlab, so I assume that you are solving the equation with Matlab. If you have a copy of the Matlab Symbolic toolkit, you can solve it directly, as the previous respondent suggested.

If not, then I suggest you write the m Matlab file to evaluate your f () function. The pseudocode that you have already written will be translated almost directly into Matlab strings. When I read it, your f () function is only a function of the beta variable, since you indicate that n1, n2, m and a are all constants. I suggest you build f (beta) values ​​for a range of values. The graph indicates where the 0s functions are located, and you can easily encode the division in half or a similar algorithm to give you your values ​​to the right degree of accuracy.

+1
source share

If you are only interested in the answer for this particular equation, try Wolfram Alpha , which will give you answers like:

alt text http://www4c.wolframalpha.com/Calculate/MSP/MSP642199013hbefb463a9000051gi6f4heeebfa7f?MSPStoreType=image/gif&s=15

If you want to solve this type of equation programmatically, you probably need to use some software packages for symbolic algebra, such as SymPy for python.

quoting official documentation :

 >>> from sympy import I, solve >>> from sympy.abc import x, y 

Solve the polynomial equation:

 >>> solve(x**4-1, x) [1, -1, -I, I] 

Solve the linear system:

 >>> solve((x+5*y-2, -3*x+6*y-15), x, y) {x: -3, y: 1} 
0
source share

If you have a great intention to have the numerical values ​​of certain symbolic expressions that you have, for example, you have a larger program that generates symbolic expressions and you want to use this expression for numerical purposes, you can simply evaluate them using "eval " If their parameters have numerical values ​​in the workspace, just use eval in your expression. For example,

 syms beta %n1,n2,m,aa= Constants % values to exemplify n1 = 1; n2 = 3; m = 1; aa = 5; u = sqrt(n2-beta^2); w = sqrt(beta^2-n1); a = tan(u)/w+tanh(w)/u; b = tanh(u)/w; f = (a+b)*cos(aa*u+m*pi)+ab*sin(aa*u+m*pi); %# The main expression 

If beta matters

 beta = 1.5; eval(beta) 

This will calculate the f value for a particular beta . Using it as a function. This solution is suitable for you in the scenario of using automatically generated symbolic expressions and will be interesting for quick testing with them. If you are writing a program to search for zeros, it will be enough using eval(f) when you must evaluate the function. Using the Matlab function to find zeros using an anonymous function would be better, but you can also wrap eval(f) inside the m file.

0
source share

All Articles