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 CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by