Is any way in Matlab to perform definite integral with conditions in the integration domain?

2 次查看(过去 30 天)
Basic example;
f(x)= x^2+3*x for x<100 f(x)=x^3 for x>100; a=10; b=200;
d=integral(f(x), a, b)
Note: this is a basic example, I know for something like the equation above with the int function in symbolic tool box can be resolve easily doing two independent indefinite integrals and then transforms its results in functions and then evaluate those functions (one function in the interval a the other function in the interval b) to obtain the result. However the equation I’m working is very complex, and when I try to find its indefinite integral with the int function, I'm receiving the following warning error " the explicit integral can't been found". For that reason I’m trying to figure out if the integral function could work having some limitation in the integration domain.
I will appreciate whatever help that can point me out to resolve this issue. Thanks

采纳的回答

Mike Hosea
Mike Hosea 2014-1-27
编辑:Mike Hosea 2014-1-27
I'm not sure I understand your clarification correctly. Here is my best guess
>> f = @(x)(x.^2+3*x).*(x <= 100) + x.^3.*(x > 100);
>> Q1 = @(c)integral(f,10,c,'Waypoints',100);
>> Q2 = @(c)integral(f,c,200,'Waypoints',100);
Then you can calculate the whole integral using Q = Q1(200) = Q2(10), and you can also calculate the two pieces Q1(c) + Q2(c) = Q for any c. The use of Waypoints is for efficiency. If any of the pieces become nonfinite while the other interval is active and finite, you may need to write a MATLAB function instead of using the logical masking technique above, say fun.m:
function y = fun(x)
y = zeros(size(x),'like',x);
for k = 1:numel(x)
if x(k) <= 100
y(k) = x(k)^2 + 3*x(k);
else
y(k) = x(k)^3;
end
end
Then Q1 and Q2 would be written
>> Q1 = @(c)integral(@fun,10,c,'Waypoints',100);
>> Q2 = @(c)integral(@fun,c,200,'Waypoints',100);
It occurs to me that you may mean to integrate the pieces as "basis functions" with finite support, in which case you would define two different functions to integrate in Q1 and Q2, respectively, each defined to be zero outside of their support, either using logical masking or using the same MATLAB function approach with if/else/end.

更多回答(1 个)

Wayne King
Wayne King 2014-1-27
编辑:Wayne King 2014-1-27
Using integral()
integral(@(x) x.^2+3*x,10,100)
integral(@(x) x.^3,100,200)
You just integrate your first function from 10 to 100 since it is defined for x<100. x^2+3*x
Integrate the second function from 100 to 200 because it is defined for x>100, x^3
  1 个评论
Oscar
Oscar 2014-1-27
编辑:Oscar 2014-1-27
Thanks Wayne,
But I forgot explain something my interest is obtain the result only from the lower integration interval in the first f(x) and only the result from the upper integration integral in the second function. Something like
f(x)= x^2+3*x for x<100 f(x)=x^3 for x>100; a=10; b=200;
d= integral = (x^2+3*x, a, NaN or whatever can be used as dummy load) e=integral = (x^3, NaN,b)
For that reason I was trying to use the int functions instead the integral, because with the int function I can obtain the indefinite integral of each function independently and then use each results as functions. Example
d=int(x^2+3*x,x) e=int(x^3,x) g= matlabFuction(d) i= matlabFuction(e) j=g(10) k=i(200)
but I can't resolve my issue using the int function, because when I tried to obtain the indefinite integral of my equation I receive the following warning " the explicit integral can't been found". I’m assuming this error is due the equation is very complex.
Again I appreciate whatever help that can point me out to resolve this issue. Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by