2
votes

I am going to devise a 2D function as a probability density function, is which a function of two variables, i.e. f = f(x,n). Then, as the target is plotting the probability variation, the integration in related to parameter x should be taken into account. The t parameter is the variable planned to be the upper bound of the integration. It is suffice to say that n is the other tuning factor. Finally, with due attention to the considered meshgrid, the probability surface is supposed to be drawn.

My option for the integration process is the symbolic int function. But there is an error: Error using mupadmex

Here is my code:

clear;

syms x;
syms n;
syms t;

sigma = 1;
mu = 0;

[t,n] = meshgrid(0:0.01:20, 1:1:100);

f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);

ff = int(f, x, -inf, t);

mesh(n,t,ff);

And the error trace:

Error using mupadmex
Error in MuPAD command: The argument is invalid. [Dom::Interval::new]

Error in sym/int (line 153)
   rSym = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s,options);

Error in field (line 14)
ff = int(f, x, -inf, t);

Would you please helping me to overcome this tie?!

PS. I know that there are some ideas to do this stuff more numerically by integral function, but I am prone to handle this case by int function, if it is possible. Because this code should be used as a service by the other snippets and the generated ff parameter is completely deserving, however it won't be a closed form function.

Thanks in advance.

1
So you are trying to evaluate the function f(x,n) 2000000 times for different x and and upper limit of the integral? Each f(x,ni) has an specific upper imit ti? Am I understanding correctly? - Ander Biguri
@AnderBiguri: You're completely on board!... The applied range for t is considerably desired for the required resolution and sweeping the final plot. Though, it is not the problem! - Roboticist
@Ordenador It seems you want to integrate a Gaussian function. Why not just use the erfc or normcdf functions? - Luis Mendo
@LuisMendo: Because I've caught some degrees of freedom for next computational steps by this approach... - Roboticist

1 Answers

1
votes

I have changed several details in your code, Ill try to explain all of them.

  1. No need of defining symbolic variables that are not going to be symbolic.

code:

clear;

syms x;

sigma = 1;
mu = 0; % This is never used!
  1. You want to integrate a function f(n,x) dx for different n. If you create a meshgrid of n (instead of a vector), you will have tons of repeated f(ni,x) that you are not going to use.

Just do:

t=(0:1:20);

n=(1:10:100);
% are you sure you dont want ((n.*x)-mu) here?
f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);
  1. int does not accept a mesh of symbolic functions! (or I haven't managed to make it work...).

So put a couple of for's there!

for ii=1:length(n)
    for jj=1:length(t)
        ff(ii,jj) = int(f(ii), x, -inf, t(jj));
    end
end
  1. Now we DO want a meshgrid for the plot!

Like this:

[t,n] = meshgrid(t, n);
  1. And you want to plot the numerical value, so use double() to convert from symbolic to numerical:

plot!:

mesh(n,t,double(ff));

Result (with low amount of points due to the obvious computational effort needed)

enter image description here