How can I effectively integrate a function in Matlab?
Show older comments
Hello everyone! I have a function in Matlab that is a product of 3 other complicated functions, like this: I(A,x)=V(x)*R(x)*F(A,x). I need to integrate the function I(A,x) with respect to the variable x in some interval (ex. x=0..x=5), but for each value of the parameter E in some interval (ex. A=0.1.. A=100).
Can anybody help with how to do this in Matlab?
I think I will need a for-loop that iterates over the parameter A in the specified interval, and then integrate over x, but the problem is that matlab won't evaluate the integral. I have for example tried defining the function as a symfun and then integrate it, but the result is something like: int(....) after a very long period of being "busy".
I have considered using "trapz" but I need one value of the integral over I for every value of A, is that possible?
How do I get Matlab to actually evaluate the integral and how can I make it do it reasonable fast?
Thank you in advance.
Answers (1)
Andrei Bobrov
on 3 Mar 2017
Edited: Andrei Bobrov
on 3 Mar 2017
Small example for using:
V = @(x) 2*x+2;
R = @(x) x.^2;
F = @(A,x) x./A-3;
I = @(A,x) V(x)*R(x)*F(A,x);
A = [.1 10 20;40 50 70 ; 80 90 100];
out = integral(@(x)I(A,x),0,5,'ArrayValued',1)
3 Comments
Small correction:
I = @(A,x) V(x).*R(x).*F(A,x);
"out" will be a 3x3 matrix with
out(i,j)=integral_{0}^{5} V(x)*R(x)*F(A(i,j),x) dx
Best wishes
Torsten.
User-Mia
on 3 Mar 2017
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!