📄 lsm_fun.m
字号:
function [Pfit_inv]=lsm_fun(x,y,n)
% solve LSM problem by calculate P0=inv(A)*b
% the function returns coefficients in decrease
% version: lsm v0.2
[temp,N]=size(x); % N: elements number
highorder=n; % the highest order of the polynomial
P0=zeros(highorder+1,1); % increase by row
Pfit_inv=zeros(1,highorder+1); % decrease by colum
A=zeros(highorder+1,highorder+1); % A*P0=b
b=zeros(highorder+1,1);
for row0=0:highorder % 计算法方程
for colum0=row0:highorder
row=row0+1;colum=colum0+1; % for compilers that index from 1 not 0
% row=row0; colum=colum0; % for compilers that index from 0 not 1
for i0=0:(N-1)
ii=i0+1;
% ii=i0;
A(row,colum)=A(row,colum)+power(x(ii),colum0)*power(x(ii),row0);
end
A(colum,row)=A(row,colum);
end
for i0=0:(N-1)
ii=i0+1;
% ii=i0;
b(row)=b(row)+y(ii)*power(x(ii),row0);
end
end
P0=inv(A)*b; % LU deco
Pfit_inv=P0(highorder+1:-1:1)';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -