how to obtain a fit with nonlinear regression data?

8 Ansichten (letzte 30 Tage)
andrea rigotto
andrea rigotto am 8 Nov. 2023
Kommentiert: Peter Perkins am 10 Nov. 2023
EDIT: modified your code so that it can run here
Hello everyone, I'm trying to get a graph with a curve that fits a series of data obtained from a non-linear regression, but I can't find the error. The error occur when i try to polt figure(1) between lines 90-100.
%% author: Andrea Rigotto
clc
clear all
close all
%%
%read all .xlsx file inside the fwolder
%cicle for obtain the sample data content directly from the .xlsx file
table = readtable('P_T_Ni.xlsx');
temperature(:,1) = table.temperature;
pressure(:,1) = table.pressure;
Ni_grt(:,1) = table.Ni_Grt;
Ni_ol(:,1) = table.Ni_Ol;
Cr_grt(:,1) = table.Wt_Cr;
Ca_grt(:,1) = table.Wt_Ca;
%% calculation
%Keq
Ni_grt = Ni_grt(~isnan(Ni_grt), :);
Keq= Ni_grt ./2900;
%temperature from °C to K
temperature = temperature +273.15 ;
%Ni olivine mean
%Ni_ol = Ni_ol(~isnan(Ni_ol), :);
%mean_Ni_ol = mean(Ni_ol);
%% ignore the NaN value
temperature = temperature(~isnan(temperature), :);
pressure = pressure(~isnan(pressure), :);
Ca_grt = Ca_grt(~isnan(Ca_grt), :);
Cr_grt = Cr_grt(~isnan(Cr_grt), :);
%% Calculate the parameters (ΔH, ΔV, ΔS)
pressure_keq=[pressure Keq];
% function of T
T = @(x,pressure_Keq) (x(1) + pressure_Keq(:,1) * x(2)) ./ (x(3) - log( pressure_Keq(:,2)));
% fixed start parameters (ΔH, ΔV, ΔS)
% The chosen values must be of the same order of magnitude as the expected values
x0 = [1000, 10, 1];
% nonlinear regression with lsqcurvefit
x_fit = lsqcurvefit(T, x0, pressure_keq, temperature);
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.
% Extract the fitted parameters
DeltaH = x_fit(1);
DeltaV = x_fit(2);
DeltaS = x_fit(3);
% the new value od temperature from Ni_grt of database
predicted_temperature = T(x_fit, pressure_keq);
% print the result
fprintf('Parametri adattati:\n');
Parametri adattati:
fprintf('ΔH = %.2f\n', DeltaH);
ΔH = 7737.42
fprintf('ΔV = %.2f\n', DeltaV);
ΔV = 26.79
fprintf('ΔS = %.2f\n', DeltaS);
ΔS = 2.60
%fprintf('Ni_ol = %.2f\n', mean_Ni_ol);
%% diff T(Ni-in-grt)-T(TA98)
deltaT = predicted_temperature-temperature;
mean_deltaT = mean(deltaT);
fprintf('mean_detaT = %.2f\n', mean_deltaT);
mean_detaT = 0.26
%% 1 sigma statistics
%calculate standard deviation
sigma_value=std(deltaT);
fprintf ('1σ of deltaT=%.2f\n', sigma_value);
1σ of deltaT=47.81
%% R^2
R_sq= 1-var(temperature-predicted_temperature)/var(temperature);
fprintf('R^2=%.2f\n',R_sq);
R^2=0.91
%% T°C Ryan 1996
T_Ryan = (1000 ./(1.506-0.184*log(Ni_grt)))-273;
%% T°C Canil 1999
T_canil = 8772 ./(2.53-log(Keq))-273;
%% T°C sudholz 2021
T_sudholz = -8254.568 ./((Ca_grt*3.023)+(Cr_grt*2.307)+(log(Keq)-2.639))-273;
%% graphs
%transform the pressure from Kbar to Gpa
pressure= pressure/10;
%trasform the Temperature from k to °C
temperature = temperature-273.15;
% comparison between T_canil, T_Ryan, T_Sudholz and us
figure(1);
fo = fitoptions('Method','NonlinearLeastSquares');
ft = fittype(8772 ./(2.53-log(Keq))-273,'Keq','options',fo);
Error using fittype>iCreateFittype
First input argument must be a string scalar or character vector, cell array or anonymous function.

Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
[fit_canil,gof]= fit(pressure,T_canil,ft,'Keq',2);
plot(fit_canil, pressure,'m')
xlabel('Pressure (GPa)')
ylabel('Temperature (°C)');
  2 Kommentare
Peter Perkins
Peter Perkins am 10 Nov. 2023
Don't do this:
table = readtable('P_T_Ni.xlsx');
table is the name of a class. Eventually, this variable name will come back to bite you. Name it t.
Peter Perkins
Peter Perkins am 10 Nov. 2023
You are using fit from Curve Fitting tbx. You might also look into using fitnlm from Statistics & Machine Learning, which supports fitting using data stored in a table. No need to extract things.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 8 Nov. 2023
The equation needs to be entered as a char array or string. You also need to have Name-Value pairs for the optional inputs. See this example: https://www.mathworks.com/help/curvefit/fittype.html#btpaend-8
I think this is what you want
ft = fittype("8772./(2.53-log(Keq))-273",'independent','Keq','options',fo);
However, note that this will still give you an error. The reason is that, if Keq is your independent variable, there are no other variables for MATLAB to adjust. What is the atual equation you are trying to fit to your data? What are your dependent and independent variables?
  13 Kommentare
Cris LaPierre
Cris LaPierre am 9 Nov. 2023
You are asking questions that only you can answer. Based on your knowlege in this space, what relationship is the data expected to have?
In both cases, the fit comes down to defining the function you want to fit. Coming up with that function is where your expertise comes in.
If the code is getting in the way, try taking a more interactive approach using the Curve Fitter App.
Again, if you are trying to derive the parameters of an equation to calculate temperature from pressure and Keq, you must first have existing data for temperature at known pressure and Keq values.
andrea rigotto
andrea rigotto am 9 Nov. 2023
thanks you, i have undertood that i need more knowledge about statistics, and i need to do more test to choising the correct functions.
now that i have more knowledge abot my study case, i have understood that what i want to do is plot graph with trendlines about differents temperature equation. Now i need to understand how to do in my case and which is the function to do this.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by