non linear fitting with subroutine

1 view (last 30 days)
Hi everybody,
I am trying to make a fit with a non linear recursive function but it appears that my code does not work.
The code is as follow
close all
clear
load 'teffe_10jumps_from_393_tann100sec_withtau.txt'
teffe_10jumps_from_393_tann100sec_withtau;
t=teffe_10jumps_from_393_tann100sec_withtau(:,1);
T=teffe_10jumps_from_393_tann100sec_withtau(:,2);
teffe=teffe_10jumps_from_393_tann100sec_withtau(:,5);
tau=teffe_10jumps_from_393_tann100sec_withtau(:,4);
A=exp(-274);
x=0.35;
C=1e5;
beta=0.25;
t_ann=t(1:211);
T_ann=T(1:211);
teffe_ann=teffe(1:211);
tau_ann=tau(1:211);
b0=[exp(-274),0.25,1e5,0.35];
lb=[0,0,1e3,0];
ub=[1e-100,1,1e8,1];
f=@(b) teffefit_rout(t_ann,T_ann,b);
z=@(b) norm(teffe_ann(11:end)-f(b));
problem = createOptimProblem('fmincon', 'x0',b0, 'objective',z,'lb',lb,'ub',ub);
gs = GlobalSearch('PlotFcns',@gsplotbestf);
[b,fval] = run(gs,problem)
The subroutine teffefit_rout is reported below
function [Tf]=teffefit(t,T,b)
Dt0=diff(t);
Dtin=0;
Dt=[0 Dt0'];
DT0=diff(T);
DTin=0;
DT=[DTin DT0'];
teffe=zeros(length(t),1);
tau0=zeros(length(t),1);
teffe(1)=T(1);
tau0(1)= b(1)*exp(b(2)*b(3)/T(1)+(1-b(2))*b(3)/teffe(1));
for i=2:length(t);
tau0(i)=@(T) b(1)*exp(b(2)*b(3)/T+(1-b(2))*b(3)/teffe(i-1));
s2=0;
for j=2:i;
s1=0;
for h=j:i;
s1=s1+Dt(h)/tau0(h);
end;
s2= s2+DT(j)*(1-exp(-(s1^b(4))));
end;
teffe(i)=teffe(1)+s2;
end;
Tf=teffe(11:end);
All functions in a script must be closed with an 'end'.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
How can I fix the problem in my code. Attached there is the file that I load to make the test.
Thank you!
  5 Comments
Adam Danz
Adam Danz on 6 Aug 2022
ah, I see that comment now. Thanks @Torsten.
After I add the "end" and change the function name, the next error I see is a "Conversion to double from function_handle" error but I'll let the OP pick it up from here and add some more information.
Daniele Sonaglioni
Daniele Sonaglioni on 8 Aug 2022
Edited: Daniele Sonaglioni on 8 Aug 2022
Hi @Adam Danz, sorry for the poorness of my previous explanation.
I am trying to optimize the function in teffefit_rout: it is an iterative function that compute the array Tf, given other two array namen t_ann and T_ann. I want to optimize the array Tf on some simulated data, that in the main code are named as teffe_ann.
The problem is that it seem that the code do not responds to changes in the initial conditions (named b0 in the main code). I also know what are the correct parameter, and they are reported as well, in the main code and are named A, x, C, beta. In general, the fit does not work because it is unable to give me the correct parameters of the model.
I don't know how to modify the main code or the subroutine to make the fit working.

Sign in to comment.

Accepted Answer

Varun
Varun on 26 Oct 2023
Hi Daniele,
Looks like you are facing errors while executing the MATLAB code snippet which you have provided.
I have debugged the code and figured out the issues. After fixing following issues, the code executed successfully.
  1. Rename function name "teffefit " to "teffefit_rout" as you are calling by this name.
  2. You have declared "tau0=zeros(length(t),1);" which means that "tau0" is a column vector of length "t" with all the values initialized to zero. But later you are trying to initialize these values with a function handle in "tau0(i)=@(T) b(1)*exp(b(2)*b(3)/T+(1-b(2))*b(3)/teffe(i-1));" which is not allowed. So, replace this line with something like "tau0_func_handle=@(T) b(1)*exp(b(2)*b(3)/T+(1-b(2))*b(3)/teffe(i-1));" and use "tau0_func_handle(h)" instead of "tau0(h)" as "s1=s1+Dt(h)/ tau0_func_handle(h);".
  3. Put an "end" after "Tf=teffe(11:end);" for the function "teffefit_rout".
  4. Avoid unnecessary semi-colons.
Please find below the updated code which is running without any errors:
close all
clear
load 'teffe_10jumps_from_393_tann100sec_withtau.txt'
teffe_10jumps_from_393_tann100sec_withtau;
t=teffe_10jumps_from_393_tann100sec_withtau(:,1);
T=teffe_10jumps_from_393_tann100sec_withtau(:,2);
teffe=teffe_10jumps_from_393_tann100sec_withtau(:,5);
tau=teffe_10jumps_from_393_tann100sec_withtau(:,4);
A=exp(-274);
x=0.35;
C=1e5;
beta=0.25;
t_ann=t(1:211);
T_ann=T(1:211);
teffe_ann=teffe(1:211);
tau_ann=tau(1:211);
b0=[exp(-274),0.25,1e5,0.35];
lb=[0,0,1e3,0];
ub=[1e-100,1,1e8,1];
f=@(b) teffefit_rout(t_ann,T_ann,b);
z=@(b) norm(teffe_ann(11:end)-f(b));
problem = createOptimProblem('fmincon', 'x0',b0, 'objective',z,'lb',lb,'ub',ub);
gs = GlobalSearch('PlotFcns',@gsplotbestf);
[b,fval] = run(gs,problem)
function [Tf]=teffefit_rout(t,T,b)
Dt0=diff(t);
Dtin=0;
Dt=[0 Dt0'];
DT0=diff(T);
DTin=0;
DT=[DTin DT0'];
teffe=zeros(length(t),1);
tau0=zeros(length(t),1);
teffe(1)=T(1);
tau0(1)= b(1)*exp(b(2)*b(3)/T(1)+(1-b(2))*b(3)/teffe(1));
for i=2:length(t)
tau0_func_handle=@(T) b(1)*exp(b(2)*b(3)/T+(1-b(2))*b(3)/teffe(i-1));
s2=0;
for j=2:i
s1=0;
for h=j:i
s1=s1+Dt(h)/tau0_func_handle(h);
end
s2= s2+DT(j)*(1-exp(-(s1^b(4))));
end
teffe(i)=teffe(1)+s2;
end
Tf=teffe(11:end);
end
Here is the output of above corrected code:

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!