Fit Model to Data
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2021-4-16
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
2021-4-16
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.
采纳的回答
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Testing Frameworks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!