fminsearch with an integral function

Hello!
I want to minimize a function with an integral using fminsearch. The question is that I dont know how to define it properly.
The function i want to minimize is:
I want to minimize it respective to θ being A, given by the user before any calculation.
How can i define the function and the fminsearch procedure?
Thanks!

 Accepted Answer

See this code
A = 1;
sigma0 = 2;
int_term = @(theta) integral(@(phi) 1/(sigma0*sqrt(2*pi))*exp(-1/2*(phi/sigma0).^2).*sin(theta-phi), 0, 2*pi);
obj_fun = @(theta) -A*cos(theta)+int_term(theta);
theta_sol = fsolve(obj_fun, 0)

8 Comments

I could solve some examples with this but when i try to take a step further I get some errors.
int_term = @(theta,Sigma0) integral(@(phi) 1/(Sigma0*sqrt(2*pi))*exp(-1/2*(phi/Sigma0).^2).**(sin(theta-phi)).^2,0,2*pi);
Func = @(theta,Sigma0,A) -A*cos(theta) + @(theta,Sigma0);
where then i define my variables
A=120;
Sigma0= 1;
and try to solve it
FuncAux = @(theta) Func(theta,Sigma0,A)
theta_sol = fminsearch(Func, 0)
The following error appears
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
I dont understand why it appears in this case.
The final goal is to fit this function to experimental data and extract the value of Sigma0 but first i want to try to plot the minimization with several given A values for constant Sigma 0.
There were some mistakes in the pasted code, I corrected it and i got an asnwer
A=120;
Sigma0= 1;
int_term = @(theta,Sigma0) integral(@(phi) 1/(Sigma0*sqrt(2*pi))*exp(-1/2*(phi/Sigma0).^2).*(sin(theta-phi)).^2,0,2*pi);
Func = @(theta,Sigma0,A) -A*cos(theta) + int_term(theta,Sigma0);
FuncAux = @(theta) Func(theta,Sigma0,A);
theta_sol = fminsearch(FuncAux, 0.1);
Can you exactly paste the code which cause error?
The code I used is this one: (the variables are all with different names as i tied to simplify to post here)
MultAmtoOe= 10^(4) * 4*pi /(10^7);
MultAmtoemucc = 1/(10^3);
mu0=4*pi/(10^7);
MS_AMR = 1320 / MultAmtoemucc;
t_AMR = 20 /(10^10);
KuAVG_AMR=(150 * 10 ^(-4) / mu0) * mu0 * MS_AMR /2;
Sigma_Angle = 55 * pi /180 ;
Sigma_Ku = 0.1* KuAVG_AMR ;
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR*(sin(theta-phi)).^2,0,2*pi);
Hteste=120/MultAmtoOe;
thetaH=90 * pi/180;
fundecoy = @(thetaPL)EnergyFunc1(thetaPL,Sigma_Ku,Sigma_Angle,Hteste,thetaH);
[thetaval,energyval]=fminsearch(fundecoy,0);
The multipilication should be done with .* operator. Simply using * implies matrix multipilcation. Try the following code
MultAmtoOe= 10^(4) * 4*pi /(10^7);
MultAmtoemucc = 1/(10^3);
mu0=4*pi/(10^7);
MS_AMR = 1320 / MultAmtoemucc;
t_AMR = 20 /(10^10);
KuAVG_AMR=(150 * 10 ^(-4) / mu0) * mu0 * MS_AMR /2;
Sigma_Angle = 55 * pi /180 ;
Sigma_Ku = 0.1* KuAVG_AMR ;
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR.*(sin(theta-phi)).^2,0,2*pi);
%^ added this dot
Hteste=120/MultAmtoOe;
thetaH=90 * pi/180;
fundecoy = @(thetaPL)EnergyFunc1(thetaPL,Sigma_Ku,Sigma_Angle,Hteste,thetaH);
[thetaval,energyval]=fminsearch(fundecoy,0);
Thanks for the help!
I have just a question why is it assumed that is a matrix operation?
%v why not added here
EnergyFunc1 = @(theta,SigmaKu,SigmaTheta0,H,thetaH) -mu0*H*MS_AMR*t_AMR*cos(thetaH-theta) + integral(@(phi) 1/(SigmaTheta0*sqrt(2*pi))*exp(-1/2*(phi/SigmaTheta0).^2).*KuAVG_AMR*t_AMR.*(sin(theta-phi)).^2,0,2*pi);
%^ added this dot
Why for example you didnt put the dot in the first term? and only in the last one
The reason is a bit intricate but I can give you general detail. In MATLAB (*) operator is defined for matrix multiplication and (.*) is defined for element-wise multiplication. For a scalar number it does not make any difference. For example, I evalaute following example function without dots
fun = @(x,a,b) a*b*x*x;
fun(2,1,1)
ans =
4
and with dot
fun = @(x,a,b) a*b*x.*x;
fun(2,1,1)
ans =
4
No problem!!! But suppose I want to input x which is a vector. The first one will fail
fun = @(x,a,b) a*b*x*x;
fun([1 2 3],1,1)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns
in the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.
Error in @(x,a,b)a*b*x*x
Because 1x3 matrix cannot multiply with 1x3 matrix according to the rule of matrix multiplication. But element-wise multiplication works
fun = @(x,a,b) a*b*x.*x;
fun([1 2 3],1,1)
ans =
1 4 9
Now consider the function in your question. It gives a scalar function. If you input a scalar value, it will give a scalar output. However, to make the integral() and fminsearch function computationally efficient, MATLAB calls your function with theta and phi as vectors, not scalars. If your function does not support element-wise operations then it will fail for a vector input.
I didn't use .* in the first term because all the term multipliying the cos() function -mu0*H*MS_AMR*t_AMR are themself scalar, so it does not matter wather you use (.*) or (*). You can use either.
To summarize, you should always use element-wise operators when you are trying to integrate a scalar function using integral.
Thanks a lot!
I will progress much faster!!!
Glad to be of help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!