curve fitting - MATLAB nlinfit with multiple unknowns -
curve fitting - MATLAB nlinfit with multiple unknowns -
i trying fit curve using nlinfit, however, have 3 unknowns.
the equation trying fit laminar velocity profile in tube.
u = umax( 1 - ( r / r ) ^ 2 ), u velocity, umax centerline velocity, r distance centerline , r radius of tube.
the problem umax, r , r unknowns.
http://www.mne.psu.edu/cimbala/learning/fluid/cv_momentum/pipe_eg.gif
from image above, centerline axis. have included code 1 set of data:
clc clear xmean = [0.13518 0.1599 0.17035 0.18053 0.18849 0.19577 0.19373 0.18781 0.17245 0.15728 0.13404 0.10981]; r = [-5.5000 -4.5000 -3.5000 -2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000 3.5000 4.5000 5.5000]; plot( xmean, r ) i not sure how format equation handle:
eqn = @(u,y) u(1).*( 1 - ( ( u(2) - y ) / u(3) ).^2 ); [ beta, r, j, covb, mse ] = nlinfit( ymean, r, eqn, alpha ); i appreciate help trying fit data. time.
in order compute rmse neccessary have maximum of 1 y value per x value, mathematically speaking "function". can plot later 1 time again way sketch shows it. computation: rotate 90 degree.
for optimization, need initiall guess (your alpha), not defined. however, minor changes in code below, 1 can optimal parameters. might improve readable. plot shown below.
plot(r, xmean ,'ko', 'markerfacecolor', 'black') % // need "function" (max 1y value 1x-value) eqn = @(par, x) par(1).*( 1 - ( (par(2) - x) / par(3) ).^2 ); par0 = [1, 1, 1]; % // initial guess start optimization par_fit = nlinfit(r, xmean, eqn, par0 ); % // optimize hold on r_fit = linspace(min(r), max(r)); % // more points smoother curve plot(r_fit, eqn(par_fit, r_fit), 'r') optimal parameters returned are
par_fit = 0.1940 -0.4826 9.0916 you might want rotate plot 1 time again right orientation, in sketch.
additional info: polyfit() instead of nlinfitsince 1 can compute flow profile analytically, 1 knows in advance is parabola. can utilize polyfit fit parabola.
par_poly_fit = polyfit(r, xmean, 2) % // yields values: par_poly_fit = -0.0023 -0.0023 0.1934 plotting paraobla gives line pretty much identical line, got optimizer, more stable since not depend on initial value.
plot(r_fit, polyval(par_poly_fit, r_fit),'g--' matlab curve-fitting
Comments
Post a Comment