clearclc% 插值x = 0:pi/4:2*pi;y = sin(x);xq = 0:pi/10:2*pi;figureyq1 =interp1(x,y,xq); % 線性yq2 =interp1(x,y,xq,'nearest'); % 鄰近yq3 =interp1(x,y,xq,'spline'); % 三次樣條subplot(3,1,1);plot(x,y,'ro',xq,yq1,'r-*','LineWidth',1);xlim([0 2*pi]);subplot(3,1,2);plot(x,y,'ro',xq,yq2,'r-*','LineWidth',1);xlim([0 2*pi]);subplot(3,1,3);plot(x,y,'ro',xq,yq3,'r-*','LineWidth',1);xlim([0 2*pi]);[fitobject,gof, output] = fit(x, y, fitType, fitOptions)fitType為擬合函數的類型,主要包括線性、二次、樣條、自定義函數等;fitOptions為擬合的設置,內容較多,可參閱幫助;fitobject為擬合的結果,包括擬合的曲線(面),參數的置信區間等;gof為一些統計信息,主要為誤差信息,如RMSE等;clearclc% 擬合figureN = 20;x =linspace(-2,2,N);x = x';r =0.25*randn(N,1);y = x.^4+r; % 原方程subplot(1,3,1);f1 =fit(x,y,'poly1'); % 線性plot(f1,x,y,'o');legend(subplot(1,3,1),'off');subplot(1,3,2);f2 =fit(x,y,'poly2'); % 二次plot(f2,x,y,'o');legend(subplot(1,3,2),'off');subplot(1,3,3);guassEqn ='a*x^4+b*x^3+c*x^2+d*x+e'; % 自定義函數startP = [0.5 0.50.5 0.5 0.5]; % 初值[f3,gof,output] =fit(x,y,guassEqn,'Start',startP); % 自定義函數擬合plot(f3,x,y,'o');legend(subplot(1,3,3),'off');gof =sse: 1.2098rsquare: 0.9977dfe: 15adjrsquare: 0.9971rmse: 0.2840
output =numobs: 20numparam: 5residuals: [20x1 double]Jacobian: [20x5 double]exitflag: 1firstorderopt: 3.1844e-08iterations: 2funcCount: 18cgiterations: 0algorithm: 'trust-region-reflective'stepsize: 2.3681e-08 message: 'Success. Fitting convergedto a solution.'本文介紹了插值與擬合在Matlab中基本使用方法,如有興趣的讀者可留言進一步討論相互學習。作者:數聯科技工作室
聲明:首發數聯科技工作室公眾號(ID:DigitUnionTecHouse),部分圖片源自網絡,如有不當請聯繫我們,歡迎分享。
如果您正在學習CAE ,歡迎加入我們的學習型工程師社群,與我們抱團一起學習理論、軟體和行業應用。更多詳情請諮詢仿真小助手(在仿真秀公眾號對話框回復 小助手 ,備註 進群 即可)