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

4

7

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?

Freese answered 3/1, 2010 at 16:39 Comment(3)
Please edit your question a bit - make your equation into code using four spaces or equation.Heartthrob
i'm sorry.i can't speak english very well. what do u mean by four spaces?Freese
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.Heartthrob
R
11

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');
Roughneck answered 3/1, 2010 at 17:6 Comment(3)
my matlab is 7.4 . it has symbolic toolbox, but does'nt identify "matlabFunction"!!Freese
@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.Roughneck
meanwhile, f is so complex that "solve" finds no answer for it. i must use numerical methods such as bisection.Freese
M
1

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.

Maletta answered 3/1, 2010 at 17:19 Comment(1)
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.Freese
A
0

If you broad intention is to have numeric values of certain symbolic expressions you have, for example, you have a larger program that generates symbolic expressions and you want to use these expression for numeric purposes, you can simply evaluate them using 'eval'. If their parameters have numeric values in the workspace, just use eval on 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)+a-b*sin(aa*u+m*pi);  %# The main expression

If beta has a value

beta = 1.5;
eval(beta)

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

Adduct answered 24/10, 2016 at 2:58 Comment(0)
H
-1

If you're interested with just the answer for this specific 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 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}
Heartthrob answered 3/1, 2010 at 16:45 Comment(3)
thank you. but the last link is not accessible in our location.Freese
The SymPy? I can download it and put it in another web location for you.Heartthrob
what is Sympy? if it is possible, send a link for me to download it. thanksFreese

© 2022 - 2024 — McMap. All rights reserved.