Take the 2-minute tour ×

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)+a-b*sin(aa*u+m*pi);  %# The main expression

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

share|improve this question
Please edit your question a bit - make your equation into code using four spaces or equation. –  Adam Matan Jan 3 '10 at 16:42
i'm sorry.i can't speak english very well. what do u mean by four spaces? –  Alireza Jan 3 '10 at 17:13
before writing an equation or a piece of code, just type four spaces, like " a=tan(u)/w+tanh(w)/u;". It will highlight your code so it would look nicer. –  Adam Matan Jan 3 '10 at 17:25

3 Answers 3

up vote 8 down vote accepted

You have a couple of options...

Option #1: Automatically generate a function

If you have version 4.9 (R2007b+) or later of the Symbolic Toolbox you can convert a symbolic expression to an anonymous function or a function M-file using the matlabFunction function. An example from the documentation:

>> syms x y
>> r = sqrt(x^2 + y^2);
>> ht = matlabFunction(sin(r)/r)

ht = 


Option #2: Generate a function by hand

Since you've already written a set of symbolic equations, you can simply cut and paste part of that code into a function. Here's what your above 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)+(a-b).*sin(aa.*u+m.*pi);

When calling this function f you have to input the values of beta and the 4 constants and it will return the result of evaluating your main expression.

NOTE: Since you also mentioned wanting to find zeroes of f, you could try using the SOLVE function on your symbolic equation:

zeroValues = solve(f,'beta');
share|improve this answer
my matlab is 7.4 . it has symbolic toolbox, but does'nt identify "matlabFunction"!! –  Alireza Jan 3 '10 at 19:58
@Alireza: Sorry, I forgot to specify the version of the toolbox you would need. MATLAB 7.5 would probably have the version of the Symbolic Toolbox where matlabFunction first appears. –  gnovice Jan 3 '10 at 21:21
meanwhile, f is so complex that "solve" finds no answer for it. i must use numerical methods such as bisection. –  Alireza Jan 5 '10 at 9:40

Someone has tagged this question with Matlab so I'll assume that you are concerned with solving the equation with Matlab. If you have a copy of the Matlab Symbolic toolbox you should be able to solve it directly as a previous respondent has suggested.

If not, then I suggest you write a Matlab m-file to evaluate your function f(). The pseudo-code you're already written will translate almost directly into lines of Matlab. As I read it your function f() is a function only of the variable beta since you indicate that n1,n2,m and a are all constants. I suggest that you plot the values of f(beta) for a range of values. The graph will indicate where the 0s of the function are and you can easily code up a bisection or similar algorithm to give you their values to your desired degree of accuracy.

share|improve this answer
my principal code is something like what i wrote here which f is very long and i dont want to write directly the expanded f again in a new line. just using it's name. –  Alireza Jan 3 '10 at 19:56
But you've already written the code ! –  High Performance Mark Jan 3 '10 at 20:34

If you're interested with just the answer for this specific equation, Try Wolfram Alpha, which will give you answers like:

alt text

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

quoting the official documentation:

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

Solve a polynomial equation:

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

Solve a linear system:

>>> solve((x+5*y-2, -3*x+6*y-15), x, y)
{x: -3, y: 1}
share|improve this answer
thank you. but the last link is not accessible in our location. –  Alireza Jan 3 '10 at 17:14
The SymPy? I can download it and put it in another web location for you. –  Adam Matan Jan 3 '10 at 17:23
SymPy should not be necessary considering she has MatLab... –  benofsky Jan 3 '10 at 21:19
what is Sympy? if it is possible, send a link for me to download it. thanks –  Alireza Jan 4 '10 at 4:47
It's a python library for symbolic math, see code.google.com/p/sympy –  Adam Matan Jan 4 '10 at 20:49

Your Answer


