Defining the limits of an integral

조회 수: 5 (최근 30일)
Jack Wilson
Jack Wilson 2019년 6월 27일
댓글: Star Strider 2019년 6월 27일
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?

채택된 답변

Star Strider
Star Strider 2019년 6월 27일
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
Jack Wilson
Jack Wilson 2019년 6월 27일
Thank you!
It gives me the same warnings in Octave, but it turned out that it runs properly in Matlab!
Star Strider
Star Strider 2019년 6월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Octave에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by