Defining the limits of an integral
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Octave에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!