Clear Filters
Clear Filters

Fit Model to Data

4 views (last 30 days)
mehtap agirsoy
mehtap agirsoy on 16 Apr 2021
Answered: mehtap agirsoy on 16 Apr 2021
Hello there,
I have a question regardind fitting model to data. I have 3 models, my problem is with time constant. Time constant parameters that are coming out of time vector, T, rather than parameter vector, P. This effectively gives them fixed values for time constants that are far too low. All of the models should fit the data fairly well. I ccould not figure it out and need help.
Main code is
close all; clear all;
% SET CALIBRATION CONSTANT
CF = 2.1931;
% LOAD DATA FILE
[fname,pname] = uigetfile('*.txt','LOAD DATA FILE');
DAT = dlmread([pname,'\',fname]);
% TIME IS COLUMN 1, DISPLACEMENT IS COLUMN 2
TIME = DAT(:,1);
DISP = CF*DAT(:,2); % APPLY CALIBRATION TO DISPLACEMENT
% CLIP AT 59.9s
IN = find(TIME >= 59.9);
TIME = TIME(IN);
DISP = DISP(IN);
% ZERO INITIAL AND FLIP DISPLACEMENT
TIME = TIME - TIME(1);
DISP = -DISP + DISP(1);
Y=DISP;
% DEFINE FUNCTION
[YM1] = MODEL_1(TIME,Y);
[YM2] = MODEL_2(TIME,Y);
[YM3] = MODEL_3(TIME,Y);
YM=[YM1 YM2 YM3];
My model function is
function [YM1] = MODEL_1(TIME,Y)
MODEL = @(P,T) P(1)+P(2).*(1-exp(-T./T(3)));
P0 = [0,1,1]; % INITIAL GUESS VALUES FOR PARAMETERS
PF = lsqcurvefit(MODEL,P0,TIME,Y);
YM1 = MODEL(PF,TIME);
% CALCULATE ADJUSTED R^2 HERE
Ybar = mean(Y); % AVERAGE OBSERVED
SStot = sum( (Y - Ybar).^2 ); % TOTAL SUM OF SQUARES
SSres = sum( (Y - YM1).^2 ); % RESIDUAL SUM OF SQUARES
R2 = 1 - SSres/SStot; % R SQUARED
n = length(Y); % NUMBER OF OBSERVATIONS
p = length (PF); % NUMBER OF PARAMETERS
Rbar2 = 1 - (1 - R2)*(n - 1)/(n - p - 1);
disp(['R^2 = ',num2str(R2),', ADJUSTED R^2 = ',num2str(Rbar2)])
end
and main code is
close all; clear all;
% SET CALIBRATION CONSTANT
CF = 2.1931;
% LOAD DATA FILE
[fname,pname] = uigetfile('*.txt','LOAD DATA FILE');
DAT = dlmread([pname,'\',fname]);
% TIME IS COLUMN 1, DISPLACEMENT IS COLUMN 2
TIME = DAT(:,1);
DISP = CF*DAT(:,2); % APPLY CALIBRATION TO DISPLACEMENT
% CLIP AT 59.9s
IN = find(TIME >= 59.9);
TIME = TIME(IN);
DISP = DISP(IN);
% ZERO INITIAL AND FLIP DISPLACEMENT
TIME = TIME - TIME(1);
DISP = -DISP + DISP(1);
Y=DISP;
% DEFINE FUNCTION
[YM1] = MODEL_1(TIME,Y);
[YM2] = MODEL_2(TIME,Y);
[YM3] = MODEL_3(TIME,Y);
YM=[YM1 YM2 YM3];
  2 Comments
Image Analyst
Image Analyst on 16 Apr 2021
Unfortunately you forgot to attach any .txt files so no one can do anything with that code.
I guess you blew right past the posting guidelines. But luckily you can read them now. They are in the "Community Guidelines" link below or here's another one:
Make it easy for us to help you, not hard. We'll check back later for the txt file.
Star Strider
Star Strider on 16 Apr 2021
Here the function lists 2 parameters, however the initial estimat vector is for 3:
MODEL = @(P,T) P(1)+P(2).*(1-exp(-T./T(3)));
P0 = [0,1,1]; % INITIAL GUESS VALUES FOR PARAMETERS
Should ‘MODEL’ be:
MODEL = @(P,T) P(1)+P(2).*(1-exp(-T./P(3)));
instead?
Also, 0 is never a good initial choice for a parameter estimate.

Sign in to comment.

Accepted Answer

mehtap agirsoy
mehtap agirsoy on 16 Apr 2021
Many thanks

More Answers (1)

mehtap agirsoy
mehtap agirsoy on 16 Apr 2021
Thanks for the quideline. I will update my question and will insert .txt file.

Categories

Find more on General Applications in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!