matlab程序运行太慢,请大神帮看看,如何优化加快运算速度!

我电脑处理器:Intel(R) Core(TM)i5-6300U CPU @2.40GHz 2.50GHz,内存8GB,win10系统。
我的算法中主要包含正无穷定积分、第二类零阶修正贝塞尔函数、exp等。
我编写的程序如下:
mt=str2double(get(handles.edit1,'string'));
M=str2double(get(handles.edit2,'string'));
u=str2double(get(handles.edit3,'string'));
ne=str2double(get(handles.edit4,'string'));
DL=str2double(get(handles.edit5,'string'));
DT=str2double(get(handles.edit6,'string'));
t0=str2double(get(handles.edit7,'string'));
t=str2double(get(handles.edit8,'string'));
[x,y]=meshgrid(str2double(get(handles.edit11,'string')):...
    str2double(get(handles.edit15,'string')):...
    str2double(get(handles.edit13,'string')));
syms z
w1=mt*t0/(4*pi*M*ne*(DL*DT)^0.5);
w2=exp(x.*u/(2*DL));
B=((u*x./(2*DL)).^2+(u*y).^2/(4*DL*DT)).^0.5;
w3=2*besselk(0,B);
w5=int((1/z)*exp(-z-(B.^2/(z*4))),t*u^2/(4*DL),inf);
w4=double(w5);
C=w1*w2.*(w3-w4);
result=[reshape(x,[],1) reshape(y,[],1) reshape(C,[],1)];
set(handles.edit16,'string',num2str(reshape(x,[],1)))
set(handles.edit21,'string',num2str(reshape(y,[],1)))
set(handles.edit22,'string',num2str(reshape(C,[],1)))
在运行时,x和y均为-100~500,步长5的数,实际运算时间在2小时左右;
我尝试了x和y取0~20,步长5的数,实际运算时间在6秒左右;这样算,一组数需要1秒多;
实际应用的时候,我需要的最大取值范围是-200~1000,步长5的数。这么算的话,需要大半天能算完!
这样效率太低了,求大神帮我看看,我如何优化程序,可以大量的减少运行速度,十分感谢

建议:取消syms,用数值积分方法先计算积分号里面的东西,而不要用符号积分算法。将下面这段:

syms z
w1=mt*t0/(4*pi*M*ne*(DL*DT)^0.5);
w2=exp(x.*u/(2*DL));
B=((u*x./(2*DL)).^2+(u*y).^2/(4*DL*DT)).^0.5;
w3=2*besselk(0,B);
w5=int((1/z)*exp(-z-(B.^2/(z*4))),t*u^2/(4*DL),inf);
w4=double(w5);

替换为

w1=mt*t0/(4*pi*M*ne*(DL*DT)^0.5);
w2=exp(x.*u/(2*DL));
B=((u*x./(2*DL)).^2+(u*y).^2/(4*DL*DT)).^0.5;
w3=2*besselk(0,B);
ifun = @(z,B)(1./z).*exp(-z-(B.^2./(z*4)));
w5=integral(@(z)ifun(z,B),t*u^2/(4*DL),inf);
w4=w5;

追问

感谢您的指点!但是运行时候报错了,说矩阵维度必须一致!弄了半天也不行,还望指教!另外第五行的积分变量是z,B不是,是不是可以去掉B

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-10-23
看不太懂你的程序,
不过明显有很大问题,
数值计算绝对不应该出现syms(符号变量),
感觉教你Matlab的完全不懂Matlab,
请把你的符号积分全部换成数值积分
就你这简单的小积分(-200:5:1000) 就是几秒的事追问

我上星期刚接触matlab,自己鼓弄的。您的意思我大概明白了,能否推荐个可以接受积分上限是inf的数值积分函数,要精度高点的,十分感谢!

追答

doc integral
然后根据说明把你的函数套进去试试

相似回答