Fit a line and integration

5 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
Walter Roberson
Walter Roberson on 5 Oct 2022
When you use the three-output version of polyfit() then to improve precision, polyfit normalizes the independent variable by mean and standard deviation and fits the normalized variable. This can help considerably in reducing numeric errors.
Imagine for example that your independent variables were 10^10, 10^10+10^8, 10^10+2*10^8, 10^10+3*10^8 and so on, then when you go to take x^4 then the 10^10 part is going to get ^4 and you start getting values in the 10^40 range, and the 10^8 contribution would only go to 10^32... you lose a lot of precision. Whereas if you take the independent variable and normalize it you would be dealing with values on the order of +/- 2 .
Anyhow when you use this form, polyfit writes the mean and standard deviation into the third output; when you are building your own equation using the coefficients returned by polyfit you have to normalize the independent variable yourself.
When you use fit() with 'normalize' option, fit() does the same mapping and records properties named meanx and stdx in the cfit object that is being returned... but I cannot find any supported methods for extracting the values of those properties. There are unsupported methods involving using struct(), but those involve a warning message and adding the code to deal with that just doesn't seem worthwhile considering that using polyfit() with no warning works just as well.
Sam J
Sam J on 5 Oct 2022
Thanks for complete answer!

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!