polyfit whats wrong?

x = [0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3]
y = [0.652;0.217;0.264;0.0689;-0.183;0.209:0.529;0.689;1.18;1.48;2.21;3.09;3.64;]
p1 = polyfit( x, y, 4)
why isn´t this working?

回答 (1 件)

Star Strider
Star Strider 2020 年 12 月 21 日

1 投票

why isn´t this working?
It should throw an error telling you exactly what the problem is. When I ran the code you posted, the result was:
Error using polyfit (line 44)
The first two inputs must have the same number of elements.
Here, ‘x’ is a (13x1) vector and ‘y’ is a (12x1) vector.
They have to have the same numbers of elements. It appears that an element is missing from ‘y’.

7 件のコメント

Joao Mendes
Joao Mendes 2020 年 12 月 21 日
x = [0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3]
y = [0.652;0.217;0.264;0.0689;-0.183;0.209; 0.529;0.689;1.18;1.48;2.21;3.09;3.64;]
p1 = polyfit( x, y, 4)
what about now?
Joao Mendes
Joao Mendes 2020 年 12 月 21 日
it doesn´t say, or I can´t find it
Im using matlab online
Star Strider
Star Strider 2020 年 12 月 21 日
That works!
If you want to also see the plot:
x = [0;0.25;0.5;0.75;1;1.25;1.5;1.75;2;2.25;2.5;2.75;3];
y = [0.652;0.217;0.264;0.0689;-0.183;0.209; 0.529;0.689;1.18;1.48;2.21;3.09;3.64;];
p1 = polyfit( x, y, 4);
yv = polyval(p1, x);
figure
plot(x, y, 'p')
hold on
plot(x, yv, '-r')
hold off
grid
.
Joao Mendes
Joao Mendes 2020 年 12 月 21 日
Thanks for the help!
it does work on my friends matlab but on my own it doesn´t :)
Jan
Jan 2020 年 12 月 21 日
編集済み: Jan 2020 年 12 月 21 日
Who doesn' say what? For me the command is working now.
Joao Mendes
Joao Mendes 2020 年 12 月 21 日
on my computer it says:
Attempt to execute SCRIPT polyfit as a function:
/MATLAB Drive/polyfit.m
on my friend´s, with exactly the same code it works
Star Strider
Star Strider 2020 年 12 月 21 日
You have a script that you named ‘polyfit’. That’s the problem.
The solution is to change the name of your script to something like ‘mypolyfit’ or something else that makes sense in the context of whatever you’re doing.

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

カテゴリ

質問済み:

2020 年 12 月 21 日

コメント済み:

2020 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by