# Fit a line and integration

5 views (last 30 days)
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
filename='Results1.xlsx';
sheet='Sheet1';
% 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);

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
filename='Results1.xlsx';
sheet='Sheet1';
[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);
Sam J on 5 Oct 2022

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!