Finding most linear region of curve + best linear fit at that point + error + plotting

Hi there, in the first part of my problem I am looking to find the most linear region of a curve; I tried using the "erf", "polyfit" and "polyval" functions to get a fit for my data but the erf function just gave me a n by 1 matrix of "1's", so I have tried to go the long way round by determining the first and second differentials of my data and find the range over which the minimum rate of change occurs but am having trouble "finding" the most linear section... as you will be able to see from my code I am attempting to use "findstr" to search the 2nd differential for a region that is suitably linear but I cannot get it to search for a region in the 2nd differential which is less than or equal to the "lims" (or limits) matrix. Searching the help has taken me quite some time and not really got me anywhere as I am not really sure what I am looking for, nor how to apply much of what I see to the problem I have.
So, assuming I somehow manage to get the most linear section of my data sorted out, I then need to get the equation of that linear section best fit line and also determine the error between the best fit and the actual data (both values being used in later equations).
Ideally I would then like to plot the actual data with the best fit (and errors?).
Please forgive the horrible code, it has been a long while since I used matlab and I was terrible at it then... computational elegance has had to give way to something that makes sense in my head.
clear all;
clc;
%Constants and variables
Cw=4185; %Specific heat capacity water
Cg=3086; %Specific heat capacity glycerol
ratiow2g=100; %ratio of specifics
C=((Cw/100)*ratiow2g)+((Cg/100)*(100-ratiow2g)); %Combined specifics
V=0.001; %Volume
m=0.003; %Mass
Em=0.0005; %Error in mass
ts=5; %Timing interval (s)
%import data
impdat=importdata('T2.xlsx');
dat=impdat.data;
szd=length(dat);
time=[0:5:((szd-1)*ts)]';
data=[time,dat];
%plot(time,dat);
%First differential of data with respect to time
for i=(1:szd-1);
ddatdtime(1:i:szd)=((dat(i+1)-dat(i))/ts);
end
%Second differential of data with respect to time
for j=(1:szd-2);
d2datd2time(1:j:szd)=((ddatdtime(j+1)-ddatdtime(j))/ts);
end
szd2=length(d2datd2time);
lims=[0.001 0.001 0.001 0.001 0.001];
res=findstr(d2datd2time, lims); %The section of the data which has the most linear slope?
ddatlin= %The gradient of the most linear portion of the data
Eddatlin = %The error in gradient/fit line
%SLP calculation
SLP=((C*V/m)*ddatlin);
%SLP error calculation
ESLP=(((C*V/m)*Eddatlin)+((C*V/m^2)*Em));
And an example of my data:
dat =
23.4000
23.4000
23.5000
23.7000
24.0000
24.2000
24.5000
24.8000
25.0000
25.2000
25.5000
25.8000
26.0000
26.2000
26.4000
26.7000
26.9000
27.1000
27.4000
27.6000
27.8000
28.1000
28.3000
28.6000
28.8000
29.0000
29.2000
29.5000
29.7000
29.9000
30.1000
30.3000
30.6000
30.8000
31.0000
31.2000
31.4000
31.6000
31.8000
32.0000
32.3000
32.5000
32.7000
32.9000
33.0000
33.3000
33.5000
33.7000
33.9000
34.1000
34.3000
34.5000
34.6000
34.9000
35.1000
35.2000
35.4000
35.7000
35.9000
36.0000
36.2000
36.4000
36.6000
36.8000
36.9000
37.2000
37.3000
37.5000
37.7000
37.9000
38.0000
38.2000
38.4000
38.5000
38.7000
38.9000
39.1000
39.2000
39.4000
39.6000
39.8000
39.9000
40.1000
40.3000
40.4000
40.6000
40.7000
40.9000
41.1000
41.2000
41.4000
41.6000
41.8000
41.9000
42.0000
42.2000
42.4000
42.5000
42.6000
42.8000
43.0000
43.1000
43.3000
43.4000
43.5000
43.7000
43.8000
44.0000
44.1000
44.3000
44.4000
44.5000
44.7000
44.8000
45.0000
45.1000
45.3000
45.4000
45.5000
45.6000
45.8000
45.9000
46.1000
46.2000
46.3000
46.5000
46.6000
46.7000
46.9000
47.0000
47.1000
47.2000

Answers (0)

Categories

Asked:

on 7 Jun 2012

Community Treasure Hunt

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

Start Hunting!