Evaluation of integral2. Error

조회 수: 5 (최근 30일)
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 20일
댓글: DIMITRIS GEORGIADIS 2021년 2월 23일
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
  댓글 수: 1
DIMITRIS GEORGIADIS
DIMITRIS GEORGIADIS 2021년 2월 23일
That works perfect indeed! Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (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