matlab中符号函数如何求最大值的表达式,如何在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)+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?

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值