【问题标题】:vectorizing integral function in MatlabMatlab中的向量化积分函数
【发布时间】:2018-05-09 06:38:29
【问题描述】:

我在 Matlab 中有以下函数:

function y = exact_func(q0,x)
q = q0/(1+x^2);
h_func = @(t) sech(t).^2;
fun = @(t) log(1+q*h_func(t));
y = integral(fun,-Inf,Inf)/(q*integral(h_func,-Inf,Inf));
end

它接受位置x 和参数q0 并返回一个标量。如何修改函数,使其可以接受 x 的数组(一系列步骤)?最终,我想将此函数拟合到一些数据(以找到最合适的q0,但随后Matlab抱怨矩阵尺寸不一致,所以我认为这是因为我当前版本的函数只接受标量x,而不是矢量x.

【问题讨论】:

    标签: matlab vector integration vectorization curve-fitting


    【解决方案1】:

    对于数组值函数的integral,您需要将'ArrayValued' 属性设置为true。还有一些错误需要使用element-wise operations。见下面的固定代码:

    q = q0 ./ (1 + x.^2);    
    %       ↑       ↑     You need to use element-wise operations as indicated 
    h_func = @(t) sech(t).^2;
    fun = @(t) log(1 + q*h_func(t)); %---↓------↓ 
    y = integral(fun,-Inf,Inf,'ArrayValued',1) ./ (q*integral(h_func,-Inf,Inf));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2014-09-06
      • 1970-01-01
      • 2022-06-16
      • 2012-10-15
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多