MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Javier
on 26 Mar 2013

Hi.

I need to compute an integral of a function that consist in the product between a Cumulative distribution of a Normal, and another term.

I tried using the command int, but in that case I was not able to execute the normcdf command.

Matlab said: CNaux2=normcdf(aux5) Error using NaN Trailing string input must be 'single' or 'double'.

Instead, i tried using the quad command. In that case, the normcdf could be defined as a function handle, but then i coul not excute the quad command. In this case matlab said

Error using * Inner matrix dimensions must agree.

And i don´t understand why, because i don´t have any matrix here, i have only a product of functions of one variable.

Could you please help me?

Thanks! Javier

*No products are associated with this question.*

Answer by Tom Lane
on 27 Mar 2013

Accepted answer

You didn't really explain what you tried. Here's an example that works:

quad(@(x) x.*normcdf(x),-1,1)

If I use matrix multiplication "*" instead of elementwise multiplication ".*" I get the same error you report. So if your integrand is something like this, maybe you need to switch to elementwise multiplication.

Javier
on 27 Mar 2013

Thanks, i did not know that. However, i still can´t solve the integral (now the error message is Undefined function 'times' for input arguments of type 'function_handle)

What i have to do is to compute a definite integral for the function:

N(a(t))*b(t)*C(d(t)) dt

Where N(a(t)) is a normal probability function valued at the value a, which is in fact a function that depends on t. C(d(t)) is a cumulative normal probability function valued d, which is also a function that depends on t. And b(t) is another function of t.

I have tried to define first the functions a(t) b(t) and c(t) as function handles. Then i used the commandsnormcdf and normpdf to compute the normal cumulative and probability function on this function (i think this way i obtain also a function handle that depends on t, but i am not sure if this is ok. For example, CNaux1 = @(t)normcdf((K*(T-t))/sigma/sqrt(T-t)) )

Later, i define another function handle integrand= N(a(t)).*b(t).*C(d(t)) and i use the quad (integrand, 0, T) (T previously defined)

I thinh i probably made more than one mistake, i am quite a bit lost...

Thank you very much!

Answer by Youssef Khmou
on 27 Mar 2013

Edited by Youssef Khmou
on 27 Mar 2013

hi,

I am no sure about the error you get, but try this version :

m=0; s=1; x=0:0.1:10; C=normcdf(x,m,s); PHI=2*exp(j*2*pi*x); % Consider it as wave function N=abs(PHI).^2; % consider it as the probability of existence . b=exp(-(x-2).^2);; % the thrid function as you consider II=C.*b.*N; figure, plot(x,II); F=trapz(x,II);

So the integral is evaluated using the function "trapz" along the X axis ,

I hope this helps

## 2 Comments

## Javier (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/68703#comment_139222

What i have to do is to compute a definite integral for the function:

N(a(t))*b(t)*C(d(t)) dt

Where N(a(t)) is a normal probability function valued at the value a, which is in fact a function that depends on t. C(d(t)) is a cumulative normal probability function valued d, which is also a function that depends on t. And b(t) is another function of t.

I have tried to define first the functions a(t) b(t) and c(t) as function handles. Then i used the commandsnormcdf and normpdf to compute the normal cumulative and probability function on this function (i think this way i obtain also a function handle that depends on t, but i am not sure if this is ok. For example, CNaux1 = @(t)normcdf((K*(T-t))/sigma/sqrt(T-t)) )

Later, i define another function handle integrand= N(a(t)).*b(t).*C(d(t)) and i use the quad (integrand, 0, T) (T previously defined)

I thinh i probably made more than one mistake, i am quite a bit lost...

Thank you very much!

## Tom Lane (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/68703#comment_139337

This works:

If you tried something else that does not work, you should say what you tried.