Fit a line and integration

8 views (last 30 days)
Sam J
Sam J on 5 Oct 2022
Commented: Sam J on 5 Oct 2022
Hi all
I have a data set of solid fraction (fs) as a function of Temperature (T), ( attcahed file"results1'). I would like to calculate the integration of this expression: fs^2/(1-fs)^3, within the temperature range of [558,649] .First, I found the curve fit , fsfit, using poly4 and then converted it to a symbolic variable,fsvart. Then, I develped the equation fss which is basically the expression above, and converted it to a MATLAB function, ht to be integrated. Now, I do not think the fsfit is returning the correct fs value at a given temperature because the integral value is very off from what I expected.
I hope someone can help me here.
clc; close all; clear all;
syms x
% reading the data
filename='Results1.xlsx';
sheet='Sheet1';
fs = xlsread(filename,sheet,'A2:A57');
Temp = xlsread(filename,sheet,'B2:B57');
% find the curve fit
fsfit = fit(Temp,fs,'poly4','normalize','on');
% convert the fs into symbolic variables to perform the integration after
fsvar = subs(str2sym(formula(fsfit)),coeffnames(fsfit),num2cell(coeffvalues(fsfit).'));
vars = sym(indepnames(fsfit));
% Bulding the equation based on fs and inetgration
fss=fsvar^2/(1-fsvar)^3;
ht = matlabFunction(fss);
s1=integral(ht,831,922);

Accepted Answer

Walter Roberson
Walter Roberson on 5 Oct 2022
You are asking for normalization. When you do that, coeffvalues are the coefficients for the normalized independent variable, not for the original variable. However, when you build fsvar you are not taking that into account.
It is not exactly that fsvar itself is wrong in itself, but you need extra steps:
meanx = mean(Temp);
stdx = std(Temp);
fsvar = subs(fsvar, x, (x-meanx)/stdx);
Or you could take a different approach that does not use the curve fitting toolbox:
syms x
% reading the data
filename='Results1.xlsx';
sheet='Sheet1';
fs = xlsread(filename,sheet,'A2:A57');
Temp = xlsread(filename,sheet,'B2:B57');
[p, ~, mu] = polyfit(Temp, fs, 4);
fsvar = subs(poly2sym(p, x), x, (x-mu(1))/mu(2));
fss = fsvar^2/(1-fsvar)^3;
ht = matlabFunction(fss);
s1=integral(ht,831,922);
  3 Comments
Sam J
Sam J on 5 Oct 2022
Thanks for complete answer!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by