Smoothing polyval fit?
7 次查看(过去 30 天)
显示 更早的评论
I'm trying to make a line of best fit when plotting. I have two sets of data. This is my code:
clc
clear
close all
uw = importdata("wake velocity.txt");
pos = importdata("pitot position.txt") + 0.02921;
u = 36.3;
c = 0.1524;
intexp = importdata("integrand term.txt");
yw = importdata("yw.txt");
for i = 1:length(uw)
integrand(i) = (uw(i)/u)-(uw(i)/u)^2;
end
%line of best fit
p_cfd = polyfit(pos,integrand,10);
f_cfd = polyval(p_cfd,pos);
p_exp = polyfit(yw,intexp,8);
f_exp = polyval(p_exp,yw);
%plotting
plot(100*yw,intexp,'b');
hold on
plot(100*yw, f_exp,'--b');
plot(100*pos,integrand,'r')
plot(100*pos,f_cfd,'--r');
xlabel("\it y_w (cm)");
ylabel("\it Integrand term");
legend("Experimental","CFD");
When i run the code, this is the plot I get this plot:
As you can see, the polynomial fit does not fit the blue curve very well.
I would like the blue plot to be smooth (as I've tried to show below). How would I do this without adding more data points?
0 个评论
回答(2 个)
Image Analyst
2021-2-6
You need to put in way more x values for your fit than you do for your training set if you want the curve to look smooth. Otherwise you get values just at the few training x locations and with straight lines drawn between them it will look rather chunky. You do NOT need to have the same number of x values in polyval() as you did in polyfit().
Attach the 3 text files if you want more help.
Image Analyst
2021-2-6
You should not be using a 10th order polynomial to fit Gaussian Data. Use fitnlm to fit a Guassian. See attached demos. Adapt them to your data. If you can't do it, write back.
2 个评论
Image Analyst
2021-2-6
You just take the noisy sample data im my demo and replace it with your actual "blue" data. Use the fit_two_gaussians since it looks like the model you should use would have two Gaussians in it. Are you going to try it?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!