Smoothing polyval fit?

조회 수: 11 (최근 30일)
Emil Doyle
Emil Doyle 2021년 2월 6일
댓글: Image Analyst 2021년 2월 6일
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?

답변 (2개)

Image Analyst
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.
  댓글 수: 1
Emil Doyle
Emil Doyle 2021년 2월 6일
Thanks for your answer,
I've attached them. Unfortunately it isn't feasible to put more x values in, as this data is from an experiment done a while ago. The data from the experiment are "integrand term.txt" and "yw.txt", while "wake velocity.txt" and "pitot position" are taken from commercial software (where obviously it is easier to get more data points).

댓글을 달려면 로그인하십시오.


Image Analyst
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
Emil Doyle
Emil Doyle 2021년 2월 6일
I don't understand how I'm supposed to write the model function? What is it supposed to be?
Image Analyst
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?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by