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

1 Ansicht (letzte 30 Tage)
Ahmed
Ahmed am 1 Dez. 2020
Beantwortet: Image Analyst am 1 Dez. 2020
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 Kommentar
Yusuf Selim KARATAS
Yusuf Selim KARATAS am 1 Dez. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 1 Dez. 2020
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

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by