# 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 回表示 (過去 30 日間)
aref yel 2020 年 6 月 25 日

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 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
aref yel 2020 年 6 月 27 日
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 2020 年 6 月 28 日

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?

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

### 回答 (1 件)

John D'Errico 2020 年 6 月 25 日

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.

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

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by