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 天)
显示 更早的评论
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 个评论
John D'Errico
2020-6-28
编辑: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
编辑:John D'Errico
2020-6-26
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?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!