rew10812 发表于 2007-5-23 13:12

求助辛普森算法

求助高手看下我的辛普森算法求双重计分,要求如附件,x的范围是0到d,y的范围是-50,50
哪里错了,怎么改?
function I=dbl_itg(fun,c_lo,c_hi,a,b,m,n)
h=double(6.63*10^(-34));
m=double(1.75e5);
u1=1;
u2=3;
y0=0;
n0=1e4;
v=5e14;
d=0.13;
k=h*v*n0*u1*m/(4*pi);
syms x y
fun=k/((d-x)^2+(y-y0)^2)*exp((-1)*(u1+u2*sqrt((d-x)^2+(y-y0)^2)));
m=6;n=6;
b=0.13;
a=0;
c_lo=-50;
c_hi=50;
if m<2 |n<2
    fprintf('number of interals invalid\n');
    return
end
mpt=m+1;npt=n+1;
hx=(b-a)/m;
x=a+(0:m)*hx;
for i=1:mpt
   ylo=c_lo;
   yhi=c_hi;
    hy=(yhi-ylo)/n;
    y(i,:)=ylo+(0:n)*hy;
    f(i,:)=feval(fun,x(i),y(i,:));
    G(i)=Simps_v(f(i,:),hy)
end
I=Simps_v(G,hx);
错误信息如下:??? Assignment has more non-singleton rhs dimensions than non-singleton
subscripts.
Error in ==> D:\MATLAB6p1\work\dbl_itg.m
On line 29==>   y(i,:)=ylo+(0:n)*hy;

[ 本帖最后由 rew10812 于 2007-5-23 14:26 编辑 ]

eight 发表于 2007-5-23 13:31

原帖由 rew10812 于 2007-5-23 13:12 发表 http://www.chinavib.com/forum/images/common/back.gif
求助高手看下我的辛普森算法哪里错了,怎么改?
function I=dbl_itg(fun,c_lo,c_hi,a,b,m,n)
h=double(6.63*10^(-34));
m=double(1.75e5);
u1=1;
u2=3;
y0=0;
n0=1e4;
v=5e14;
d=0.13;
k=h*v*n0*u1*m ...

请先看看 置顶贴:聚宝盆,然后重新整理,把你的问题叙述清楚

rew10812 发表于 2007-5-23 14:27

问题已经修改!

eight 发表于 2007-5-23 15:10

原帖由 rew10812 于 2007-5-23 14:27 发表 http://www.chinavib.com/forum/images/common/back.gif
问题已经修改!


估计 = 两边的向量(或矩阵)长度不匹配,自己调试一下就可以了。另外,y 之前是符号变量,这里有用作数值型变量,建议换一个

rew10812 发表于 2007-5-23 15:23

function I=dbl_itg(fun,c_lo,c_hi,a,b,m,n)
h=double(6.63*10^(-34));
m=double(1.75e5);
u1=1;
u2=3;
y0=0;
n0=1e4;
v=5e14;
d=0.13;
k=h*v*n0*u1*m/(4*pi);
syms x y
%fun=k/((d-x)^2+(y-y0)^2)*exp((-1)*(u1+u2*sqrt(((d-x)^2+(y-y0)^2))));
m=6;n=6;
b=0.13;
a=0;
c_lo=-50;
c_hi=50;
if m<2 |n<2
    fprintf('number of interals invalid\n');
    return
end
mpt=m+1;npt=n+1;
hx=(b-a)/m;
x=a+(0:m)*hx;
for i=1:mpt
   ylo=c_lo;
   yhi=c_hi;
    hy=(yhi-ylo)/n;
    y=ylo+(0:n)*hy;
    f(i,:)=feval(@fun,x(i),y(i));
    G(i)=Simps_v(f(i,:),hy)
end
I=Simps_v(G,hx);
修改过后又出现错误??? Error using ==> fun
Too many output arguments.
Error in ==> D:\MATLAB6p1\work\dbl_itg.m
On line 30==>   f(i,:)=feval(@fun,x(i),y(i));
麻烦问下我要怎么修改?我真的不会阿!
页: [1]
查看完整版本: 求助辛普森算法