difficulties using fmincon codes

4 vues (au cours des 30 derniers jours)
Somnath Kale
Somnath Kale le 6 Fév 2024
Commenté : Somnath Kale le 8 Fév 2024
Hello
I have follwing otimization code with function which works ith online matlab but nit in may live editor of matlabR2021a:
can someone plese help with resolution or suggesting simple combine code merging both function and program
fitting code:
clear;
a0=[1;1e-6];
options=optimset('Display','notify'); %suppress console messages unless fail to converge
[a,sse]=fmincon(@SomnathsFunc2,a0,[],[],[],[],[],[],[],options);
%Display results
w=a(1);
t1=a(2);
fprintf('Best fit: SumSqError=%.3f, w=%.3f, t1=%.8f\n',sse,w,t1);
Best fit: SumSqError=0.020, w=1.036, t1=0.00000330
%plot results
figure;
t = [1E-7 2E-7 5E-7 1E-6 2E-6 5E-6 1E-5 2e-5 5e-5 1e-4 2e-4 5e-4 1e-3 2e-3 5e-3];
p = [0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856];
ppred=zeros(1,length(p));
A=1;
fun=@(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x))^2))/((x-log(t1))^2+w^2);
for i=1:length(t)
ppred(i)=integral(@(x)fun(x,t(i),A,w,t1),-Inf,Inf,'ArrayValued',1);
end
semilogx(t,p,'rx',t,ppred,'b.-');
xlabel('t');
ylabel('p(t)');
legend('Data','Fit');
grid on;
function:
function sse=SomnathsFunc2(a)
%Somnath Kale's function to be minimized, 2021-06-27
%This function written by WCR, using Somnath's data and function he provided.
%a=vector of parameters to be adjusted: a(1)=w, a(2)=t1
%v.2: Using new data and new equations, posted 6/26/2021 by Smonath Kale on
%Matlab Answers.
t = [1E-7 2E-7 5E-7 1E-6 2E-6 5E-6 1E-5 2e-5 5e-5 1e-4 2e-4 5e-4 1e-3 2e-3 5e-3];
p = [0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856];
ppred=zeros(1,length(p));
w=a(1);
t1=a(2);
A=1;
fun=@(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x))^2))/((x-log(t1))^2+w^2);
for i=1:length(t)
ppred(i)=integral(@(x)fun(x,t(i),A,w,t1),-Inf,Inf,'ArrayValued',1);
end
sse=sum((p-ppred).^2);
end
  4 commentaires
Somnath Kale
Somnath Kale le 6 Fév 2024
Modifié(e) : Somnath Kale le 6 Fév 2024
@Matt J thank you!
plese find the error msg:
Unrecognized function or variable 'getIpOptions'.
Error in fmincon (line 859)
options = getIpOptions(options,sizes.nVar,mEq,flags.constr,defaultopt,10,0.01);
I will be beneficial if suggests a simple code without use of function. Im bit unconfertable with function uses.
Somnath Kale
Somnath Kale le 6 Fév 2024
Modifié(e) : Somnath Kale le 6 Fév 2024
@Avni Agrawal Im not sure then what is problem. I was thinking is it may be because my installed version of matlab which contains few products only (matlab, intrument control toolbox, signal processing tool box, and optimazation tool box only). I have attached the error msg herewith for your referance.

Connectez-vous pour commenter.

Réponse acceptée

Catalytic
Catalytic le 6 Fév 2024
Modifié(e) : Catalytic le 6 Fév 2024
however it still uses @SomnathsFunc2 i would be happy to exclude rather than this cant we direclty add that part in code rather than calling function.
It would be silly to remove it, since it makes your code more readable. However, you can shorten the code by using lsqcurvefit instead of lsqnonlin -
clear;
t = [1E-7 2E-7 5E-7 1E-6 2E-6 5E-6 1E-5 2e-5 5e-5 1e-4 2e-4 5e-4 1e-3 2e-3 5e-3];
p = [0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856];
% Define initial guess for parameters
a0 = [1; 5e-5];
% Define options for lsqnonlin
options = optimoptions('lsqcurvefit','Display','final');
% Perform optimization using lsqnonlin
a = lsqcurvefit(@SomnathsFunc2,a0,t,p,[],[],options);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% Display results
w = a(1);
t1 = a(2);
fprintf('Best fit: w=%.3f, t1=%.8f\n', w, t1);
Best fit: w=1.036, t1=0.00000330
% Plot results
semilogx(t, SomnathsFunc2(a,t),'b.-',t,p,'xr');
xlabel('t'); ylabel('p(t)'); legend('Data', 'Fit'); grid on;
function ppred = SomnathsFunc2(a,t)
ppred = zeros(1, length(t));
w = a(1);
t1 = a(2);
A = 1;
fun = @(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x)).^2))./((x-log(t1)).^2+w^2);
for i = 1:length(t)
ppred(i) = integral(@(x)fun(x,t(i),A,w,t1),-Inf,Inf,'ArrayValued',1);
end
end

Plus de réponses (2)

Avni Agrawal
Avni Agrawal le 6 Fév 2024
Modifié(e) : Avni Agrawal le 6 Fév 2024
Hi Somnath,
I understand that you're encountering an error related to the `fmincon` function. To address this issue effectively, it's essential to confirm whether the Optimization Toolbox is installed on your machine.
You can verify the presence of the Optimization Toolbox by following these steps:
1. Check for the existence of the file 'getIpOptions.m' in the directory specified below:
winopen(fullfile(matlabroot,'toolbox\optim\optim'))
This command should open the directory where the function is stored. Look for the 'getIpOptions.m' file in that directory.
2. If the file exists, but the problem persists, it's possible that the path to the file has been somehow removed. Restart MATLAB and see if the issue resolves. Additionally, check your 'startup.m' file (if it exists) to ensure that important paths are not being removed. You can also try running `restoredefaultpath`.
3. If the 'getIpOptions.m' file does not exist in the specified directory, it indicates that the Optimization Toolbox may not be installed on your system. In such a scenario, consider reinstalling the Optimization Toolbox to resolve the issue.
I hope this helps.
  4 commentaires
Avni Agrawal
Avni Agrawal le 6 Fév 2024
If you want to find the best fit parameters without using `fmincon`, you can directly minimize the sum of squared errors (SSE) using other optimization methods or techniques. One common approach is to use MATLAB's built-in optimization functions such as `lsqnonlin`, which is commonly used for nonlinear least squares optimization.
Here's how you can modify the code to use `lsqnonlin`:
clear;
% Define initial guess for parameters
a0 = [1; 5e-5];
% Define options for lsqnonlin
options = optimoptions('lsqnonlin','Display','iter');
% Perform optimization using lsqnonlin
a = lsqnonlin(@SomnathsFunc2,a0,[],[],options);
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
First-order Norm of Iteration Func-count Resnorm optimality Lambda step 0 3 1.31419 1.75e+04 0.01 1 17 0.813953 1.75e+04 1e+09 1.42146e-05 2 20 0.382598 1.6e+04 1e+08 1.27183e-05 3 23 0.00208238 2.56e+03 1e+07 2.08443e-05 4 26 0.000476598 189 1e+06 8.12187e-07 Local minimum possible. lsqnonlin stopped because the relative size of the current step is less than the value of the step size tolerance.
% Display results
w = a(1);
t1 = a(2);
fprintf('Best fit: w=%.3f, t1=%.8f\n', w, t1);
Best fit: w=1.000, t1=0.00000303
% Plot results
figure;
t = [1E-7 2E-7 5E-7 1E-6 2E-6 5E-6 1E-5 2e-5 5e-5 1e-4 2e-4 5e-4 1e-3 2e-3 5e-3];
p = [0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856];
ppred = zeros(1, length(p));
A = 1;
fun = @(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x)).^2))./((x-log(t1)).^2+w^2);
for i = 1:length(t)
ppred(i) = integral(@(x)fun(x,t(i),A,w,t1),-Inf,Inf,'ArrayValued',1);
end
semilogx(t, p, 'rx', t, ppred, 'b.-');
xlabel('t');
ylabel('p(t)');
legend('Data', 'Fit');
grid on;
function sse = SomnathsFunc2(a)
% Somnath Kale's function to be minimized, 2021-06-27
% This function written by WCR, using Somnath's data and function he provided.
% a = vector of parameters to be adjusted: a(1) = w, a(2) = t1
% v.2: Using new data and new equations, posted 6/26/2021 by Somnath Kale on MATLAB Answers.
t = [1E-7 2E-7 5E-7 1E-6 2E-6 5E-6 1E-5 2e-5 5e-5 1e-4 2e-4 5e-4 1e-3 2e-3 5e-3];
p = [0.0824 0.1515 0.2339 0.3229 0.4505 0.6173 0.7434 0.822 0.9151 0.9795 0.982 0.9829 0.9861 0.9846 0.9856];
ppred = zeros(1, length(p));
w = a(1);
t1 = a(2);
A = 1;
fun = @(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x)).^2))./((x-log(t1)).^2+w^2);
for i = 1:length(t)
ppred(i) = integral(@(x)fun(x,t(i),A,w,t1),-Inf,Inf,'ArrayValued',1);
end
sse = sum((p-ppred).^2);
end
In this code, `lsqnonlin` is used to minimize the sum of squared errors directly, without the need for a constrained optimization approach. The optimization is performed by adjusting the parameters `w` and `t1` to minimize the difference between the predicted and observed values of `p`.
Somnath Kale
Somnath Kale le 6 Fév 2024
Déplacé(e) : Matt J le 6 Fév 2024
@Avni Agrawal thank you for your suggested code using lsnonlin function. however it still uses @SomnathsFunc2 i would be happy to exclude rather than this cant we direclty add that part in code rather than calling function. Unfortunatelt that involvemnt of integration is making it bit ununderstandale to me.
Plese suggets if possible!
I apreciate your time and efforts!

Connectez-vous pour commenter.


Somnath Kale
Somnath Kale le 8 Fév 2024
@Avni Agrawal and @Catalytic thanks for your sugestions. both of your answers were realy helpful. But unfortunately matlab dose not allow to accept both the answers. I can accept only one.
  2 commentaires
Matt J
Matt J le 8 Fév 2024
But you can upvote any answer, even ones that you didn't accept, as I have done for @Avni Agrawal just now.
Somnath Kale
Somnath Kale le 8 Fév 2024
Thank you @Matt J.! I Just did it.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by