difficulties using fmincon codes
조회 수: 4 (최근 30일)
이전 댓글 표시
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);
%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
채택된 답변
Catalytic
2024년 2월 6일
편집: Catalytic
2024년 2월 6일
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);
% Display results
w = a(1);
t1 = a(2);
fprintf('Best fit: w=%.3f, t1=%.8f\n', w, t1);
% 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
댓글 수: 0
추가 답변 (2개)
Avni Agrawal
2024년 2월 6일
편집: Avni Agrawal
2024년 2월 6일
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
Avni Agrawal
2024년 2월 6일
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);
% Display results
w = a(1);
t1 = a(2);
fprintf('Best fit: w=%.3f, t1=%.8f\n', w, t1);
% 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
2024년 2월 8일
댓글 수: 2
Matt J
2024년 2월 8일
But you can upvote any answer, even ones that you didn't accept, as I have done for @Avni Agrawal just now.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


