views:

563

answers:

3

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?

A: 

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}
Adam Matan
thank you. but the last link is not accessible in our location.
Alireza
The SymPy? I can download it and put it in another web location for you.
Adam Matan
SymPy should not be necessary considering she has MatLab...
benofsky
what is Sympy? if it is possible, send a link for me to download it.thanks
Alireza
It's a python library for symbolic math, see http://code.google.com/p/sympy/
Adam Matan
+2  A: 

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 = 

     @(x,y)sin(sqrt(x.^2+y.^2)).*1./sqrt(x.^2+y.^2)

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);
end

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');
gnovice
my matlab is 7.4 . it has symbolic toolbox, but does'nt identify "matlabFunction"!!
Alireza
@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
meanwhile, f is so complex that "solve" finds no answer for it.i must use numerical methods such as bisection.
Alireza
+1  A: 

Hi

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.

Regards

Mark

High Performance Mark
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
But you've already written the code !
High Performance Mark

related questions