non linear fitting with subroutine
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daniele Sonaglioni
le 5 Août 2022
Réponse apportée : Varun
le 26 Oct 2023
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);
How can I fix the problem in my code. Attached there is the file that I load to make the test.
Thank you!
5 commentaires
Réponse acceptée
Varun
le 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.
- Rename function name "teffefit " to "teffefit_rout" as you are calling by this name.
- 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);".
- Put an "end" after "Tf=teffe(11:end);" for the function "teffefit_rout".
- 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:
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!