non linear fitting using two input variables and seven parameters

7 views (last 30 days)
Hi,
I have developped a program to fit my expeirmental data to a model, the program run, but the fit is just digusting (and I have an exiflag=1 that does not make any sense)
Here is the main :
clc % clear command window
clear all % efface toutes les variables
close all % ferme toutes les figures
% load NiCr.txt
load Pt.txt
% load NiCup.txt
Pot= Pt(:,1);
CurExp=Pt(:,2);
% Pot= NiCr(:,1);
% CurExp=NiCr(:,2);
% Pot= NiCup(:,1);
% CurExp=NiCup(:,2);
x0=[0.000000001 0.00000000001 0.5 0.5]; % first vector to be adjusted if necessary
%ub mean upper limit, values can be played with, if necessary
ubk1 = 0.0001;
ubk2 = 0.0001 ;
ubalpha = 1 ;
ConC = H2conc(CurExp) ;
Longueur= size(Pot);
for i = 1 : Longueur
x = [ Pot(i),ConC(i) ] ;
Input(i,:)=x ;
end
% % BEFORE RUNNING PROGRAM, ENSURE THAT THE TEMPERATURE IS CHANGED IN THE
% % FOUR FUNCTIONS
options = optimset('Tolfun',1e-40);
[sol, resnorm,residual,exitflag,output]=lsqcurvefit( 'Model', x0, Input,CurExp,[0 0 0 0],[ubk1 ubk2 ubalpha ubalpha],options)
CurCal =Model(sol, Input);
% % VthetaS=thetaS(sol, Pot);
% % VthetaH=thetaH(sol, Pot);
% VthetaHydride=thetaHydride(sol, Pot);
% VthetaSH=thetaSH(sol, Pot) ;
% VthetaSH2=thetaSH2(sol, Pot) ;
hold all
plot(Pot, CurCal, 'g*-')
plot(Pot, CurExp, 'r+')
title('Comparaison modele vs experience');
xlabel('Potential');
ylabel('Current');
% outputdata = [Pot CurExp CurCal residual VthetaS VthetaH VthetaHydride VthetaSH VthetaSH2];
And the functions I have created :
model.m
function [Y] = Model(x, Input)
k1 = x(:,1);
k2 = x(:,2);
alphao1 = x(:,3);
alphao2 = x(:,4) ;
Pot= Input(:,1) ;
ConC= Input(:,2);
% H2conc = 0.0017.*(Pot).^4-0.0034.*(Pot).^3+0.0003.*(Pot).^2-9.*10.^-6.*Pot+10.^-7 ;
% H2conc = 7.29475235532999e-08 ;
Eth1 = 0; %Value for volmer and heyvroskuy
tau = 2.2.*10.^-9; %enter here the value of tau determined for the catalyst, at the correct temperature
temp = 298;% adjust here the value of the temperature used for the experiment in Kelvin
f = 96500./(8.314.*temp);
Var1 = k1.*exp(alphao1.*f.*(Pot+Eth1)) ; % Ko1
Var2 = k2.*exp(alphao2.*f.*(Pot+Eth1)) ; %KO2
Y = (2.*tau.*96500.*Var1.*Var2)./(1./ConC + Var2./ConC + Var1) ;
end
and H2conc.m
function [Z] = H2conc(CurExp)
m = 0.62.*(5.11.*10.^-5).^(2./3).*12.94.*(10.^-2).^(-1/6); % D taken from Difusion of H2 in water at 298K ; kinematic viscosity of water at 298 K
idm = 3.72E-04 ;
Z =(idm-CurExp)./(96500.*m) ;
end
When I run the program, it is saying that the program have found a local minimum, but here is the comparison between the experimental data (red) and the fit (green)
The fit is obviously not good.
I have tried to modify the program and changing he upper and lower bounds, as well as x0, but it always give me that disgusting fit.
Any suggestions?
Thanks for your help and your time!
Remi
  2 Comments
John D'Errico
John D'Errico on 25 Jan 2019
Edited: John D'Errico on 25 Jan 2019
We don't have your data. How can we help you if you provide only part of the information, but none of the important stuff?

Sign in to comment.

Accepted Answer

Remi Blanchard
Remi Blanchard on 25 Jan 2019
Hi all, I have re-used my program on a dummy two variables function, and it worked perfectly
The problem seems to be linked to an incomplete model, which I am currently refining.
I am posting the dummy program here if it can help anyone in the future.
Here is the main:
% main of fitting program
clc % clear command window
clear all % efface toutes les variables
close all % ferme toutes les figures
load test.txt
X= test(:,1);
Y= test(:,2) ;
CurExp=test(:,3);
x0=[1 0.5 0.5 0.5]; % first vector to be adjusted if necessary
%ub mean upper limit, values can be played with, if necessary
ubk1 = 20;
ubk2 = 20 ;
ubalpha = 15 ;
Longueur=size(CurExp);
for i = 1 : Longueur
x = [ X(i), Y(i) ] ;
Input(i,:)=x ;
end
% % BEFORE RUNNING PROGRAM, ENSURE THAT THE TEMPERATURE IS CHANGED IN THE
% % FOUR FUNCTIONS
options = optimset('Tolfun',1e-40);
[sol, resnorm,residual,exitflag,output]=lsqcurvefit( 'Model', x0, Input,CurExp,[0 0 0 0],[ubk1 ubk2 ubalpha ubalpha],options)
CurCal =Model(sol, Input);
hold all
plot(X, CurCal, 'g*-')
plot(X, CurExp, 'r+')
title('Comparaison modele vs experience');
xlabel('Potential');
ylabel('Current');
Here is the code for the dummy function :
function [Y] = Model(x, Input)
A = x(:,1);
B = x(:,2);
C = x(:,3);
D = x(:,4) ;
X= Input(:,1) ;
Y= Input(:,2);
Var1 = A.*X + B.*Y ;
Var2 = C.*Y + D./Y ;
Y = Var1./Var2 ;
end
The picutre of the fitted data is attached to this text, as well as the data used for the fitting.
I wish you all a nice day!
Remi

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!