加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab中将符号表达式转换为函数

(2011-03-09 08:37:08)
标签:

杂谈

分类: matlab

方法一(官方推荐):
syms x;
%经过一系列运算,得到一个表达式fs
F=vectorize(f);
或:
fstr=char(fs) %转换为字符串
f=inline(fs,'x')
F=vectorize(f); %矢量化,如果要对函数积分或进行其它数组运算,必须有这一步

下面是vectorize的解释:
 VECTORIZE Vectorize expression.
    VECTORIZE(S), when S is a string expression, inserts a '.' before
    any '^', '*' or '/' in S.  The result is a character string.
 
    VECTORIZE(FUN), when FUN is an INLINE function object, vectorizes the
    formula for FUN.  The result is the vectorized version of the INLINE
    function.

方法二:自己转换(经测试,速度比上面的快一半)
fstr=char(fs) %转换为字符串
fs=strrep(fs,'*','.*');
fs=strrep(fs,'/','/');
fs=strrep(fs,'^",'.^');
f=inline(fs,'x')
或:
fstr=char(fs)
f=inline(vectorize(fstr),'x');
与上面时间基本相当,等效。


例子:

一维谐振子波函数图像的matlab绘制

波函数
clear
n=2 %n值自定
maple('with(orthopoly)')
y=maple(['H(',num2str(n),',x)']);
f=inline(['exp(-x*x/2)*(',y,')'])
F=vectorize(f);
Nn=sqrt(1/sqrt(pi)/2^n/prod(1:n));
x0=sqrt(2*5+1);
x1=floor(x0)+2;
x=-x1:0.05:x1;
plot(x,Nn*F(x))

几率密度
clear
n=1 %n值自定
maple('with(orthopoly)')
y=maple(['H(',num2str(n),',x)']);
f=inline(['(exp(-x*x/2)*(',y,'))^2'])
F=vectorize(f);
Nn=sqrt(1/sqrt(pi)/2^n/prod(1:n));
x0=sqrt(2*n+1);
x1=floor(x0)+2;
x=-x1:0.05:x1;
plot(x,Nn^2*F(x))

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有