フィルターのクリア

How can I modify this code so it gives the value 0 for c. It should only provide ax^2+bx

3 ビュー (過去 30 日間)
Ahmed
Ahmed 2020 年 12 月 4 日
編集済み: Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
function [coefficients, MSE] = pnnnfit(x, y, order)
coefficients = polyfit(x, y, order)
y_fitted = polyval(coefficients, x)
squaredError = (y - y_fitted) .^ 2
MSE = sum(squaredError)
end
%x=[0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5];
% y=[0 -5.27 -8.10 -8.46 -6.38 -1.84 5.15 14.59 26.48 40.83 57.63];
ans =
4.9069 -13.0140 0.0265
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 4 日
If you mean that a^2+b*x should exactly equal y, then that is not the case.
The best degree 2 fit is 61067/12450 * x^2 - 17802227/1369500*x + 10357/136950000

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

回答 (1 件)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
%x=[0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5];
%y=[0 -5.27 -8.10 -8.46 -6.38 -1.84 5.15 14.59 26.48 40.83 57.63];
function [coefficients] = pnnnfit(x, y)
coefficients=[sum(x.^4) sum(x.^3) ; sum(x.^3) sum(x.^2)]\[sum(y.*x.^2) sum(y.*x)]';
% a = coefficients(1)
% b = coefficients(2)
% you need to add more code to find MSE but it is simple
plot(x,y,x,coefficients(1)*x.^2+coefficients(2)*x)
end
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 4 日
Odd.. when you could just
[x(:).^2, x(:)]\y(:)
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
編集済み: Asad (Mehrzad) Khoddam 2020 年 12 月 4 日
Yes, you are right. I used the actual mathematical equations for the least squares method. Matlab has a simplified '\' operator for solving equations and the least square method

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

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by