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

1 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by