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

2 views (last 30 days)
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 Comments
aref yel
aref yel on 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 on 28 Jun 2020
Edited: John D'Errico on 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?

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 25 Jun 2020
Edited: John D'Errico on 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?

Tags

Community Treasure Hunt

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

Start Hunting!