why the result of the following code vector y is not equals to y1 or y2 . How can I get the equals of them.

1 Ansicht (letzte 30 Tage)
clc
clear all;
close all;
x=[4757;4767;4866;4890;4892;4990;4996;5106];
y=[3387;3431;3438;3549;3580;3590;3549;3662];
aa(1,:)=polyfit(x,y,1);
y2=polyval(aa(1,:),x);
%%%%%%%
y3=x*aa(:,1)+aa(:,2);
%%%%%%%
  3 Kommentare
aref yel
aref yel am 27 Jun. 2020
As I mentioned in this quastion, why we gained y ~= y2 or y =~ y3 . I woulde like to get result y==y2 or y==y3. how can I code them (y2 or y3).
John D'Errico
John D'Errico am 28 Jun. 2020
Bearbeitet: John D'Errico am 28 Jun. 2020
Seriously, that is your question? You wish to see y == y2? (Or y == y3).
x=[4757;4767;4866;4890;4892;4990;4996;5106];
y=[3387;3431;3438;3549;3580;3590;3549;3662];
plot(x,y)
So you have data that does NOT fall on a straight line.
But you performed a straight line fit to data that is not linear. And you are surprised that the straight line fit did not reproduce your data exactly?
Really, if you are going to wish for something impossible, I would personally wish for peace and harmony around the world, or something equally worthwhile.
So is your question really how to use INTERPOLATION, as opposed to a polynomial fit to your data? A polynomial fit (thus polyfit) is an approximation. Is that what you need, as opposed to the question you have asked?
Or is your question to ask if sometimes y2 and y3 might produce infinitessimally different results for higher order polynomials?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 25 Jun. 2020
Bearbeitet: John D'Errico am 26 Jun. 2020
y2
y2 =
3414.6226519281
3421.81651603882
3493.03577073497
3510.3010446007
3511.73981742285
3582.23968570792
3586.55600417435
3665.68850939229
>> y3
y3 =
3414.6226519281
3421.81651603882
3493.03577073497
3510.3010446007
3511.73981742285
3582.23968570792
3586.55600417435
3665.68850939229
>> y2 - y3
ans =
0
0
0
0
0
0
0
0
>> y2 == y3
ans =
8×1 logical array
1
1
1
1
1
1
1
1
While you should normally not expect floating point numbers computed in two different ways to be identical, they are so here.
So what is your problem?

Kategorien

Mehr zu Polynomials 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