构造一个函数

时间:2013-01-15 11:57:41

标签: function matlab user-defined-functions anonymous-function

从这段代码开始:

    clc, clear all, close all
tic

k1 = 0.01:0.1:100;
k2 = 0.01:0.1:100;
k3 = 0.01:0.1:100;

k = sqrt(k1.^2 + k2.^2 + k3.^2);

c = 1.476;
gamma = 3.9;

colors = {'cyan'};
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E = @(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,numel(k1));
E_int(1) = 1.5;

for i = 2:numel(k)
    E_int(i) = E_int(i-1) - integral(E,k(i-1),k(i));
end

beta = c*gamma./(k.*sqrt(E_int));


F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));

count = 0;
for i = 1:numel(k1)
    count = count + 1;
    phi_11 = @(k2,k3) phi_11_new(k1,k2,k3,beta,i);
    phi_22 = @(k2,k3) phi_22_new(k1,k2,k3,beta,i);
    phi_33 = @(k2,k3) phi_33_new(k1,k2,k3,beta,i);
    F_11(count) = integral2(phi_11,-100,100,-100,100);
    F_22(count) = integral2(phi_22,-100,100,-100,100);
    F_33(count) = integral2(phi_33,-100,100,-100,100);
end

figure
hold on
plot(k1,F_11,'b')
plot(k1,F_22,'cyan')
plot(k1,F_33,'magenta')
hold off

,其中

function phi_11 = phi_11_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k1(i).^2 + k2.^2 - k3.*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta(i).*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta(i)));
xhsi1 = C1 - k2./k1(i).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1(i).^2 - 2.*k1(i).*k30.*xhsi1 + (k1(i).^2 + k2.^2).*xhsi1_q);
end

function phi_22 = phi_22_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k1(i).^2 + k2.^2 - k3.*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta(i).*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta(i)));
xhsi2 = k2./k1(i).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end

function phi_33 = phi_33_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2+k2.^2+k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2+k2.^2+k30.^2);
E_k0 = (1.453.*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end

此程序使我的结果与来自某项研究的其他人不相符。我应该匹配的结果发布在下面:

enter image description here

而我的看起来像这些

enter image description here

很容易认为只有comp符合理论结果;因此,我认为这个缺陷可能存在于函数phi_11_new(和phi_22_new)之外的beta定义中。

您是否有人建议如何计算phi_11_new(和phi_22_new)中的beta而不是我目前的方式?

我事先感谢你们的支持。

最诚挚的问候, FPE

3 个答案:

答案 0 :(得分:2)

我改进了插值,使其不再因小值而分解。它还返回更正确的值,因为它现在插值值的对数。

这是现在的代码。

function test15()

[k1,k2,k3] = deal(0.01:0.1:400);

k = sqrt(k1.^2 + k2.^2 + k3.^2);

c = 1.476;
gamma = 3.9;

Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));

[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));

k_vec = k;
beta_vec = beta;

kLim = 100;

for ii = 1:numel(k1)
    phi_11 = @(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
    phi_22 = @(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
    phi_33 = @(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
    F_11(ii) = quad2d(phi_11,-kLim,kLim,-kLim,kLim);
    F_22(ii) = quad2d(phi_22,-kLim,kLim,-kLim,kLim);
    F_33(ii) = quad2d(phi_33,-kLim,kLim,-kLim,kLim);
end

figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on

end

function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);

log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);

k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end

function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);

log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);

k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30.*k1.*beta));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end

function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);

log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);

k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end

这个数字似乎很好地与原始结果一致。即使仍然存在一些差异。

附注:由于在模拟中将k值设置为100,因此图中大于此值的值不正确。它们的计算不使用完整(k2,k3) - “圆”中的所有值。我们还可以看到这些值的偏差。

New loglog-plot of F11, F_22 and F_33.

答案 1 :(得分:1)

好的,这就是我现在所拥有的。我想听听你的想法 - 它还不完美。我无法访问函数integralintegral2,因此如果您可以重新插入它们(例如,而不是我的quad2d)并测试代码,您可能会获得比我现在更好的结果。

我的第一个想法是在beta - 函数中[k1,k2,k3]的每个三元组的for循环中计算phi。事实证明这非常慢,所以我使用了k - 值的向量,并像以前一样计算了相应的向量beta。然后将这两个向量传递给phi,其中值用于插值函数(interp1)以查找beta - 特定k - 值的值。

function myFunction()

[k1,k2,k3] = deal(0.01:0.1:400);

k = sqrt(k1.^2 + k2.^2 + k3.^2);

c = 1.476;
gamma = 3.9;

Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));

[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));

k_vec = k;
beta_vec = beta;

for ii = 1:numel(k1)
    phi_11 = @(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
    phi_22 = @(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
    phi_33 = @(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
    F_11(ii) = quad2d(phi_11,-100,100,-100,100);
    F_22(ii) = quad2d(phi_22,-100,100,-100,100);
    F_33(ii) = quad2d(phi_33,-100,100,-100,100);
end

figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on

end

function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);

beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));

k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end

function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);

beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));

k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30.*k1.*beta));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end

function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);

beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));

k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end

这产生了下图。请注意,对于k1的最小值,集成不会成功。

F_11, F_22 and F_33 in a loglog-plot.

编辑 - 关于在phi-functions中计算beta的评论

由于您基本上尝试了我最初做过的事情,所以我添加了一个示例,说明我如何在beta - 函数中计算phi矩阵。请注意,此代码非常慢,以至于我从未实际运行它。

function phi_11 = phi_11_new(k1,k2,k3)
k = sqrt(k1^2 + k2.^2 + k3.^2);

c = 1.476;
gamma = 3.9;
beta = zeros(size(k));
E = @(x) (1.453*x.^4)./((1 + x.^2).^(17/6));
for ii = 1:size(k,1)
    for jj = 1:size(k,2)
        E_int = 1.5-quad(E,0.001,k(ii,jj));
        beta(ii,jj) = c*gamma/(k(ii,jj)*sqrt(E_int));
    end
end


k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end

答案 2 :(得分:0)

目前我正在运行上述代码的不同版本。

如下

clc, clear all, close all
tic



k1 = (0.01:0.1:100);

c = 1.476;
gamma = 3.9;


F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));

count = 0;
for i = 1:numel(k1)
    count = count + 1;
    phi_11 = @(k2,k3) phi_11_new(k1,k2,k3,gamma,i);
    phi_22 = @(k2,k3) phi_22_new(k1,k2,k3,gamma,i);
    phi_33 = @(k2,k3) phi_33_new(k1,k2,k3,gamma,i);
    F_11(count) = integral2(phi_11,-100,100,-100,100);
    F_22(count) = integral2(phi_22,-100,100,-100,100);
    F_33(count) = integral2(phi_33,-100,100,-100,100);
end

这次phi_11,phi_22和phi_33为

function phi_11 = phi_11_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));  
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k0.^2 - 2.*k30.^2 + beta.*k30.*k1(i))./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
xhsi1 = C1 - k2./k1(i).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1(i).^2 - 2.*k1(i).*k30.*xhsi1 + (k1(i).^2 + k2.^2).*xhsi1_q);
end

function phi_22 = phi_22_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2)))); 
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta.*k1(i).^2).*(k0.^2 - 2.*k30.^2 + beta.*k1(i).*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
xhsi2 = (k2./k1(i)).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end

function phi_33 = phi_33_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2+k2.^2+k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2)))); 
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2+k2.^2+k30.^2);
E_k0 = (1.453.*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end

请注意,现在beta是在phi函数中计算的。此外,我使用的是等效的beta表达式。有关详细信息,请查看下面的图片

enter image description here

因此,在更新的模型中,我正在调用表现相当慢的hypergeom。 这就是为什么我想通过能谱积分来计算第一个代码中的beta的主要原因。

顺便说一下,目前我还不知道如何成功地完成这项工作。

祝你好运, FPE