Defining the limits of an integral

1 view (last 30 days)
I need to find the bounds of an integral in the following way:
I need to give an a number, which will be the value of the integral, and the output of the matlab/octave function will be b.
My code gives me a correct number, but it gives warning messages also, so what can be the problem?
This is my code: (I made it in Octave)
function first(a)
f = @(x)(1/sqrt(2*pi)).*exp((-1/(x.^2)));
intf = @(b) quad(f,-b,b);
b = fzero(@(b) intf(b) - a,0)
endfunction
And this happens when a = 1
warning: division by zero
warning: called from
first>@<anonymous> at line 3 column 25
first>@<anonymous> at line 4 column 15
first>@<anonymous> at line 5 column 26
fzero at line 290 column 10
first at line 5 column 5
warning: division by zero
warning: called from
first>@<anonymous> at line 3 column 25
first>@<anonymous> at line 4 column 15
first>@<anonymous> at line 5 column 26
fzero at line 290 column 10
first at line 5 column 5
.
.
.
warning: division by zero
warning: called from
first>@<anonymous> at line 3 column 25
first>@<anonymous> at line 4 column 15
first>@<anonymous> at line 5 column 26
fzero at line 290 column 10
first at line 5 column 5
b = 2.6582
Can you help me?

Accepted Answer

Star Strider
Star Strider on 27 Jun 2019
You forgot to use element-wise operations in the division in ‘f’:
f = @(x)(1/sqrt(2*pi)).*exp((-1./(x.^2)));
When I corrected that and ran this:
a = 1;
f = @(x)(1/sqrt(2*pi)).*exp((-1./(x.^2)));
intf = @(b) quad(f,-b,b);
b = fzero(@(b) intf(b) - a,1)
it produced:
b =
2.658202700888970
Note that I also changed the inital estimate from 0 to 1. That preven ts an infinite initial result.
  2 Comments
Jack Wilson
Jack Wilson on 27 Jun 2019
Thank you!
It gives me the same warnings in Octave, but it turned out that it runs properly in Matlab!
Star Strider
Star Strider on 27 Jun 2019
As always, my pleasure!
I have no experience with Octave. When I did an Interweb search just now, apparently Octave handles element-wise operations with the same ‘dot’ operators, however it also appears to require them for addition and subtraction.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!