Double Integral with interpolateSolution() from PDE solver in MATLAB

I have results from the PDE solver in MATLAB and I need to a double integral, I would assume by using the integral2() function.
The problem is I cannot form an input that is acceptable by the integral2 function, directly that is. I have tried using an anonymous function like this
f = @(x,y) interpolateSolution(result, x, y)
And this works perfectly but when I do integration on it.
integral2(f, 0, 5, 0, 8)
I get an error like this
Error using integral2Calc>integral2t/tensor (line 241)
Integrand output size does not match the input size.
I do not understand why I can't do this. I've been able to perform a fit using the toolbox and get a fit for the solution for which then I can create an anonymous function and perform integration. It works when using the fit, but not when I use the interpolateSolution() function. I would settle for the fit, but it takes too much time to solve the PDE, fit it, and perform integration and I'm trying to do an optimization study. Is there a better way to do integration on the results directly from PDE solver?

回答(2 个)

From the documentation:
The function f must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations.
Is this the case for your function "interpolateSolution" ?
Best wishes
Torsten.

5 个评论

Hi thanks for the answer. To be clear, the function interpolateSolution is not my function but a built in function. On top of that, I believe it can two arrays of the same size and return an array of corresponding values, such as the example here (computed)
>>> interpolateSolution(result, [1.5 2.5 3.5], [1.25 2.25 3.5])
ans =
0.0993
0.2927
0.4495
Even when evaluated from the anonymous function handle, it works the same.
These arrays do not seem to be the same size: the output is a column vector, while the inputs are row vectors. Perhaps all you need to do is transpose the output.
Alan Weiss
MATLAB mathematical toolbox documentation
Yes, that was my bad. But even when I transpose the solution it doesnt work. For example,
>> f = @(x,y) interpolateSolution(result, x, y)';
>> f([3.5 2.5],[1.25 4.25])
ans =
0.0968 0.0828
And it still gives me the error when I try to integrate.
>> integral2(f, 0, 5, 0, 3)
Error using integral2Calc>integral2t/tensor (line 241)
Integrand output size does not match the input size.
Am I making some sort of a mistake here?
I don't really know what is going on. But maybe you can ensure that the returned function values have the correct size by this sort of function:
function f = intfun(x,y,result)
f = interpolateSolution(result,x,y);
f = reshape(f,size(x));
Call the function like this, assuming that result is in your workspace:
integral2(@(x,y)intfun(x,y,result),0,5,0,3)
Or maybe, in a single anonymous function,
f = @(x,y)reshape(interpolateSolution(result,x,y),size(x));
But as I say, this may fail because I don't really know what is going on.
Alan Weiss
MATLAB mathematical toolbox documentation
I have the same problem. Using the reshape the interpolation finds a value without an error, but I don't know if it is the right solution. Is there any new information on why this error occurs?

请先登录,再进行评论。

This is the recommendation from the interpolateSolution page:
interpolateSolution converts query points to column vectors xq(:), yq(:), and (if present) zq(:). The returned solution is a column vector of the same size. To ensure that the dimensions of the returned solution are consistent with the dimensions of the original query points, use reshape. For example, use uintrp = reshape(gradxuintrp,size(xq)).

Community Treasure Hunt

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

Start Hunting!

Translated by