Info

This question is closed. Reopen it to edit or answer.

Problems with vectorization during minimization/integration

1 view (last 30 days)
Hi,
I have a problem with vectorization during the minimization and/or integration in my code. This is probably very specific, but I'm asking anyways in the hope of getting any hints. I'm trying to compute the following loop:
for y = 1:Y
func_min = @(x)( func1(x, y) )
result = fminbnd(func_min, 0, 1)
end
where
function func1(x, y)
func1 = integral(func2, a, b)
and
func2 = @(y)( func3(x, y)*vec )
and
function func3(x, y)
func3 = [x, x^2, x^3, y, y^2, y^3]
Which is to say that I am trying to minimize a function that contains an integral, and the integrand of that integral is in itself a function of the variable I'm integrating over as well as the variable I'm minimizing over. I already got a great answer regarding a problem during the integration step here, and I've rewritten func3 accordingly, so that whatever the integral function passes as argument for y, the following matrix multiplication works. Now however I run into trouble during the minimization step (I think), as the program again exits with a matrix multiplication error. When I check the dimensions of variable x afterwards, I find that it is a 1x40 cell, where each element itself is of dimension 12 10 8 13. I tried to vectorize things using gdivide, gsubtract etc. instead of the regular operators, but it doesn't seem to work.
I understand that this is a somewhat long and specific question, but I'd appreciate any hints!
Thanks,
Nils
  2 Comments
Aniruddha Katre
Aniruddha Katre on 18 Aug 2014
I tried to reproduce the error you are receiving by using the information you provided. But it seems like there is some important information missing from the function definitions you have provided.
  1. What is the value and dimension of the variable "vec"?
  2. What are the values for the variables "a" and "b"?
  3. What changes did you exactly make in "func3"?
Can you provide a more detailed version of the code you are executing as well as the exact error message you are receiving? This information will help resolve the question much faster.
Nils
Nils on 15 Sep 2014
Hi Aniruddha,
thanks for your answer. I tried to keep the example as short as possible, but maybe I was to short. As to your questions:
1. vec is a vector of real numbers, the dimensions are 6x1 - the idea here is that func2 returns a scalar when the inputs to func3, x and y, are scalar.
2. a and b could be any real number, for simplicity you can assume 0 and 1.
3. The changes I've made to func3 are such that when the input x to is a vector, func3 will return a matrix where each of the rows correspond to one element of x. So for scalar x, func3 will return
[x, x^2, x^3, y, y^2, y^3],
while for (say) a 2x1 vector x as input, func3 will return
[x(1), x(1)^2, x(1)^3, y, y^2, y^3; x(2), x(2)^2, x(2)^3, y, y^2, y^3].
Hope this helps!

Answers (0)

Community Treasure Hunt

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

Start Hunting!