필터 지우기
필터 지우기

Matlab Integral function for a self teaching beginner

조회 수: 2 (최근 30일)
Bruce Griffin
Bruce Griffin 2022년 7월 14일
답변: Steven Lord 2022년 7월 14일
I need to take an integral over a meshgrid of space and time.
at each point in space and time I need to take an integral and Im having trouble understand the integral command.
To simplify I need where t is time an x is space and c is what im integrading over.
I believe that
t=0:1:10
x=0:1:10
[time space]=meshgrid(t,x)
fun=@(c) time+space+c
answer=integral(fun,-10,10)
So why does this not work but
answer=integral(fun,-10,10,'ArrayValued',1) does?
Im trying to conceptualy understand

채택된 답변

Star Strider
Star Strider 2022년 7월 14일
The integral function numerically integrates the function supplied to it over the variable range defined in the function call. It does not assume matrix data unless you tell it, otherwise assuming scalar data.
Use the trapz function for gridded data —
t=0:1:10;
x=0:1:10;
c = pi; % Create 'c'
[time space]=meshgrid(t,x);
fun=@(c) time+space+c;
answer_cols=trapz(fun(c))
answer_cols = 1×11
81.4159 91.4159 101.4159 111.4159 121.4159 131.4159 141.4159 151.4159 161.4159 171.4159 181.4159
answer = trapz(answer_cols)
answer = 1.3142e+03
.
  댓글 수: 3
Bruce Griffin
Bruce Griffin 2022년 7월 14일
what if I need c to be integrated over a different interval such as -inf to inf?
Star Strider
Star Strider 2022년 7월 14일
Use the integral function with the 'ArrayValued' name-value pair.
The integral function and the trapz (or incremental cumtrapz) functions do different things depending on what the data are and what result you want.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 7월 14일
By default, integral calls the integrand function you pass into it with an array of data, not just a scalar. It requires your integrand function to return an array the same size as the input as output. Each element of the output represnts the value of your integrand function at the corresponding element of the input.
In your code:
t=0:1:10;
x=0:1:10;
[time space]=meshgrid(t,x);
fun=@(c) time+space+c;
that would only work if c was compatibly sized with time and space. One example of a c that's compatibly sized is a scalar.
fun(42)
ans = 11×11
42 43 44 45 46 47 48 49 50 51 52 43 44 45 46 47 48 49 50 51 52 53 44 45 46 47 48 49 50 51 52 53 54 45 46 47 48 49 50 51 52 53 54 55 46 47 48 49 50 51 52 53 54 55 56 47 48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 58 49 50 51 52 53 54 55 56 57 58 59 50 51 52 53 54 55 56 57 58 59 60 51 52 53 54 55 56 57 58 59 60 61
One example of a c that's not compatibly sized with time and space is a 1-by-2 vector. Adding an 11-by-11 matrix and a 1-by-2 matrix is not mathematically defined.
fun([42, -1])
Arrays have incompatible sizes for this operation.

Error in solution (line 4)
fun=@(c) time+space+c;
But you have no control over the size of the data with which integral calls your function, unless you explicitly tell it "My function returns an array of data, so just call it with a scalar each time". That's what the 'ArrayValued' name-value argument tells integral if you specify that the value of that name-value argument as 1.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by