need help to fix the error! least square curve d = E *t^2 + D* t

1 回表示 (過去 30 日間)
Ahmed
Ahmed 2020 年 12 月 1 日
回答済み: Image Analyst 2020 年 12 月 1 日
function [ m, Y ] = pnnnfit( x, y, n )
m=polyfit(x,y,n)
y_new=polyval(m,x)
er_sq=(y-y_new).^2
Y=sum(er_sq)
end
x=[0 0 0;1 1 0;4 2 0; 9 3 0; 16 4 0; 25 5 0; 36 6 0; 49 7 0; 64 8 0; 81 9 0; 100 10 0];
y=[ 0; -5.2;-8.10;-8.46;-6.38;-1.84;5.15;14.59;26.48;40.83;57.63];
pnnnfit(x,y,2)
Error using polyfit (line 44)
The first two inputs must have the same number of elements.
Error in pnnnfit(line 2)
t=polyfit(x,y,n)
  1 件のコメント
Yusuf Selim KARATAS
Yusuf Selim KARATAS 2020 年 12 月 1 日
Dear Ahmed,
I do not know which line is 44 but as far as I understand you have x matris [3x11] and y matris as [1x11]. Matlab warns you that these two need to have same dimensions.
I am not sure. I just wanted to give you an idea.
THanks.

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

回答 (1 件)

Image Analyst
Image Analyst 2020 年 12 月 1 日
x has 3 columns whereas y has only 1. Try fitting only one column of x, like this:
x=[0 0 0;1 1 0;4 2 0; 9 3 0; 16 4 0; 25 5 0; 36 6 0; 49 7 0; 64 8 0; 81 9 0; 100 10 0]
y=[ 0; -5.2;-8.10;-8.46;-6.38;-1.84;5.15;14.59;26.48;40.83;57.63]
% Fit first column only
[m, Y] = pnnnfit(x(:, 1), y, 2)
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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by