Matlab: integrate function in dependence of a parameter

463 Views Asked by At

I am a matlab beginner and can not spot the mistake in the following code:

a=0.5; b=0.5; f=@(u,x) exp(-x.^2/2)*(2*pi).^(-1/2)*exp(log(a+b.*x.^2).*u); t=0:0.1:1; g=0*t; for i=1:numel(t) fab=@(x) f(t(i),x); g(i)=quadgk(fab,-inf,inf); end surf(t,g)

Where is the format wrong? I really do not get it. I want to plot the value of a integral in dependence of a parameter.

Thank you!

1

There are 1 best solutions below

1
On

Two things. First, add some more ., if you use

f=@(u,x) exp(-x.^2/2).*(2*pi).^(-1/2).*exp(log(a+b.*x.^2).*u);

f works as it should. Second, surf does not work for 1d-plots, use plot, giving

a=0.5; b=0.5; f=@(u,x) exp(-x.^2/2).*(2*pi).^(-1/2).*exp(log(a+b.*x.^2).*u); t=0:0.1:1; g=0*t; for i=1:numel(t) fab=@(x) f(t(i),x); g(i)=quadgk(fab,-inf,inf); end plot(t,g)