Numerical Integration with Parameters
79 views (last 30 days)
I have an integral that is integrating over x and it also has two parameters, s, and b. I want to tell matlab a bunch of values of s, say 0 to 100 and have it tell me the values of b for which the integral of s, b, and x (integrated with respect to x) =0 (within an arbitrary tolerance). I've played around with the 'integrate' function and 'fzero' but I can't seem to pass in the parameters the right way. Any help appreciated.
John D'Errico on 28 Oct 2016
Edited: John D'Errico on 28 Oct 2016
Tools like integral, integrate, quad, etc., are NUMERICAL integration tools. They cannot be used with general unknown (symbolic) parameters in the kernel or limits. Yes, you can set up a function handle that passes in a known parameter, so at the time you write the function handle, the parameter will not be known.
For example, compute the integral of x^2+a*x+2, between the limits on x of 2 to 10. Then solve for a, such that the integral is 17. Yes, we can do this symbolically, and do so easily. The exact solution is -989/144.
syms x a
P = x^2 + a*x + 2;
solve(int(P,x,[2 10]) == 17)
But can we do it numerically?
The function is a polynomial that is a function of both x and a. At the time of a numerical integration though, we need to have passed in the value of a.
pfun = @(x,a) polyval([1,a,2],x);
fun = @(a) integral(@(x) pfun(x,a),2,10)
fzero(@(a) fun(a) - 17,1)
fzero(@(a) fun(a) - 17,1)
As you can see, fzero agrees with the solution.
If there are TWO unknown parameters however, then you have a problem with TWO unknowns and ONE equation. There is then no unique solution in general, and you will be unable to solve it as I did above. There will be an infinite number of solutions, what one might call a 1-manifold embedded in a 2-dimensional plane. The solution will be a general curve, an implicit function of some shape.
Can you compute that curve suing MATLAB? The symbolic toolbox is your best bet. However, you can also use the contour function to do the work, generating a plot. It will only be an approximation though.
Teja Muppirala on 29 Oct 2016
For example, if the function is f(x) = 1+s*x+b*x^2 and you're integrating from 0 to 1, and you want to find the b (numerically) that makes the integral = 0, when s is varied from 0:100.
b = ;
ic = 0;
sList = 0:100;
opts = optimoptions('fsolve','display','off');
for s = sList
b(end+1) = fsolve( @(b) integral(@(x)1+s*x+b*x.^2,0,1) , ic, opts);
ic = b(end); % Use the last value as an initial condition