Evaluation of integral2. Error

1 次查看(过去 30 天)
Using the following code I get a matrix dimension error. I suspect something goes wrong when I integrate out z from func_3. Could anyone help on that?
clear all; clc; close all;
% Define functions:
func_1 = @(x) (1/(0.1*sqrt(2*pi))).*exp(-0.5.*((x - 1)./0.1).^2);
func_2 = @(y) (1/(0.01*sqrt(2*pi))).*exp(-0.5.*((y - 0.1)./0.01).^2);
func_12 = @(x, y) func_1(x).*func_2(y);
% Visualization:
fcontour(func_12,[0.8 1.2 0.05 0.15])
% Define new function:
c = 1.05;
sigma_e = 0.05;
func_3 = @(x, y, z) (1./y).*exp(-0.5.*((z - x)./y).^2).*...
exp(-0.5.*((c - z)./sigma_e).^2);
% Intergate out z:
l_bound = -Inf;
u_bound = Inf;
func_4 = @(x, y) integral(@(z) func_3(x, y, z), l_bound, u_bound);
% Get the new function:
func_5 = @(x, y) func_4(x, y).*func_12(x, y);
% Compute the integral:
q = integral2(func_5, -Inf, Inf, 0, Inf);
w = 1/q;

采纳的回答

Shashank Gupta
Shashank Gupta 2021-2-23
Hi Dimitris,
It does look like problem is in integration of func_3 and looking at the error I feel like some kind of matrix or vector is involved in the calculation. My first attempt to solve this issue is to enable "ArrayValued" flag in integral function. This will make sure the whenever the matrix or vector calculation involved will smoothly run. I am attaching the changes below. It should work.
func_4 = @(x, y) integral(@(z) func_3(x, y, z), l_bound, u_bound,"ArrayValued",1);
I hope this resolves the issue.
Cheers

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by