How to plot a 3rd order best fit line through 3 sets of data?
1 次查看(过去 30 天)
显示 更早的评论
I have 3 sets of data as vectors. X terms Cp80, Cp50, Cp30 and their corresponding Cv80, Cv50, and Cv30 y terms. I currently am using this block of code to plot them.
figure
hold on
plot(Cv80,Cp80,'*')
plot(Cv50,Cp50,'o')
plot(Cv30,Cp30,'p')
hold off
I need to run a third order best fit line through these three data sets and generate the R^2 value. I am unfamiliar with Polyfit, as I always use the basic fitting tools on the plot, and the documentation is less than helpful. I would appreciate it greatly if someone could show me how to fit one line and its R^2 value to these three datasets.
0 个评论
回答(2 个)
Roger Stafford
2016-4-5
编辑:Roger Stafford
2016-4-5
How about
x1 = [Cv80(:);Cv50(:);Cv30(:)];
y1 = [Cp80(:);Cp50(:);Cp30(:)];
p = polyfit(x1,y1,3);
y = polyval(p,x1);
plot(x1,y)
If the values of x1 are not monotone, you can first sort them:
x1 = [Cv80(:);Cv50(:);Cv30(:)];
y1 = [Cp80(:);Cp50(:);Cp30(:)];
[x1,ix] = sort(x1);
y1 = y1(ix);
p = polyfit(x1,y1,3);
y = polyval(p,x1);
plot(x1,y)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!