フィルターのクリア

calculate coefficients with linear regression

9 ビュー (過去 30 日間)
Antrea Plastira
Antrea Plastira 2022 年 10 月 12 日
コメント済み: Image Analyst 2022 年 10 月 12 日
i have a set of data (xj, yj) which describe a function f(x) = a0 + a1x +a2 x^2 +a3 e^x
how can i find the coefficents a0, a1,a2,a3 modeling with linear regression and using quatratic programming.
so far i have found the slope and intercept.
my code is:
clc
clear
load('DataEx3(1).mat');
% calculating the mean for the x and y variable
x_mean = mean(xj);
y_mean = mean(yj);
% number of data points
n = length(xj);
%cross deviation of x and y
dev_xy = sum(xj*yj.') - (n*x_mean*y_mean);
%square deviation of x
dev_x = sum(xj*xj.') - (n*x_mean*x_mean);
% calculation of optimum coefficient a
a = dev_xy/dev_x;
% calculation of optimum intercept (c)
c = y_mean - (a*x_mean);
disp('value of coffiecent m is: '); disp(a);
disp('value of optimum intercept is: '); disp(c);
% best line has the form y = mx+c

採用された回答

Torsten
Torsten 2022 年 10 月 12 日
編集済み: Torsten 2022 年 10 月 12 日
A = [ones(numel(xj),1),xj,xj.^2,exp(xj)];
b = yj;
sol = A\b;
a0 = sol(1)
a1 = sol(2)
a2 = sol(3)
a3 = sol(4)
where xj and yj are used as column vectors.
  2 件のコメント
Antrea Plastira
Antrea Plastira 2022 年 10 月 12 日
when i tried this way, it appears the following message:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in assignment1 (line 31)
A = [ones(numel(xj),1),xj,xj.^2,exp(xj)];
i forgot to mention that the xj and yj data provided are [1x401] double
Torsten
Torsten 2022 年 10 月 12 日
編集済み: Torsten 2022 年 10 月 12 日
As I wrote, you have to turn xj and yj into column vectors.
Your xj and yj are row vectors.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 10 月 12 日
Why do you want to use linear regression for that very non-linear function? Why not use fitnlm to do a non-linear fit to your equation? Demos attached.
  2 件のコメント
Torsten
Torsten 2022 年 10 月 12 日
The function is linear in the parameters to be fitted. Thus a linear regression suffices.
Image Analyst
Image Analyst 2022 年 10 月 12 日
@Torsten Oh (sound of hand slapping forehead) you're right.
However if the x in the exponential had a coefficient a4,
f(x) = a0 + a1 * x + a2 * x^2 + a3 * exp(a4 *x)
then we could use fitnlm. Not sure why that last x doesn't have a coefficient, which would allow for a "slope" or different steepnesses of the exponential term. I think that would give a more flexible model. You could always put in a4, and if it's 1, then it's 1, and a linear estimation is fine. But if a4 is not 1, it might be a better model by including a4.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSupport Vector Machine Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by