Azzera filtri
Azzera filtri

Smoothing polyval fit?

7 visualizzazioni (ultimi 30 giorni)
Emil Doyle
Emil Doyle il 6 Feb 2021
Commentato: Image Analyst il 6 Feb 2021
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?

Risposte (2)

Image Analyst
Image Analyst il 6 Feb 2021
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 Commento
Emil Doyle
Emil Doyle il 6 Feb 2021
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).

Accedi per commentare.


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

Accedi per commentare.

Categorie

Scopri di più su Curve Fitting Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by