# Evaluation of integral2. Error

1 view (last 30 days)
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 on 23 Feb 2021
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
DIMITRIS GEORGIADIS on 23 Feb 2021
That works perfect indeed! Thank you very much.