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的解释:
方法二:自己转换(经测试,速度比上面的快一半)
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))