How do I make a function from a symbolic expression in MATLAB?

16,494

Solution 1

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

Solution 2

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:
16,494
Alireza
Author by

Alireza

M.S in physics, Shiraz, Iran. male & single.

Updated on June 11, 2022

Comments

  • Alireza
    Alireza almost 2 years

    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?

  • Alireza
    Alireza over 14 years
    thank you. but the last link is not accessible in our location.
  • Adam Matan
    Adam Matan over 14 years
    The SymPy? I can download it and put it in another web location for you.
  • Alireza
    Alireza over 14 years
    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
    Alireza over 14 years
    my matlab is 7.4 . it has symbolic toolbox, but does'nt identify "matlabFunction"!!
  • gnovice
    gnovice over 14 years
    @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.
  • Alireza
    Alireza over 14 years
    what is Sympy? if it is possible, send a link for me to download it. thanks
  • Alireza
    Alireza over 14 years
    meanwhile, f is so complex that "solve" finds no answer for it. i must use numerical methods such as bisection.