How to set up numerical integration in MATLAB?

I want to integrate this expression:

Normal Distribution function:

However, I seem to be having problems setting up the function. As indicated in this MATLAB explanation, I defined a separate function called "NDfx.m" that looks like this:

function [ y ] = NDfx(x) y = (1/sqrt(2*pi))*exp(-.5*x^2); % Error occurs here end 

However, when I call it in my main function, I get an error message in the above line. My main function looks like this:

 function[P] = NormalDistro(u,o2,x) delta = xu; dev = abs((delta)/o2); % Normalizes the parameters entered into function P_inner = quad(@NDfx,-dev,dev); % Integrates function NDfx from -dev to dev (error here) P_outer = 1 - P_inner; % Calculation of outer bounds of the integral if delta > 0 P = P_inner + (P_outer/2); elseif delta < 0 P = P_outer/2; elseif dev == 0 P = .5; end end 

The specific error I get is:

Error in ==> mpower

Inputs must be scalar and square matrix

+4
source share
2 answers

You have configured integration correctly. The error lies in defining the function itself. When using variables for functions to be integrated, "." (period) must precede operators such as ^ and * when they apply to a variable:

 function [y] = NDfx(x) y = (1/sqrt(2*pi))*exp(-.5*(x.^2)); end 
+5
source

Krono and user57368 are correct. They already answered your question correctly. My answer is simply to answer a question that you did not ask. That is, why do you even use an ATV? The fact is that many people want to integrate the function of this form, and it has already been done! Use existing tools to solve your problems, as these tools are often written by someone who knows how to solve the problem accurately and efficiently.

In this case, the existing tool consists of the erf and erfc functions. They provide an accurate, effective, vectorized solution to your problem. The only thing you will need to do is figure out how to convert these integrals to your current problem, made by simply scaling the input for erf and output.

0
source

Source: https://habr.com/ru/post/1341081/


All Articles