在matlab中绘制一个涉及积分的函数

时间:2012-10-15 19:28:37

标签: function matlab

我正在尝试绘制一个包含定积分的函数。我的代码使用所有匿名函数。当我运行该文件时,它给了我一个错误。我的代码如下:

    %%% List of Parameters %%%
    gamma_sp = 1;   
    cap_gamma = 15;   
    gamma_ph = 0;   
    omega_0 = -750;   
    d_omega_0 = 400;   
    omega_inh = 100;   
    d_omega_inh = 1000;   

    %%% Formulae %%%   
    gamma_t = gamma_sp/2 + cap_gamma/2 + gamma_ph;   
    G = @(x) exp(-(x-omega_inh).^2./(2*d_omega_inh.^2))./(sqrt(2*pi)*d_omega_inh);   
    F = @(x) exp(-(x-omega_0).^2./(2*d_omega_0.^2))./(sqrt(2*pi)*d_omega_0);   
    A_integral = @(x,y) G(x)./(y - x + 1i*gamma_t);   
    Q_integral = @(x,y) F(x)./(y - x + 1i*gamma_t);   
    A = @(y) integral(@(x)A_integral(x,y),-1000,1000);   
    Q = @(y) integral(@(x)Q_integral(x,y),-3000,0);   

    P1 = @(y) -1./(1i.*(gamma_sp + cap_gamma)).*(1./(y + 2.*1i.*gamma_t)*(A(y)-conj(A(0)))-1./y.*(A(y)-A(0))+cap_gamma./gamma_sp.*Q(y).*(A(0)-conj(A(0))));   

    P2 = @(y) conj(P1(y));   
    P = @(y) P1(y) - P2(y);
    sig = @(y) abs(P(y)).^2;

    rng = -2000:0.05:1000;   
    plot(rng,sig(rng))   

在我看来,当程序运行plot命令时,它应该将rng的每个值放入sig(y),并且该值将用作A_integral和Q_integral中的y值。但是,当我尝试运行程序时,matlab会抛出错误。

Error using  - 
Matrix dimensions must agree.

Error in @(x,y)G(x)./(y-x+1i*gamma_t)

Error in @(x)A_integral(x,y)

Error in integralCalc/iterateScalarValued (line 314)
            fx = FUN(t);

Error in integralCalc/vadapt (line 133)
        [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 76)
    [q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);

Error in @(y)integral(@(x)A_integral(x,y),-1000,1000)

Error in
@(y)-1./(1i.*(gamma_sp+cap_gamma)).*(1./(y+2.*1i.*gamma_t)*(A(y)-conj(A(0)))-1.    /y.*(A(y)-A(0))+cap_gamma./gamma_sp.*Q(y).*(A(0)-conj(A(0))))

Error in @(y)P1(y)-P2(y)

Error in @(y)abs(P(y)).^2

Error in fwm_spec_diff_paper_eqn (line 26)
plot(rng,sig(rng)) 

关于我做错了什么的想法?

2 个答案:

答案 0 :(得分:1)

你有

>> rng = -2000:0.05:1000;   
>> numel(rng) 
ans = 
    60001

所有60001元素都传递给

A = @(y) integral(@(x)A_integral(x,y),-1000,1000);  

调用

A_integral = @(x,y) G(x)./(y - x + 1i*gamma_t);

(类似于Q)。问题是,integral是一种自适应求积法,意味着(粗略地)它将插入x的{​​{1}}量随着A_integral行为的确定而变化A_integral

因此,x中的元素数量通常与y调用中x中的元素数量不同。这就是A_integral失败的原因。

考虑到您尝试执行的操作的复杂性,我认为最好将所有匿名函数重新定义为正确的函数,并将其中的一些函数集成到单个函数中。查看y-x +1i*gamma_t的文档,看看它是否有帮助(例如,bsxfun而不是bsxfun(@minus, y.', x)也许可以解决其中的一些问题),否则,仅在{{1}中进行矢量化}并循环遍历y-x

答案 1 :(得分:0)

谢谢Rody,这对我来说很有意义。我一直在尝试像mathematica一样使用matlab,我忘了matlab是如何做的。我稍微修改了一下代码,它产生了正确的结果。积分的评估非常粗略,但应该很容易解决。我在下面发布了修改后的代码。

%%% List of Parameters %%%
gamma_sp = 1;
cap_gamma = 15;
gamma_ph = 0;
omega_0 = -750;
d_omega_0 = 400;
omega_inh = 100;
d_omega_inh = 1000;

%%% Formulae %%%
gamma_t = gamma_sp/2 + cap_gamma/2 + gamma_ph;
G = @(x) exp(-(x-omega_inh).^2./(2*d_omega_inh.^2))./(sqrt(2*pi)*d_omega_inh);
F = @(x) exp(-(x-omega_0).^2./(2*d_omega_0.^2))./(sqrt(2*pi)*d_omega_0);
A_integral = @(x,y) G(x)./(y - x + 1i*gamma_t);
Q_integral = @(x,y) F(x)./(y - x + 1i*gamma_t);

w = -2000:0.05:1000;
sigplot = zeros(size(w));
P1plot = zeros(size(w));
P2plot = zeros(size(w));
Pplot = zeros(size(w));
aInt_range = -1000:0.1:1200;
qInt_range = -2000:0.1:100;
A_0 = sum(A_integral(aInt_range,0).*0.1);

for k=1:size(w,2)

P1plot(k) = -1./(1i*(gamma_sp + cap_gamma)).*(1./(w(k)+2.*1i.*gamma_t).*(sum(A_integral(aInt_range,w(k)).*0.1)-conj(A_0))-1./w(k).*(sum(A_integral(aInt_range,w(k)).*0.1)-A_0)+cap_gamma./gamma_sp.*sum(Q_integral(qInt_range,w(k)).*0.1).*(A_0-conj(A_0)));
P2plot(k) = conj(P1plot(k));
Pplot(k) = P1plot(k) - P2plot(k);
sigplot(k) = abs(Pplot(k)).^2;

end

plot(w,sigplot)